Skip to content

Commit 7e7011d

Browse files
authored
fix: Fix hard reload when using <Link /> in Next.js 15 (#1620)
1 parent 8b9a2fc commit 7e7011d

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

packages/next-intl/rollup.config.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import preserveDirectives from 'rollup-plugin-preserve-directives';
22
import {getBuildConfig} from 'tools';
33
import pkg from './package.json' with {type: 'json'};
44

5+
function rewriteBundle(regex, replaceFn) {
6+
return {
7+
name: 'rewrite-bundle',
8+
generateBundle(options, bundle) {
9+
for (const fileName of Object.keys(bundle)) {
10+
const chunk = bundle[fileName];
11+
const updatedCode = chunk.code.replace(regex, replaceFn);
12+
chunk.code = updatedCode;
13+
}
14+
}
15+
};
16+
}
17+
518
export default [
619
...getBuildConfig({
720
input: {
@@ -34,7 +47,20 @@ export default [
3447
if (warning.code === 'MODULE_LEVEL_DIRECTIVE') return;
3548
warn(warning);
3649
},
37-
plugins: [preserveDirectives()]
50+
plugins: [
51+
preserveDirectives(),
52+
53+
// Since we're writing our code with ESM, we have to import e.g. from
54+
// `next/link.js`. While this can be used in production, since Next.js 15
55+
// this somehow causes hard reloads when `next/link.js` is imported and
56+
// used to link to another page. There might be some optimizations
57+
// happening in the background that we can't control. Due to this, it
58+
// seems safer to update imports to a version that doesn't have `.js`
59+
// suffix and let the bundler optimize them.
60+
rewriteBundle(/['"]next\/(\w+)\.js['"]/g, (match, p1) =>
61+
match.replace(`next/${p1}.js`, `next/${p1}`)
62+
)
63+
]
3864
}),
3965
...getBuildConfig({
4066
env: ['development'],

0 commit comments

Comments
 (0)