Make live reload work with missing </body>

The HTML spec doesn’t require it, and I prefer to omit it. This has been
bothering me for ages, but I hadn’t gotten round to fixing it yet.

This can cause nominally invalid HTML to be emitted, if `</body>` was
omitted but `</html>` was present, but that’s unlikely to happen, and
this is for development purposes only, and the right thing will happen
anyway in all environments (per browser behaviour and spec).

I don’t think this warrants a changelog entry.
This commit is contained in:
Chris Morgan 2020-03-29 16:15:51 +05:30
parent cc64803553
commit cc01d3f82f

View file

@ -626,15 +626,17 @@ impl Site {
}
/// Inject live reload script tag if in live reload mode
fn inject_livereload(&self, html: String) -> String {
fn inject_livereload(&self, mut html: String) -> String {
if let Some(port) = self.live_reload {
return html.replace(
"</body>",
&format!(
r#"<script src="/livereload.js?port={}&amp;mindelay=10"></script></body>"#,
port
),
let script = format!(
r#"<script src="/livereload.js?port={}&amp;mindelay=10"></script>"#,
port,
);
if let Some(index) = html.rfind("</body>") {
html.insert_str(index, &script);
} else {
html.push_str(&script);
}
}
html