question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

with-polyfills: Dynamic import of polyfill causes chunk load error

See original GitHub issue

Examples bug report

Example name

with-polyfills

Describe the bug

with-polyfills shows how to polyfill missing features prior to initialization of Next app. However, I would like to load polyfills only for the missing features using the dynamic import.

For example IntersectionObserver is supported widely in the latest versions of browsers, so it’s usually not necessary to load the intersection-observer polyfill. Thus I would like to import the polyfill only when the feature is missing like this:

// client/polyfills.js

async function loadPolyfills() {
  if (typeof window.IntersectionObserver === 'undefined') {
    await import('intersection-observer')
  }
}
export default loadPolyfills()

When I do that the application crashes when browser attempts to load the polyfill with the following error:

Unhandled Rejection (ChunkLoadError): Loading chunk 0 failed.
(error: http://localhost:3000/static/chunks/0.js)

Apparently Webpack points to the chunk in a wrong location, since the chunk is available in _next directory, i.e. http://localhost:3000/_next/static/chunks/0.js

To Reproduce

I have prepared a repository with reproduction of the issue. The feature detection is commented out to reproduce the behavior in any browser.

  1. Download or clone https://github.com/jnv/next-with-polyfills-dynamic-import
  2. Install dependencies with yarn
  3. Start application with yarn dev
  4. Go to http://localhost:3000/
  5. See the error

Expected behavior

Browser loads the chunk without error, before Next initialization.

Screenshots

Error in browser

System information

  • OS: macOS
  • Browser: does not apply
  • Version of Next.js: 9.0.2

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:33
  • Comments:25 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
JackCAcommented, Jan 8, 2020

I’d remove the word “polyfill” from this issue because it seems to just flat out happen for dynamic imports. I am still continually hit by this

2reactions
ccambournaccommented, Jan 9, 2021

Hi,

I experienced the same issue and fixed it inside custom Webpack configuration file by adding the following:

// One-time procedures to run
if (!isServer) {
	// For polyfills chunks to be correctly loaded
	config.output.publicPath = '/_next/';
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Solve the Chunk Load Error in JavaScript - Rollbar
Whenever there's an error observed in dynamically fetching helper JavaScript files known as Chunks, a ChunkLoad Error is thrown.
Read more >
Lazy loading polyfills with WebPack and dynamic imports
Conclusion. Splitting polyfills into separate chunks helps to limit size of the application for the majority of users running a modern browser.
Read more >
An Annotated webpack 4 Config for Frontend Web…
Since we're using import() in an async function (“ main ”), that function await s the result of our dynamically loaded JavaScript import...
Read more >
How to fix ChunkLoadError in your ReactJS application
If you use code splitting and dynamic imports, old code can cause this issue. Here's how to fix it ... What can cause...
Read more >
module not found: error: can't resolve 'fs' angular - You.com
The error is because of angular-cli does not support modules in node like "fs" ... module loading 'node_modules/systemjs/dist/system.src.js', // Polyfills ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found