Skip to content

Commit

Permalink
fix: Fix hard reload when using <Link /> in Next.js 15 (#1620)
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn authored Dec 18, 2024
1 parent 8b9a2fc commit 7e7011d
Showing 1 changed file with 27 additions and 1 deletion.
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

0 comments on commit 7e7011d

Please sign in to comment.