The pattern the example provides fails when altering to load the polyfill for Object.assign. Results in browser console error:
Uncaught Error: EventPluginRegistry: Cannot inject event plugins that do not exist in the plugin ordering, `assign`.
at invariant (react-dom.development.js:55)
at recomputePluginOrdering (react-dom.development.js:331)
at Object.injectEventPluginOrder (react-dom.development.js:442)
at eval (react-dom.development.js:5653)
at eval (react-dom.development.js:18889)
at Object../node_modules/react-dom/cjs/react-dom.development.js (dll_67211bc3619cb7902ee2.js:138)
at __webpack_require__ (dll_67211bc3619cb7902ee2.js:21)
at eval (index.js:32)
at Object../node_modules/react-dom/index.js (dll_67211bc3619cb7902ee2.js:151)
at __webpack_require__ (dll_67211bc3619cb7902ee2.js:21)
Repo of altered example https://github.com/Soundvessel/nextjs-example-polyfill-issue
polyfills.js to provide polyfill for Object assign./* eslint no-extend-native: 0 */
// core-js comes with Next.js. So, you can import it like below
import assign from 'core-js/library/fn/object/assign'
// Add your polyfills
// This files runs at the very beginning (even before React and Next.js core)
console.log('Load your polyfills')
Object.prototype.assign = assign
No console error.
polyfills.js, the only modified file from cloned with-polyfills example to confirm bug since I can't supply code of my private project http://prntscr.com/la3v4nThis issue seems to pop up a lot. We really need better documentation on how to effectively polyfill issues with imported node_modules/NPMs without throwing the entire core-js library at it or resorting to custom babel configs.
I was getting the same error when trying to add the polyfills to the prototype like Object.prototype.entries = entries. When I changed it to just the class instead Object.entries = entries the error stopped occurring and everything began working as expected. I am not sure if this would be the best practice or not, hope it helps!
Here is my polyfill.js that is working:
import entries from 'core-js/fn/object/entries';
import values from 'core-js/fn/object/values';
import arrayIncludes from 'core-js/fn/array/includes';
import includes from 'core-js/library/fn/string/includes';
import find from 'core-js/fn/array/find';
if (!Array.find) {
Array.find = find;
}
if (!Object.values) {
Object.values = values;
}
if (!Object.entries) {
Object.entries = entries;
}
if (!String.includes) {
String.includes = includes;
}
if (!Array.includes) {
Array.includes = arrayIncludes;
}
assign function is a static method of Object.
You should use Object.assign = assign instead.
@tuoxiansp you're right 馃槃
I guess you could update the example based on this issue.
Thanks @JH108 also
Most helpful comment
assignfunction is a static method of Object.You should use
Object.assign = assigninstead.