vercel / vercel/next.js

Dynamic "import" creates a race condition with initial load requests

Open
#43,284 17 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

examples not stale Upstream
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-msw example 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

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-level await to wait for the initMocks() 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
  1. The example imports/requires MSW setup synchronously.
  2. 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.
  3. Side-effect: MSW must not be included in the production bundle as an indirect result of this fix/change.
To Reproduce

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.