Next.js: Uncaught ReferenceError: webpackHotUpdate is not defined

Created on 1 Apr 2019  路  7Comments  路  Source: vercel/next.js

Bug report

Describe the bug

Hi, we encountered an weird exception "Uncaught ReferenceError: webpackHotUpdate is not defined" in our next.js project. This exception occurs each time we reload the page.

I found it is related to our custom webpack config. We prepend an entry that contains some polyfills in the webpack config and the problem occurs.

It seems that webpack added a file of which url is like http://localhost:3000/_next/static/webpack/static/runtime/main.js.0e14f9bffebb9d8aa940.hot-update.js. (let's call it hot-update.js later) In this file, a global function named webpackHotUpdate is called.

webpackHotUpdate is defined in in http://localhost:3000/_next/static/runtime/webpack.js. In fact, this script is already in the html content before the hot-update.js. But it's an async script, somehow it is executed after hot-update.js.

Since the scripts has some dependencies, I think next.js should render scripts nodes as defer script instead of async script.

To Reproduce

clone this repo: https://github.com/lukeupup/nextjs-bug-report
and run: npm run dev

Expected behavior

scripts that have dependencies should be executed in order.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: OSX
  • Browser (if applies) Chrome Version 73.0.3683.86 (Official Build) (64-bit) / Firefox 65.0.2
  • Version of Next.js: 7.0.3 (8.0.3 also has this problem)

Most helpful comment

Actually, I got rid of the error by checking if the polyfill was part of the config before unshifting it. Because I noticed it was part of the array twice. Now I don't get the hot reload error anymore.

  webpack: (config, { buildId, dev, isServer, defaultLoaders }) => {

    const originalEntry = config.entry;
    config.entry = async () => {
      const entries = await originalEntry();
      if (entries['main.js'] && !entries['main.js'].includes('./polyfills.js')) {
        entries['main.js'].unshift('./polyfills.js');
      }
      return entries;
    };

All 7 comments

I guess this is triggered by you changing the polyfill file you're adding into Next.js internals, by default main.js wouldn't have hot updates as it never changes.

Hmm, the problem is that I haven't changed the polyfill. webpackHotUpdate is not executed by I change something, but executed by each time we open / reload the page. The hot-update.js is rendered in the page in SSR stage.

The real problem here is the script files that webpack produces must be executed in a certain order, and