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

suggest using dynamic import instead of require #228

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/getting-started/integrate/browser.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ import App from './App'
if (process.env.NODE_ENV === 'development') {
const { worker } = require('./mocks/browser')
worker.start()
/*
// or you can do this instead
Copy link
Member

Choose a reason for hiding this comment

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

The problem is that dynamic import works in a different way than require does, so they are not interchangeable in any regard.

  • require is not statically analyzable so bundlers cannot tree-shake its calls / import can be tree-shaken.
  • require is sync; import is async and must be awaited.

Those are very distinct differences and they directly affect how you should treat setups that utilize either method.

On a related note, we're discussing an issue in Next where the usage of dynamic import results in a race condition between the worker activation and requests on the initial load (see vercel/next.js#43284).

;(async () => {
const { worker } = await import('./mocks/browser')
worker.start()
})()
*/
}

ReactDOM.render(<App />, document.getElementById('root'))
Expand Down