Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): add ie polyfill to webpack entry #13241

Closed
wants to merge 2 commits into from
Closed

Conversation

nihgwu
Copy link
Contributor

@nihgwu nihgwu commented Apr 9, 2019

Description

This PR adds polyfill for IE according to the browserlist config, the current polyfill strategy (useBuiltIns: usage) works well in most cases, but I encountered with errors only on IE in all my sites built with Gatsby, I digged a lot and found that(my guess) it's caused by the polyfill for Symbol, the useBuiltIns: usage doesn't work as React can live without Symbol so it's not polyfilled, but then it's polyfilled if the Symbol is used in other packages, so we have to add the Symbol polyfill before React bootstraps, here is my current solution for my sites https://github.com/Autodesk/react-base-table/blob/master/website/gatsby-node.js#L16

Related Issues

Fixes #7003

@nihgwu nihgwu requested a review from a team as a code owner April 9, 2019 08:11
@nihgwu nihgwu changed the title chore(gatsby): add ie polyfil to webpack entry chore(gatsby): add ie polyfill to webpack entry Apr 9, 2019
@nihgwu nihgwu requested a review from a team April 9, 2019 08:25
@DSchau DSchau added the status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response label Apr 11, 2019
@wardpeet
Copy link
Contributor

This PR looks really great! It's a bit weird that babel-polyfill isn't polyfilling. Let me dig into that first before proceeding with this PR.

@wardpeet wardpeet self-assigned this Apr 11, 2019
@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 11, 2019

I guess that's because in SSR building, React is being processed but Symbol is not polyfilled as it's optional, React will use a fallback, then processing the third-party packages, Symbol is polyfilled because of useBuiltIns=true (I included the third-party packages for babel-loader), then you deploy the SSR output, React detects mismatch of elements, so I tried to polyfill Symbol before React and it works like a . charm, and I find there are a lot of this kind of errors like reported in #7003 not only in Gatsby, but most of answers are not really helpful, someone found that add @babel/polyfill before the entry solve the problem, but I guess the polyfill for Symbol is the key as other polyfills should work with useBuiltins=true

let iePolyfill = false
if (
supportedBrowsers.includes(`ie 9`) ||
supportedBrowsers.includes(`ie 10`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are ie-mob 10 and ie-mob 11, should we also check for those versions? Personally I don't want to do that

supportedBrowsers.includes(`ie 9`) ||
supportedBrowsers.includes(`ie 10`)
) {
iePolyfill = `react-app-polyfill/ie9`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we only need to polyfill Set Map Symbol and other polyfills will be done by babel-loader, we could create our own polyfill rules instead of using react-app-polyfill, but I love the best practice from create-react-app

@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 17, 2019

@wardpeet any chance to review? the change is relative slight but would save those who are struggling with IE support, especially the Symbol problem which is hard to identify, thanks

@wardpeet
Copy link
Contributor

Hey, sorry for the late response but we rather not make this change. We are more eager to go for #7064. Which will let us babel transform all our plugins which will let babel-polyfill, polyfill all features.

At the moment there is a possible workaround by using https://www.gatsbyjs.org/packages/gatsby-plugin-compile-es6-packages/ and adding react-dom to that list.

We want you to know that we certainly want to keep you as a contributor to Gatsby.

Thanks again, and we look forward to seeing more PRs from you in the future! 💪 💜

@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 17, 2019

@wardpeet as I said, even you add those es6 packages to be compiled by babel loader, it won't solve the problem I pointed out, the Symbol polyfill issue, I did add those es6 packages in my project https://github.com/Autodesk/react-base-table/blob/master/website/gatsby-node.js#L35-L38, and it still doesn't help because of that

BTW, I contributed a lot before but was busy with my own stuffs then. I just received my swags today(I got the coupons long before, but just used them last week), they are so good 😄

@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 17, 2019

Let me explain a bit more with IE, I'm using an ES6 package react-inspector, so I have to make it transpiled by babel, and that package also uses Symbol and Symbol is polyfilled by babel, but before that no packages use Symbol directly as they fallback if Symbol is not available like var hasSymbol = typeof Symbol === 'function' && Symbol.for;, when I haven't used react-inspector, everything goes fine, as Symbol is not required, but when I navigate to the page used react-inspector the page will crash, because in this page, the Symbol is available since it's polyfilled, but React is initialized without Symbol support before that, so there are mismatches and throw Objects are not valid as a React child (found: object with keys {$$typeof, type, key, ref, props, _owner}).

@pieh
Copy link
Contributor

pieh commented Jun 10, 2019

Hey @nihgwu! Did you by any chance check if #14111 solve the problem? Ideally we could just use that and not add extra babel presets

@pieh pieh added status: awaiting author response Additional information has been requested from the author and removed status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response labels Jun 10, 2019
@nihgwu
Copy link
Contributor Author

nihgwu commented Jun 10, 2019

@pieh see my comment here #13241 (comment), that PR just make all the packages transpiled by Babel, but still doesn't solve the Symbol issue I described

@pieh
Copy link
Contributor

pieh commented Jun 10, 2019

@pieh see my comment here #13241 (comment), that PR just make all the packages transpiled by Babel, but still doesn't solve the Symbol issue I described

Hmm, I didn't try it, but if all packages would be transpiled (including React) - wouldn't @babel/preset-env pick up that it needs to polyfill Symbol and that would happen before react-inspector and then both React and react-inspector would use that polyfilled Symbol?

@wardpeet
Copy link
Contributor

#14111 seems to work for react-inspector. Somehow I can't get your example working

@wardpeet
Copy link
Contributor

I've tested #14111 and got your example to work with the babel-loader PR.

Thanks a bunch for tackling this, sadly i'll be closing as babel-loader is a better approach as it will make our live easier when moving #14289 forward.

@wardpeet wardpeet closed this Jun 14, 2019
@nihgwu
Copy link
Contributor Author

nihgwu commented Jun 14, 2019

@wardpeet Thanks so much for you for you patients

@nihgwu nihgwu deleted the neo/ie-polyfil branch June 14, 2019 14:09
@wardpeet
Copy link
Contributor

@nihgwu Fix got p in gatsby@2.11.0.

Thank you, for your patience 😉

@nihgwu
Copy link
Contributor Author

nihgwu commented Jun 27, 2019

cool, will try, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting author response Additional information has been requested from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[v2] IE11 throws Objects are not valid as a React child
4 participants