Fix "failed to load for SSR" error when removing Astro <style>
blocks (#4548)
* wip: try removing ssrLoadModule on styles * chore: changeset * fix: invalidate mod before crawling graph * Revert "fix: invalidate mod before crawling graph" This reverts commit883710d21c
. * Revert "wip: try removing ssrLoadModule on styles" This reverts commit880e73d94a
. * SAD fix: try/catch on ssrLoadModule * refactor: isFile -> isRootFile * docs: update comments for new findings
This commit is contained in:
parent
4dad059318
commit
69b640b87c
2 changed files with 17 additions and 9 deletions
5
.changeset/small-jobs-camp.md
Normal file
5
.changeset/small-jobs-camp.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix "failed to load for SSR" on styles when using tailwind
|
|
@ -15,19 +15,18 @@ const STRIP_QUERY_PARAMS_REGEX = /\?.*$/;
|
||||||
export async function* crawlGraph(
|
export async function* crawlGraph(
|
||||||
viteServer: vite.ViteDevServer,
|
viteServer: vite.ViteDevServer,
|
||||||
_id: string,
|
_id: string,
|
||||||
isFile: boolean,
|
isRootFile: boolean,
|
||||||
scanned = new Set<string>()
|
scanned = new Set<string>()
|
||||||
): AsyncGenerator<vite.ModuleNode, void, unknown> {
|
): AsyncGenerator<vite.ModuleNode, void, unknown> {
|
||||||
const id = unwrapId(_id);
|
const id = unwrapId(_id);
|
||||||
const importedModules = new Set<vite.ModuleNode>();
|
const importedModules = new Set<vite.ModuleNode>();
|
||||||
const moduleEntriesForId = isFile
|
const moduleEntriesForId = isRootFile
|
||||||
? // If isFile = true, then you are at the root of your module import tree.
|
? // "getModulesByFile" pulls from a delayed module cache (fun implementation detail),
|
||||||
// The `id` arg is a filepath, so use `getModulesByFile()` to collect all
|
// So we can get up-to-date info on initial server load.
|
||||||
// nodes for that file. This is needed for advanced imports like Tailwind.
|
// Needed for slower CSS preprocessing like Tailwind
|
||||||
viteServer.moduleGraph.getModulesByFile(id) ?? new Set()
|
viteServer.moduleGraph.getModulesByFile(id) ?? new Set()
|
||||||
: // Otherwise, you are following an import in the module import tree.
|
: // For non-root files, we're safe to pull from "getModuleById" based on testing.
|
||||||
// You are safe to use getModuleById() here because Vite has already
|
// TODO: Find better invalidation strat to use "getModuleById" in all cases!
|
||||||
// resolved the correct `id` for you, by creating the import you followed here.
|
|
||||||
new Set([viteServer.moduleGraph.getModuleById(id)]);
|
new Set([viteServer.moduleGraph.getModuleById(id)]);
|
||||||
|
|
||||||
// Collect all imported modules for the module(s).
|
// Collect all imported modules for the module(s).
|
||||||
|
@ -59,7 +58,11 @@ export async function* crawlGraph(
|
||||||
if (fileExtensionsToSSR.has(npath.extname(importedModulePathname))) {
|
if (fileExtensionsToSSR.has(npath.extname(importedModulePathname))) {
|
||||||
const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
|
const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
|
||||||
if (!mod?.ssrModule) {
|
if (!mod?.ssrModule) {
|
||||||
await viteServer.ssrLoadModule(importedModule.id);
|
try {
|
||||||
|
await viteServer.ssrLoadModule(importedModule.id);
|
||||||
|
} catch {
|
||||||
|
/** Likely an out-of-date module entry! Silently continue. */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue