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 commit 883710d21c.

* Revert "wip: try removing ssrLoadModule on styles"

This reverts commit 880e73d94a.

* SAD fix: try/catch on ssrLoadModule

* refactor: isFile -> isRootFile

* docs: update comments for new findings
This commit is contained in:
Ben Holmes 2022-08-30 11:27:19 -04:00 committed by GitHub
parent 4dad059318
commit 69b640b87c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix "failed to load for SSR" on styles when using tailwind

View file

@ -15,19 +15,18 @@ const STRIP_QUERY_PARAMS_REGEX = /\?.*$/;
export async function* crawlGraph(
viteServer: vite.ViteDevServer,
_id: string,
isFile: boolean,
isRootFile: boolean,
scanned = new Set<string>()
): AsyncGenerator<vite.ModuleNode, void, unknown> {
const id = unwrapId(_id);
const importedModules = new Set<vite.ModuleNode>();
const moduleEntriesForId = isFile
? // If isFile = true, then you are at the root of your module import tree.
// The `id` arg is a filepath, so use `getModulesByFile()` to collect all
// nodes for that file. This is needed for advanced imports like Tailwind.
const moduleEntriesForId = isRootFile
? // "getModulesByFile" pulls from a delayed module cache (fun implementation detail),
// So we can get up-to-date info on initial server load.
// Needed for slower CSS preprocessing like Tailwind
viteServer.moduleGraph.getModulesByFile(id) ?? new Set()
: // Otherwise, you are following an import in the module import tree.
// You are safe to use getModuleById() here because Vite has already
// resolved the correct `id` for you, by creating the import you followed here.
: // For non-root files, we're safe to pull from "getModuleById" based on testing.
// TODO: Find better invalidation strat to use "getModuleById" in all cases!
new Set([viteServer.moduleGraph.getModuleById(id)]);
// Collect all imported modules for the module(s).
@ -59,7 +58,11 @@ export async function* crawlGraph(
if (fileExtensionsToSSR.has(npath.extname(importedModulePathname))) {
const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
if (!mod?.ssrModule) {
await viteServer.ssrLoadModule(importedModule.id);
try {
await viteServer.ssrLoadModule(importedModule.id);
} catch {
/** Likely an out-of-date module entry! Silently continue. */
}
}
}
}