-
Notifications
You must be signed in to change notification settings - Fork 28k
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
Using next/link.js
in an ESM app causes a hard reload / full ESM support via subpath exports for next
#77200
Comments
@amannn Thank you for submitting an issue!
Can you clarify why you need to add an explicit file extension when importing Link? |
@samcx Thank you for the quick reply! So the issue is that the ESM spec requires mandatory file extensions, e.g.: // ❌ Invalid
import Link from 'next/link';
// ✅ Valid
import Link from 'next/link.js'; To make authoring code easier, bundlers like Turbopack, webpack and to some extend Vite(st) typically allow to leave out these file extensions by (pre-)bundling source code. However, under some circumstances, this will still cause issues. In the example of Vitest, only first-party user code is pre-bundled and not any 3rd party dependencies. Due to this, if you have a package that imports from Next.js, this will cause errors when used:
I've updated the reproduction in So as it stands currently, a 3rd party dependency can either:
I think this could be addressed in two ways on the Next.js side:
{
// ...
"exports": {
"./link": "./link.js",
"./navigation": "./navigation.js",
// ...
}
} (2) might be preferable since it would work well for the imports that Next.js has popularized in its docs, but it'd be a breaking change since it would make the public API of // Would break when you add `exports`
import { ResponseCookies } from 'next/dist/compiled/@edge-runtime/cookies'; I guess Next.js wants to discourage importing private APIs anyway, but practically some users might use such imports today. The above tries to outline the issue from a minimal example. Practically, this currently affects libraries like /cc @franky47 Maybe you've also encountered similar errors with users of nuqs since you're importing from Next.js into an ESM package? /cc @karlhorky If I'm not mistaken, I saw your name once in a discussion about full ESM support in Next.js, but I can't find the relevant issue right now. Maybe you have other insights? |
next/link.js
in an ESM app causes a hard reloadnext/link.js
in an ESM app causes a hard reload / full ESM support via subpath exports for next
Thanks for the ping @amannn, I just had this issue come up again in 47ng/nuqs#869, but with nuqs is indeed an ESM package, and we provide adapters for:
I'll try and reproduce your hard reload issue to see if it impacts |
Link to the code that reproduces this issue
amannn/nextjs-bug-repro-linkjs@eedf0a9
To Reproduce
pnpm i
pnpm run dev
/test
Current vs. Expected behavior
Currently, the target page is opened with a hard reload. I'd expect it to use soft navigation—same as when using
next/link
without the explicit file extension.Provide environment information
Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:00 PDT 2024; root:xnu-10063.141.2~1/RELEASE_X86_64 Available memory (MB): 16384 Available CPU cores: 12 Binaries: Node: 20.11.1 npm: 10.2.4 Yarn: 1.22.22 pnpm: 9.15.4 Relevant Packages: next: 15.3.0-canary.11 // Latest available version is detected (15.3.0-canary.11). eslint-config-next: N/A react: 19.0.0 react-dom: 19.0.0 typescript: 5.8.2 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Linking and Navigating
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
amannn/next-intl#1796 (comment)
https://next-intl.dev/docs/environments/testing#vitest
The text was updated successfully, but these errors were encountered: