Skip to content

hot reload does not work if the HTML does not include <body> #153

@trusktr

Description

@trusktr

version 0.4.0

Describe the bug

For example, given an HTML file like this,

<style>h1 {color: blue}</style>
<h1>hello</h1>

the server will not reload when changes are made. Instead, the file has to be written like this:

<body>
  <style>h1 {color: blue}</style>
  <h1>hello</h1>
</body>

I think the issue is here:

const injectCandidates = [new RegExp('</head>', 'i'), new RegExp('</html>', 'i'), new RegExp('</body>', 'i')]
let match
for (let i = 0; i < injectCandidates.length; ++i) {
match = injectCandidates[i].exec(data)
if (match) {
this.injectTag = match[0]
break
}
}
if (this.injectTag) {
data = data.replace(this.injectTag, this.code + this.injectTag)
}

Looks like if it does not find </head>, </html>, or </body>, then it will not inject code.

I think it should have a last resort which is to simply append the injected script tag to the end of the file (this means it will implicitly be at the end of the <body> that the browser will generate).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions