Dynamic "import" creates a race condition with initial load requests
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 142k
- Forks
- 32.4k
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 351
Description
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
This issue is not related to the precise Next version but rather to the
with-mswexample code.
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000
Binaries:
Node: 16.13.2
npm: 8.1.2
Yarn: 1.22.17
pnpm: 7.4.0
Relevant packages:
next: 12.3.4
eslint-config-next: N/A
react: 17.0.2
react-dom: 17.0.2
Which example does this report relate to?
with-msw
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
- Originally reported in https://github.com/mswjs/msw/issues/1474 (I describe what/why happens here).
I believe due to #25607, MSW example has migrated from require() to dynamic imports import() in order to tree-shake MSW from production builds. That, however, introduced a different problem:
- Since
await import()is async, and there's no top-levelawaitto wait for theinitMocks()function, there's a race condition created between the client-side code and the mocks being imported.
This race condition manifests upon initial page requests as those may be ignored by MSW since the code is not done resolving await import() when those requests happen.
Expected Behavior
- The example imports/requires MSW setup synchronously.
- Alternatively, the example ensures that the app doesn't run until MSW is imported. This import is not a costly operation, it's just a matter of asynchronicity and awaiting it.
- Side-effect: MSW must not be included in the production bundle as an indirect result of this fix/change.
To Reproduce
- See the reproduction repo in https://github.com/mswjs/msw/issues/1474
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the with-msw example and trace its initMocks() dynamic import and the initial client-side requests described in the issue. Compare the example behavior with the reproduction in the linked MSW issue. Done means the example avoids the initial-request race while keeping MSW out of the production bundle.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nextjs
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100