summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2022-08-30 11:27:19 -0400
committerGravatar GitHub <noreply@github.com> 2022-08-30 11:27:19 -0400
commit69b640b87c5d0f346129cd0cbd23efaf366bc8b1 (patch)
tree4d6e1357e50e76e6f1ed626236e3164c646a99f4
parent4dad059318e41579647361f3d0387fdaa656c19e (diff)
downloadastro-69b640b87c5d0f346129cd0cbd23efaf366bc8b1.tar.gz
astro-69b640b87c5d0f346129cd0cbd23efaf366bc8b1.tar.zst
astro-69b640b87c5d0f346129cd0cbd23efaf366bc8b1.zip
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 883710d21c5193677e2358b76549489b5f33b946. * Revert "wip: try removing ssrLoadModule on styles" This reverts commit 880e73d94a42b1476fa3a2e611fc5017a3b4bdbd. * SAD fix: try/catch on ssrLoadModule * refactor: isFile -> isRootFile * docs: update comments for new findings
-rw-r--r--.changeset/small-jobs-camp.md5
-rw-r--r--packages/astro/src/core/render/dev/vite.ts21
2 files changed, 17 insertions, 9 deletions
diff --git a/.changeset/small-jobs-camp.md b/.changeset/small-jobs-camp.md
new file mode 100644
index 000000000..aa2d6e910
--- /dev/null
+++ b/.changeset/small-jobs-camp.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix "failed to load for SSR" on styles when using tailwind
diff --git a/packages/astro/src/core/render/dev/vite.ts b/packages/astro/src/core/render/dev/vite.ts
index 3412f2398..479b3b99b 100644
--- a/packages/astro/src/core/render/dev/vite.ts
+++ b/packages/astro/src/core/render/dev/vite.ts
@@ -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. */
+ }
}
}
}