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

fix: Fix hard reload when using <Link /> in Next.js 15 #1620

Merged
merged 1 commit into from
Dec 18, 2024
Merged
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
28 changes: 27 additions & 1 deletion packages/next-intl/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import preserveDirectives from 'rollup-plugin-preserve-directives';
import {getBuildConfig} from 'tools';
import pkg from './package.json' with {type: 'json'};

function rewriteBundle(regex, replaceFn) {
return {
name: 'rewrite-bundle',
generateBundle(options, bundle) {
for (const fileName of Object.keys(bundle)) {
const chunk = bundle[fileName];
const updatedCode = chunk.code.replace(regex, replaceFn);
chunk.code = updatedCode;
}
}
};
}

export default [
...getBuildConfig({
input: {
Expand Down Expand Up @@ -34,7 +47,20 @@ export default [
if (warning.code === 'MODULE_LEVEL_DIRECTIVE') return;
warn(warning);
},
plugins: [preserveDirectives()]
plugins: [
preserveDirectives(),

// Since we're writing our code with ESM, we have to import e.g. from
// `next/link.js`. While this can be used in production, since Next.js 15
// this somehow causes hard reloads when `next/link.js` is imported and
// used to link to another page. There might be some optimizations
// happening in the background that we can't control. Due to this, it
// seems safer to update imports to a version that doesn't have `.js`
// suffix and let the bundler optimize them.
rewriteBundle(/['"]next\/(\w+)\.js['"]/g, (match, p1) =>
match.replace(`next/${p1}.js`, `next/${p1}`)
)
]
}),
...getBuildConfig({
env: ['development'],
Expand Down
Loading