Skip to content

Commit

Permalink
Fix prefetch sourcemap generation (#12346)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Nov 1, 2024
1 parent 836cd91 commit 20e5a84
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-walls-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes sourcemap generation when prefetch is enabled
22 changes: 16 additions & 6 deletions packages/astro/src/prefetch/vite-plugin-prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,25 @@ export default function astroPrefetch({ settings }: { settings: AstroSettings })
},
transform(code, id) {
// NOTE: Handle replacing the specifiers even if prefetch is disabled so View Transitions
// can import the internal module as not hit runtime issues.
// can import the internal module and not hit runtime issues.
if (id.includes(prefetchInternalModuleFsSubpath)) {
return code
.replace('__PREFETCH_PREFETCH_ALL__', JSON.stringify(prefetch?.prefetchAll))
.replace('__PREFETCH_DEFAULT_STRATEGY__', JSON.stringify(prefetch?.defaultStrategy))
// We perform a simple replacement with padding so that the code offset is not changed and
// we don't have to generate a sourcemap. This has the assumption that the replaced string
// will always be shorter than the search string to work.
code = code
.replace(
'__EXPERIMENTAL_CLIENT_PRERENDER__',
JSON.stringify(settings.config.experimental.clientPrerender),
'__PREFETCH_PREFETCH_ALL__', // length: 25
`${JSON.stringify(prefetch?.prefetchAll)}`.padEnd(25),
)
.replace(
'__PREFETCH_DEFAULT_STRATEGY__', // length: 29
`${JSON.stringify(prefetch?.defaultStrategy)}`.padEnd(29),
)
.replace(
'__EXPERIMENTAL_CLIENT_PRERENDER__', // length: 33
`${JSON.stringify(settings.config.experimental.clientPrerender)}`.padEnd(33),
);
return { code, map: null };
}
},
};
Expand Down

0 comments on commit 20e5a84

Please sign in to comment.