From 96e687a7b3fae9da22d3957b7e14378b72ab102e Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Wed, 18 Dec 2024 16:52:11 +0100 Subject: [PATCH] fix: Fix hard reload when using `` --- packages/next-intl/rollup.config.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/next-intl/rollup.config.js b/packages/next-intl/rollup.config.js index ad96e6512..7a2543390 100644 --- a/packages/next-intl/rollup.config.js +++ b/packages/next-intl/rollup.config.js @@ -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: { @@ -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'],