next.js: Custom polyfills sometimes do not work in IE11
In IE11 our custom scripts/polyfills.js sometimes do not work.
Desciption
Here’s contents of scripts/polyfills.js:
import 'core-js';
import 'react-app-polyfill/ie9';
console.log('MY POLYFILL!');
Here’s contents of next.config.js:
const withTM = require('next-transpile-modules');
const withPlugins = require('next-compose-plugins');
const withBundleAnalyzer = require('@next/bundle-analyzer');
const withSourceMaps = require('@zeit/next-source-maps');
const nextConfiguration = {
experimental: {
granularChunks: true,
modern: false
},
useFileSystemPublicRoutes: false,
poweredByHeader: false,
webpack: (config, { defaultLoaders }) => {
const originalEntry = config.entry;
config.entry = async () => {
const entries = await originalEntry();
if (
entries['main.js'] &&
!entries['main.js'].includes('./scripts/polyfills.js')
) {
entries['main.js'].unshift('./scripts/polyfills.js');
}
return entries;
};
config.module.rules.push({
test: /\.svg$/,
use: [
defaultLoaders.babel,
{
loader: 'react-svg-loader'
}
]
});
return config;
}
};
module.exports = withPlugins(
[
[withTM, { transpileModules: ['ui-lib'] }],
[withSourceMaps(), { devtool: 'source-map' }],
[withBundleAnalyzer, { enabled: process.env.ANALYZE === 'true' }]
],
nextConfiguration
);
Error example -

Minified React error #31; visit https://reactjs.org/docs/error-decoder.html?invariant=31&args[]=object%20with%20keys%20%7B%24%24typeof%2C%20type%2C%20key%2C%20ref%2C%20props%2C%20_owner%7D&args[]=
Scripts in html look like this -

Our custom polyfill is part of main.js which is loaded after frameworks.js bundle (the place where React is). If you can see both scripts are async meaning that sporadically frameworks.js loads first which ends in error.
If we include <script src="https://cdn.polyfill.io/v2/polyfill.min.js" /> in the head of the page error disappears.
Question - what’s the new way to use custom polyfills with Next.js 9.2.2 and granular chunks built-in?
Thanks in advance!
System information
- OS: Windows 10
- Browser: Internet Explorer 11
- Version of Next.js: 9.2.2
Additional context
Is this still up to date - https://github.com/zeit/next.js/tree/canary/examples/with-polyfills ?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 3
- Comments: 18 (9 by maintainers)
@mattclough1 same here.
with-polyfills exampleapproach isn’t working anymore. Tested on v9.4. (we’re usingclient/polyfills.jsnot only for IE11, but also for critical-css https://github.com/dotsunited/load-css-polyfill)Next.js 9.3 will mostly solve this, and the default recommendation for polyfill inclusion will be to import in the top of
pages/_app.js.Can you try upgrading to
next@canary?the workaround that worked for us -
@timneutkens, I also tried @mattclough1’s repo with v9.3.0 and got the same result.
To second this, I had issues polyfilling for IE11 with the
with-polyfillsexample in a completely fresh project. Here’s an example: https://github.com/mattclough1/next-ie11-polyfill-testI also attempted to upgrade to
next@canary, which had the same result, but it seems like the polyfilling approach may be different for that version.I was able to make it work by downgrading to
v9.0.8just a note that this problem - https://github.com/zeit/next.js/issues/10794#issuecomment-594023086 was not the case in Next.js
v9.0.5