From 2acf72d512a2e7b11798051fb663feaa040e515b Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 1 Dec 2023 23:38:22 +0100 Subject: [PATCH] fix: tailwind failing on non-existant files (#2118) --- plugins/tailwind.ts | 10 ++++++++-- src/server/config.ts | 2 +- tests/fixture_tailwind/fresh.gen.ts | 2 ++ tests/fixture_tailwind/routes/_middleware.ts | 15 +++++++++++++++ tests/tailwind_test.ts | 12 ++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 tests/fixture_tailwind/routes/_middleware.ts diff --git a/plugins/tailwind.ts b/plugins/tailwind.ts index 0c432666..86fa9f68 100644 --- a/plugins/tailwind.ts +++ b/plugins/tailwind.ts @@ -82,8 +82,8 @@ export default function tailwind(): Plugin { const tailwindMiddleware: PluginMiddleware = { path: "/", middleware: { - handler: async (req, ctx) => { - const pathname = new URL(req.url).pathname; + handler: async (_req, ctx) => { + const pathname = ctx.url.pathname; if (pathname.endsWith(".css.map")) { const cached = cache.get(pathname); @@ -110,6 +110,12 @@ export default function tailwind(): Plugin { }; cache.set(pathname, cached); } catch (err) { + // If the file is not found than it's likely a virtual file + // by the user that they respond to via a middleware. + if (err instanceof Deno.errors.NotFound) { + return ctx.next(); + } + cached = { content: text, map: "", diff --git a/src/server/config.ts b/src/server/config.ts index 2ad7f316..803a538b 100644 --- a/src/server/config.ts +++ b/src/server/config.ts @@ -124,7 +124,7 @@ export async function getInternalFreshState( return { config: internalConfig, manifest, - loadSnapshot: !isLegacyDev, + loadSnapshot: !isLegacyDev && !config.dev, denoJsonPath, denoJson, build: false, diff --git a/tests/fixture_tailwind/fresh.gen.ts b/tests/fixture_tailwind/fresh.gen.ts index e6643ad2..2e895a65 100644 --- a/tests/fixture_tailwind/fresh.gen.ts +++ b/tests/fixture_tailwind/fresh.gen.ts @@ -3,6 +3,7 @@ // This file is automatically updated during development when running `dev.ts`. import * as $_app from "./routes/_app.tsx"; +import * as $_middleware from "./routes/_middleware.ts"; import * as $index from "./routes/index.tsx"; import { type Manifest } from "$fresh/server.ts"; @@ -10,6 +11,7 @@ import { type Manifest } from "$fresh/server.ts"; const manifest = { routes: { "./routes/_app.tsx": $_app, + "./routes/_middleware.ts": $_middleware, "./routes/index.tsx": $index, }, islands: {}, diff --git a/tests/fixture_tailwind/routes/_middleware.ts b/tests/fixture_tailwind/routes/_middleware.ts new file mode 100644 index 00000000..362a96be --- /dev/null +++ b/tests/fixture_tailwind/routes/_middleware.ts @@ -0,0 +1,15 @@ +import { FreshContext } from "$fresh/server.ts"; + +export async function handler( + _req: Request, + ctx: FreshContext, +) { + if (ctx.url.pathname === "/middleware-only.css") { + return new Response(".foo-bar { color: red }", { + headers: { + "Content-Type": "text/css", + }, + }); + } + return await ctx.next(); +} diff --git a/tests/tailwind_test.ts b/tests/tailwind_test.ts index f7740733..76f6ceb2 100644 --- a/tests/tailwind_test.ts +++ b/tests/tailwind_test.ts @@ -37,3 +37,15 @@ Deno.test("TailwindCSS - config", async () => { { loadConfig: true }, ); }); + +Deno.test("TailwindCSS - middleware only css", async () => { + await withFakeServe( + "./tests/fixture_tailwind/dev.ts", + async (server) => { + const res = await server.get("/middleware-only.css"); + const content = await res.text(); + assertStringIncludes(content, ".foo-bar"); + }, + { loadConfig: true }, + ); +});