Repository navigation
Description
Astro Info
Astro v6.1.5
Vite v7.3.2
Node v22.22.2
System macOS (arm64)
Package Manager npm
Output server
Adapter @astrojs/node (v10.0.4)
Integrations @astrojs/vue (v6.0.1)
@storyblok/astro (v9.0.3)
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
Please Note, This bug was already discussed in another Issue #16258.
In Astro version 6.1.3 there was a new Vite plugin added to add assetQueryParams to asset imports (#16110). This plugin introduces a bug when adding query params to dynamic imports. The replacement will lead to improts like await import('./xxx.!~{019}~.js'?dpl=testing) where the query string is added behind the quote.
The issue lies in the following line:
| rewritten = rewritten.slice(0, imp.e) + '?' + queryString + rewritten.slice(imp.e); |
the rewriting of the code might be wrong as in es-module-lexer, for a static import like from './x.js', source.slice(imp.s, imp.e) gives the bare specifier text without quotes. But for a dynamic import like import('./x.js'), source.slice(imp.s, imp.e) gives the whole string literal, including the quotes. The README example shows source.slice(imports[3].s, imports[3].e) returning "'asdf'"
A fix for this line might be (please see this as reproducable example):
// Static imports: e points after the specifier text
// Dynamic string imports: e points after the closing quote
// on dynamic imports, imp.d will be always bigger than -1
const insertAt = imp.d > -1 ? imp.e - 1 : imp.e;
rewritten = rewritten.slice(0, insertAt) + `?${queryString}` + rewritten.slice(insertAt);What's the expected result?
Dynamic import should not break the build, when the adapter uses assetQueryParams. You should also add test cases for dynamic imports in the astro components to cover this use case.
Link to Minimal Reproducible Example
Participation
- I am willing to submit a pull request for this issue.
Activity
astrobot-houston commentedon Apr 10, 2026
I was able to reproduce this issue. The root cause is a misunderstanding of
es-module-lexer's offset behavior: for static imports likefrom './x.js',imp.epoints right after the specifier text (before the closing quote), but for dynamic imports likeimport('./x.js'),imp.epoints after the closing quote. The original code inplugin-chunk-imports.tsalways inserted the query string atimp.e, which placed?dpl=...outside the quotes for dynamic imports, producing broken output likeimport('./xxx.js'?dpl=testing)instead ofimport('./xxx.js?dpl=testing').I was able to fix this issue. The fix checks whether an import is dynamic (
imp.d > -1) and adjusts the insertion point toimp.e - 1for dynamic imports so the query string lands inside the closing quote. Test coverage has been expanded to verify both static and dynamic inter-chunk imports receive the query params correctly. All 6 tests inasset-query-params.test.jspass. View Suggested FixP4: important. This breaks production builds for any user whose adapter sets
assetQueryParams(e.g., Vercel's skew protection via?dpl=<deployment_id>) and whose client-side code uses dynamicimport()for relative JS modules. This is a common pattern for code-splitting and lazy loading, so it likely affects a significant number of users deploying with adapters that rely on asset query params.Full Triage Report
Reproduction
The bug was confirmed by code analysis. In
packages/astro/src/core/build/plugins/plugin-chunk-imports.tsonmain, therenderChunkhook always inserts the query string atimp.e:Per
es-module-lexersemantics:imp.d === -1):imp.epoints after the specifier text, before the closing quote → insertion atimp.eis correctimp.d > -1):imp.epoints after the closing quote → insertion atimp.eplaces the query string outside the quotes, breaking the importThis produces invalid JavaScript like
import('./chunk.js'?dpl=testing)instead ofimport('./chunk.js?dpl=testing').Diagnosis
de669f0a11), released in Astro v6.1.3packages/astro/src/core/build/plugins/plugin-chunk-imports.ts:54es-module-lexerFix
flue/fix-16279cccaefdf38plugin-chunk-imports.ts: Addedimp.d > -1check to useimp.e - 1for dynamic importsasset-query-params.test.js: Split test assertions into separate static and dynamic import checksDynamicLoader.astrotest fixture with a dynamicimport()of a shared moduleindex.astrofixture to include theDynamicLoadercomponentAll 6 tests pass after the fix.
This report was made by an LLM. Mistakes happen, check important info.
barry3406 commentedon Apr 10, 2026
Confirmed — this is the same
es-module-lexeroffset asymmetry that the triage bot pinpointed. For staticfrom "./x.js",imp.s..imp.ecovers the bare specifier without quotes; for dynamicimport("./x.js"), the slice includes the quotes. So when the chunk-imports plugin appends?dpl=...to a dynamic import, it landsimport("./x.js?dpl=...")instead of inside the literal.The fix is to detect dynamic imports via
imp.d > -1atplugin-chunk-imports.ts:51and rewrite the inside of the quoted string instead of appending after the slice end. I can put up a PR shortly with a regression test for both static and dynamic forms.jmurty commentedon Apr 10, 2026
I'm coming late to this issue from #16258.
I've put up PR #16282 with @leifmarcus's fix applied and a tweaked test to exercise dynamic imports. Feel free to close my PR if there's another one incoming.
And thanks both of you for your efforts on this. I'm looking forward to the improved skew protection we will soon get on Vercel deployments.
johnnydecimal commentedon Apr 15, 2026
Confirming that I run into this with my prod app. For me, Clerk's auth components were the canary - Clerk's integration injects a page-load
<script>that doesawait import('astro:transitions/client'), and that's what was tripping the chunk emit.Took me a while to realise the issue was Astro/the adapter and not Clerk, because removing Clerk made it go away. The "Browser APIs are not available on the server" hint that Astro adds to the esbuild parse error sent me down a few wrong paths - might be worth dropping that hint when the underlying error is a syntax error rather than a runtime ReferenceError.
FWIW I use Netlify not Vercel.
Will hang for the PR! Thanks all.
Add new test cases instead of changing existing ones
Add new test cases instead of changing existing ones
leifmarcus commentedon Apr 18, 2026
Fixed in Astro 6.1.8