Skip to content

Dynamic imports are broken when using assetQueryParams in adapter #16279

Closed

Description

@leifmarcus

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

#16258 (comment)

Participation

  • I am willing to submit a pull request for this issue.

Activity

  1. astrobot-houston commented on Apr 10, 2026

    @astrobot-houston
    Contributor

    I was able to reproduce this issue. The root cause is a misunderstanding of es-module-lexer's offset behavior: for static imports like from './x.js', imp.e points right after the specifier text (before the closing quote), but for dynamic imports like import('./x.js'), imp.e points after the closing quote. The original code in plugin-chunk-imports.ts always inserted the query string at imp.e, which placed ?dpl=... outside the quotes for dynamic imports, producing broken output like import('./xxx.js'?dpl=testing) instead of import('./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 to imp.e - 1 for 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 in asset-query-params.test.js pass. View Suggested Fix

    P4: 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 dynamic import() 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.ts on main, the renderChunk hook always inserts the query string at imp.e:

    rewritten = rewritten.slice(0, imp.e) + '?' + queryString + rewritten.slice(imp.e);

    Per es-module-lexer semantics:

    • Static imports (imp.d === -1): imp.e points after the specifier text, before the closing quote → insertion at imp.e is correct
    • Dynamic imports (imp.d > -1): imp.e points after the closing quote → insertion at imp.e places the query string outside the quotes, breaking the import

    This produces invalid JavaScript like import('./chunk.js'?dpl=testing) instead of import('./chunk.js?dpl=testing').

    Diagnosis

    Fix

    • Branch: flue/fix-16279
    • Commit: cccaefdf38
    • Changes:
      1. plugin-chunk-imports.ts: Added imp.d > -1 check to use imp.e - 1 for dynamic imports
      2. asset-query-params.test.js: Split test assertions into separate static and dynamic import checks
      3. Added DynamicLoader.astro test fixture with a dynamic import() of a shared module
      4. Updated index.astro fixture to include the DynamicLoader component

    All 6 tests pass after the fix.

    This report was made by an LLM. Mistakes happen, check important info.

  2. added
    - P4: important
    pkg: astro
    and removed
    triage: needs triage
    on Apr 10, 2026
  3. barry3406 commented on Apr 10, 2026

    @barry3406
    Contributor

    Confirmed — this is the same es-module-lexer offset asymmetry that the triage bot pinpointed. For static from "./x.js", imp.s..imp.e covers the bare specifier without quotes; for dynamic import("./x.js"), the slice includes the quotes. So when the chunk-imports plugin appends ?dpl=... to a dynamic import, it lands import("./x.js?dpl=...") instead of inside the literal.

    The fix is to detect dynamic imports via imp.d > -1 at plugin-chunk-imports.ts:51 and 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.

  4. jmurty commented on Apr 10, 2026

    @jmurty
    Contributor

    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.

  5. johnnydecimal commented on Apr 15, 2026

    @johnnydecimal

    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 does await 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.

  6. leifmarcus commented on Apr 18, 2026

    @leifmarcus
    ContributorAuthor

    Fixed in Astro 6.1.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P4: importantpkg: astro

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions