diff --git a/.changeset/twelve-sloths-kiss.md b/.changeset/twelve-sloths-kiss.md new file mode 100644 index 000000000000..c0439a5020ae --- /dev/null +++ b/.changeset/twelve-sloths-kiss.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes build errors on platforms with skew protection enabled (e.g. Vercel, Netlify) for inter-chunk Javascript using dynamic imports diff --git a/packages/astro/src/core/build/plugins/plugin-chunk-imports.ts b/packages/astro/src/core/build/plugins/plugin-chunk-imports.ts index 84a2fdb7b286..5fed5d1e20e9 100644 --- a/packages/astro/src/core/build/plugins/plugin-chunk-imports.ts +++ b/packages/astro/src/core/build/plugins/plugin-chunk-imports.ts @@ -47,8 +47,11 @@ export function pluginChunkImports(options: StaticBuildOptions): VitePlugin | un let rewritten = code; for (let i = relativeImports.length - 1; i >= 0; i--) { const imp = relativeImports[i]; - // imp.s and imp.e are the start/end offsets of the module specifier (without quotes) - rewritten = rewritten.slice(0, imp.e) + '?' + queryString + rewritten.slice(imp.e); + // imp.s and imp.e are the start/end offsets of the module specifier. + // Static imports: e points after the specifier text, before the quote. + // Dynamic string imports: e points after the closing quote. + const insertAt = imp.d > -1 ? imp.e - 1 : imp.e; + rewritten = rewritten.slice(0, insertAt) + '?' + queryString + rewritten.slice(insertAt); } return { code: rewritten, map: null }; diff --git a/packages/astro/test/asset-query-params.test.js b/packages/astro/test/asset-query-params.test.js index 641c21c7b750..d197e2c2bfd6 100644 --- a/packages/astro/test/asset-query-params.test.js +++ b/packages/astro/test/asset-query-params.test.js @@ -173,29 +173,46 @@ describe('Asset Query Parameters in Inter-Chunk JS Imports', () => { const scripts = $('script[src]'); assert.ok(scripts.length > 0, 'Should have at least one external script'); - let foundRelativeImport = false; + let foundStaticImport = false; + let foundDynamicImport = false; // Read all client JS files and check inter-chunk imports have query params const jsFiles = await fixture.glob('client/**/*.js'); for (const file of jsFiles) { const code = await fixture.readFile(`/${file}`); - // Match relative imports: from"./chunk.js", from "./chunk.js", import("./chunk.js") - const allImports = [ + // Match static imports: from "./chunk.js", from "./chunk.js" + const staticImports = [ ...code.matchAll(/(from\s*["'])(\.\.?\/[^"']+\.(?:js|mjs)(?:\?[^"']*)?)(["'])/g), + ]; + // Match dynamic imports: import("./chunk.js") + const dynamicImports = [ ...code.matchAll(/(import\s*\(\s*["'])(\.\.?\/[^"']+\.(?:js|mjs)(?:\?[^"']*)?)(["'])/g), ]; - for (const match of allImports) { - foundRelativeImport = true; + for (const match of staticImports) { + foundStaticImport = true; const importPath = match[2]; assert.match( importPath, /\?dpl=test-deploy-id/, - `Inter-chunk import should include assetQueryParams: ${match[0]}`, + `Static inter-chunk import should include assetQueryParams: ${match[0]}`, + ); + } + for (const match of dynamicImports) { + foundDynamicImport = true; + const importPath = match[2]; + assert.match( + importPath, + /\?dpl=test-deploy-id/, + `Dynamic inter-chunk import should include assetQueryParams: ${match[0]}`, ); } } assert.ok( - foundRelativeImport, - 'Expected at least one relative inter-chunk import in client JS files', + foundStaticImport, + 'Expected at least one static relative inter-chunk import in client JS files', + ); + assert.ok( + foundDynamicImport, + 'Expected at least one dynamic relative inter-chunk import in client JS files', ); }); }); diff --git a/packages/astro/test/fixtures/asset-query-params-chunks/src/components/DynamicLoader.astro b/packages/astro/test/fixtures/asset-query-params-chunks/src/components/DynamicLoader.astro new file mode 100644 index 000000000000..c2643ede351d --- /dev/null +++ b/packages/astro/test/fixtures/asset-query-params-chunks/src/components/DynamicLoader.astro @@ -0,0 +1,6 @@ +
Dynamic Loader
+ diff --git a/packages/astro/test/fixtures/asset-query-params-chunks/src/pages/index.astro b/packages/astro/test/fixtures/asset-query-params-chunks/src/pages/index.astro index 53c0d49f70c0..a7f6c2dd2abf 100644 --- a/packages/astro/test/fixtures/asset-query-params-chunks/src/pages/index.astro +++ b/packages/astro/test/fixtures/asset-query-params-chunks/src/pages/index.astro @@ -1,11 +1,13 @@ --- import CounterA from '../components/CounterA.astro'; import CounterB from '../components/CounterB.astro'; +import DynamicLoader from '../components/DynamicLoader.astro'; --- Chunk Imports Test +