next.js: NextJS 9.3 polyfills on IE11 break application
Bug report
Describe the bug
On NextJS 9.2 I had setup to use corejs@3 for Polyfills. Reading that NextJS 9.3 does auto-polyfill for IE11 when needed, I removed the custom babel config and it “works” sometimes.
I started to get this error in IE11 in some pages:

To Reproduce
I found out that the global polyfills somehow break some libraries (that maybe come with their own polyfills, I don’t know). Also, not sure if NextJS 9.3 uses corejs@3.
One library that is broken (displays above error in screenshot) in NextJS 9.3 is Hubspot lib. Just add this:
<script
charSet="utf-8"
type="text/javascript"
src="//js.hsforms.net/forms/v2.js"
/>
Either in Head in _app.tsx or anywhere else (any page with or without using next/head)
Expected behavior
It should use corejs@3?
It shouldn’t break existing libs added through scripts
Additional Information
This babel config worked in NextJS 9.2 without breaking:
'next/babel',
{
'preset-env': {
useBuiltIns: 'entry',
corejs: 3,
},
},
System information
- OS: Windows
- Browser: IE11
- Version of Next.js: 9.3
Maybe related
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 8
- Comments: 18 (10 by maintainers)
Commits related to this issue
- Use core-js promise polyfill for nomodule browsers Also updated to the core-js@3 features modules instead of importing the exact modules directly. Fixes #10966 — committed to timneutkens/next.js by timneutkens 7 years ago
- Use core-js promise polyfill for nomodule browsers (#10985) * Use core-js promise polyfill for nomodule browsers Also updated to the core-js@3 features modules instead of importing the exact modules... — committed to vercel/next.js by timneutkens 7 years ago
- Use core-js promise polyfill for nomodule browsers (#10985) * Use core-js promise polyfill for nomodule browsers Also updated to the core-js@3 features modules instead of importing the exact modules... — committed to module-federation/next.js by timneutkens 7 years ago
Just adding my 2¢ here: it’s always going to be easiest to argue in favor of polyfilling the world. Even if all of core-js were to be included, that wouldn’t fix IE11’s DOM bugs or any number of other oddities. Essentially, the polyfilling threshold is always going to be arbitrary, and it’s best to recognize that and treat it like the tradeoff that it is.
Ideally, it would be great to have the baseline
nomodulepolyfill cover some known set of widely used polyfills that bring IE11 roughly up to par with the modern browsers. Then automate contextual polyfilling similar to preset-env, but with the knowledge of what APIs can already be assumed available based on the nomodule polyfill / modern browser baseline. Unfortunately, there’s no tools that currently make this possible.One other thing to note here: it may not be sufficient to just add these polyfills into the current polyfills chunk, since the bugs they fix may be present in browsers that support
<script type=module>. I would guess many of theMath.*methods fall into this category, whereas theObjectmethods only need to be shipped in the nomodule polyfill.So we traced the failure down to that hubspot script overriding
Promiseand setting it toundefinedin certain cases, but'Promise' in windowwould betrue. Replaced the polyfill on our side with the core-js one which is quite a bit larger, but it’s not a problem when it’s loaded only in older browsers.Good catch on the RegExp one, updated it here: https://github.com/zeit/next.js/pull/10985/files#diff-41195899b53fd88f258c45bb397d00b1R26
Typed arrays are not polyfilled by default as it seemed unlikely they’d be needed for the majority of applications, if you do need them you can load the polyfill only in the location that uses it.
One of the reasons I created a custom list is that core-js-compat results in too many polyfills also, eg it includes these:
Which no one would use today as they’re deprecated and discouraged: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/small
Note that the main goal of the change was to get rid of transform-runtime, which we did, and we added in a bunch of extra polyfills that were commonly needed. The main goal was not to support the exact es6 environment as in many cases eg the Math ones are never used and you should add the polyfill in the place where it’s used to ensure it’s only loaded when the code is needed.
That is to say, I’m not opposed to adding in more polyfills if there’s a real need fo them like the
Object.ones you referred to, we just started with the transform-runtime ones.@timneutkens I took some time to make a repo: https://github.com/eddyw/nextjs-9.3-ie11-bug Run:
See it crash. Rename
_next.config.jstonext.config.js(usescore-js@3/stable) Run:Works!
👍 Also experiencing this issue. After update from
9.2.2to9.3.0, my application doesn’t work on IE11. Those errors are logged in the console:@timneutkens if you want to keep it minimal, then I think NextJS should go carefully through all polyfills generated by
core-js-compatwhen target isIE 11. I agree that some of them are deprecated or discouraged but even the one in PR is missing plenty of ES6 features that change the default behavior of some built-in methods that IE11 may have.Here is another broken case that I managed to figure out why my app was breaking (Related to ImmerJS breaking):
Object.prototype.preventExtensionsisn’t polyfilled Reproduce with:In Chrome, you’ll get:
In IE11, you’ll get:
🤷♀
Quote from MDN:
Using the complete set of polyfills for IE11 works. Or add:
To sum up, some IE11 built-ins still need to have polyfills. Same thing for
Object.isSealed,Object.isFrozen,Object.isExtensiblewhich have different behavior in ES6.Then, do I need to create a case for
math? 😆 because I’m kind of sureMath.log10doesn’t work either (among all its other newest methods)So, I’ll insist on my proposal 😅 . Could the user specify OPTIONALLY the targets to for
core-js-compatto generate the polyfills entry? because the go-to solution for NextJS limitations is always modify the Webpack config but that’s not reliable as NextJS new updates may change internals or the entry name, etc etc.Depends on what you’re doing exactly, would be best to post a GitHub Discussion instead.
@timneutkens alright, off work now.
So I found out that this line causes issues: https://github.com/zeit/next.js/blob/canary/packages/next-polyfill-nomodule/src/index.js#L64
Replaced with:
And fixes one issue.
Another issue I found is again with
next-polyfill-nomodule. It doesn’t ship with enough polyfills for things such as RegExp[1] and I also don’t see typed arrays in there. It’s difficult to understand the errors thrown in IE11, so I just wrote a script to generatepolyfill-nomoduleas you guys have but usingcore-js@3andcore-js-compat:This is the script: https://github.com/eddyw/nextjs-9.3-ie11-bug/commit/788e8b205fa861a9ec250837dc24a28b23c04302#diff-8ca0d40421065a36dbd988289e8a40f5 This is the generated
polyfill-nomodulewith targetIE 11: https://github.com/eddyw/nextjs-9.3-ie11-bug/commit/788e8b205fa861a9ec250837dc24a28b23c04302#diff-f8a56ba1f0d1b020949a8a97fc1555e8As you can see it’s … bigger than the one NextJS currently uses but includes more polyfills for IE11 that NextJS doesn’t include.
Generating the polyfills for IE11 (with core-js@3) is just a couple of lines of code (as linked above to my repo):
My proposal is, could NextJS (optionally) generate polyfills for the specified targets (
polyfill-nomodule.js) if a user passes abrowserTargetsproperty (or any better name)? If a user doesn’t pass this property, then NextJS could use its minimal version of polyfills (polyfill-nomodule.js). The reason is because you just can’t cover all use cases, so IMHO, it’s not wise to assume that the minimal polyfills it ships with are enough for all apps.Note
The current code in my repo replaces the NextJS polyfills with the ones automatically generated by
core-js-compat:And it works.
Ref
es.regexp.stickynot covered among others. I don’t have a specific case right now. If you Google a bit, there are plenty of IE11 issues with NextJS regarding RegExp@timneutkens I don’t think that’s the issue 🙈 May I suggest changing this whole thing: https://github.com/zeit/next.js/blob/canary/packages/next-polyfill-nomodule/src/index.js To just:
This solved all issues. Including a
Syntax Error in regular expressionthat I just found and it’s linked to this comment https://github.com/zeit/next.js/issues/7993#issuecomment-595582989Honestly, I don’t fully understand the purpose of
next-polyfill-nomodule. Babel does this already with config:It’ll look for
browserListdefined in, for example,package.jsonand load polyfills only for those. We had0.25%, not deadwhich includes IE11, so it just worked before.Regardless, would it be possible to have an option to disable default NextJS polyfills to allow the user add their own? A custom
next.config.jsto add all stable would look like:However, this defeats the whole purpose of having
preset-envwithtargetsbecause it’ll ignorebrowserList.