Skip to content

Instantly share code, notes, and snippets.

@threepointone
Last active January 13, 2022 10:10
Show Gist options
  • Save threepointone/d01eabcdc2485c234e9303b8424df556 to your computer and use it in GitHub Desktop.
Save threepointone/d01eabcdc2485c234e9303b8424df556 to your computer and use it in GitHub Desktop.
fix for ssr + dynamic imports with webpack
function ensure(moduleId, fn, done){
if(__webpack_modules__[moduleId]) { //eslint-disable-line no-undef
return done(undefined, __webpack_require__(moduleId)) //eslint-disable-line no-undef
}
fn().then(Module => done(undefined, Module), done)
}
// usage
ensure(require.resolveWeak('./x.js'),
async () => import('./x.js'),
// or use require.ensure for predictable chunk names
// () => require.ensure([], require => require('./x.js'), chunk)
(err, Module) => {
// this could be possibly sync
// if you've loaded your chunks in the correct order
// so rehydrating with react will be fine
// ... do as you will!
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment