Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prefetch sourcemap generation #12346

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading