Parcel: Hot reload doesn't work for an empty HTML page without external CSS/JS imports

Created on 4 Mar 2018  ·  9Comments  ·  Source: parcel-bundler/parcel

### 🐛 bug report

No files or configs

🤔 Expected Behavior

I have empty HTML without any external resources (such as local CSS files or JS). When I change it and save I should get an immediate update in the browser without refresh while in dev mode.

😯 Current Behavior

I change anything in the HTML file, it gets rebuilt but not refreshed automatically in the browser. I need to click refresh.

💁 Possible Solution

When I add any import. For example to the CSS external local file and then refresh the page once, all changes to HTML start to immediately displayed on saving without a need to press refresh

💻 Code Sample

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<p style="color: red">Hello world</p>
</body>
</html>

🌍 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | latest to date
| Node | 8.9.3
| Yarn | 1.5
| Operating System | Mac OS High Sierra

Bug Help Wanted HMR ✨ Parcel 2

Most helpful comment

There is no javascript bundle to append the HMR runtime to.
https://github.com/parcel-bundler/parcel/blob/420ed63ed18c6a09e8b25754d0142b3b87ebcd71/src/packagers/JSPackager.js#L185-L191

We would have to inject a script tag into the html file.

All 9 comments

There is no javascript bundle to append the HMR runtime to.
https://github.com/parcel-bundler/parcel/blob/420ed63ed18c6a09e8b25754d0142b3b87ebcd71/src/packagers/JSPackager.js#L185-L191

We would have to inject a script tag into the html file.

Glad that my bug contribution is not false positive :) Waiting for the fix ;)

Could I work on this?

@vsui I think you could. This bug is quite annoying.

I'll take this on if it's still open @RIP21 @vsui.

This sure needs to be fixed. I’m not familiar with the internals of Parcel, but I guess that to support auto reloading without JS, we’d have to:

  1. create a browser module including HMR runtime, and then
  2. inject it to the HTML.

Browsersync injects its client script after the opening <body> tag, would that be wise to do here too?

💁 Possible Solution

if page has no one scripts, you must automatically add reload helper script, to the beginning of the head element, like:

<head>
  <script id="__parcel__helper__">
    /* code */
    /* code */
    /* code */

    // clear html from helper script tag, just in case
    var helperScriptElement = document.getElementById('__parcel__helper__')
    helperScriptElement.parentNode.removeChild(helperScriptElement);
  </script>

  <title> e.t.c. </title> 
</head>

I finally "solved" this by adding a live.js script into the *.pug pages. It does a good job at reloading the pages--might not be as fast as Parcel's hmr, but it works.

// index.pug
if env === 'development'
    script(src="https://livejs.com/live.js#html")
// pug.config.js
module.exports = {
    locals: {
        env: process.env.NODE_ENV
    }
};

Don't forget to remove this line when building for production.

Interested by this feature.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

v0idifier picture v0idifier  ·  51Comments

natqe picture natqe  ·  40Comments

Znarkus picture Znarkus  ·  38Comments

edo-codes picture edo-codes  ·  42Comments

mattdesl picture mattdesl  ·  43Comments