diff --git a/plugins/tailwind.ts b/plugins/tailwind.ts
index 7359bd25..b0e043b0 100644
--- a/plugins/tailwind.ts
+++ b/plugins/tailwind.ts
@@ -32,7 +32,10 @@ export default function tailwind(): Plugin {
let cached = cache.get(pathname);
if (!cached) {
- const filePath = path.join(staticDir, pathname);
+ const filePath = path.join(
+ staticDir,
+ pathname.replace(ctx.config.basePath, ""),
+ );
let text = "";
try {
text = await Deno.readTextFile(filePath);
diff --git a/src/server/compose.ts b/src/server/compose.ts
index dee8c307..82b8df3d 100644
--- a/src/server/compose.ts
+++ b/src/server/compose.ts
@@ -1,5 +1,5 @@
import { SEP } from "./deps.ts";
-import { ErrorHandler, FinalHandler, RouteResult } from "./router.ts";
+import { ErrorHandler, FinalHandler, RouteResult, withBase } from "./router.ts";
import {
BaseRoute,
FreshContext,
@@ -59,6 +59,7 @@ export function composeMiddlewares(
url: string,
) => RouteResult,
renderNotFound: UnknownRenderFunction,
+ basePath: string,
) {
return (
req: Request,
@@ -72,7 +73,8 @@ export function composeMiddlewares(
// identify middlewares to apply, if any.
// middlewares should be already sorted from deepest to shallow layer
const mws = selectSharedRoutes(
- paramsAndRouteResult.route?.baseRoute ?? ROOT_BASE_ROUTE,
+ paramsAndRouteResult.route?.baseRoute ??
+ toBaseRoute(withBase(ROOT_BASE_ROUTE, basePath)),
middlewares,
);
diff --git a/src/server/context.ts b/src/server/context.ts
index 39311e93..dcce8c3b 100644
--- a/src/server/context.ts
+++ b/src/server/context.ts
@@ -166,6 +166,7 @@ export class ServerContext {
handlers.errorHandler,
router.getParamsAndRoute(handlers),
renderNotFound,
+ basePath,
);
const trailingSlashEnabled = this.#state.config.router?.trailingSlash;
const isDev = this.#dev;
diff --git a/tests/base_path_test.ts b/tests/base_path_test.ts
index 0e97e5ba..8cc93f1c 100644
--- a/tests/base_path_test.ts
+++ b/tests/base_path_test.ts
@@ -2,6 +2,7 @@ import {
assert,
assertEquals,
assertMatch,
+ assertStringIncludes,
delay,
puppeteer,
STATUS_CODE,
@@ -9,6 +10,7 @@ import {
import {
clickWhenListenerReady,
getErrorOverlay,
+ runBuild,
waitForText,
withFakeServe,
withFresh,
@@ -42,7 +44,7 @@ Deno.test("rewrites middleware request", async () => {
await withFresh("./tests/fixture_base_path/main.ts", async (address) => {
const res = await fetch(`${address}/api`);
const body = await res.text();
- assertEquals(body, "it works");
+ assertEquals(body, "middleware is working");
});
});
@@ -168,3 +170,80 @@ Deno.test("dev_command config: shows codeframe", async () => {
},
);
});
+
+Deno.test("TailwindCSS - dev mode", async () => {
+ await withFakeServe("./tests/fixture_base_path/dev.ts", async (server) => {
+ const res = await server.get("/styles.css");
+ const content = await res.text();
+ assertStringIncludes(content, ".text-red-600");
+
+ const res2 = await server.get("/styles.css?foo=bar");
+ const content2 = await res2.text();
+ assert(!content2.includes("@tailwind"));
+ }, { loadConfig: true });
+});
+
+Deno.test("middleware test", async (t) => {
+ await withFakeServe(
+ "./tests/fixture_base_path/dev.ts",
+ async (server) => {
+ await t.step("expected root", async () => {
+ const res = await server.get("/foo/bar");
+ const content = await res.text();
+ assertEquals(res.headers.get("server"), "fresh server");
+ assertStringIncludes(content, "middleware is working");
+ });
+
+ await t.step("redirect root", async () => {
+ const res = await server.get("");
+ const content = await res.text();
+ assertEquals(res.headers.get("server"), "fresh server");
+ assertStringIncludes(content, "middleware is working");
+ });
+
+ await t.step("miiddleware before an invalid route", async () => {
+ const res = await server.get("/asdfasdfasdfasdfasdfasdf");
+ assertEquals(res.headers.get("server"), "fresh server");
+ await res.body?.cancel();
+ });
+ },
+ { loadConfig: true },
+ );
+});
+
+Deno.test("TailwindCSS - build mode", async () => {
+ await runBuild("./tests/fixture_base_path_build/dev.ts");
+ await withFakeServe(
+ "./tests/fixture_base_path_build/main.ts",
+ async (server) => {
+ const res = await server.get("/styles.css");
+ const content = await res.text();
+ assertStringIncludes(content, ".text-red-600{");
+ },
+ { loadConfig: true },
+ );
+});
+
+Deno.test("TailwindCSS - config", async () => {
+ await withFakeServe(
+ "./tests/fixture_base_path_config/dev.ts",
+ async (server) => {
+ const res = await server.get("/styles.css");
+ const content = await res.text();
+ assertStringIncludes(content, ".text-pp");
+ },
+ { loadConfig: true },
+ );
+});
+
+Deno.test("TailwindCSS - middleware only css", async () => {
+ await withFakeServe(
+ "./tests/fixture_base_path/dev.ts",
+ async (server) => {
+ const res = await server.get("/middleware-only.css");
+ const content = await res.text();
+ assertStringIncludes(content, ".foo-bar");
+ },
+ { loadConfig: true },
+ );
+});
diff --git a/tests/fixture_base_path/deno.json b/tests/fixture_base_path/deno.json
index ad956c3f..7802f6a0 100644
--- a/tests/fixture_base_path/deno.json
+++ b/tests/fixture_base_path/deno.json
@@ -9,7 +9,10 @@
"preact/": "https://esm.sh/preact@10.15.1/",
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.5.0",
- "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0"
+ "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
+ "tailwindcss": "npm:tailwindcss@3.3.5",
+ "tailwindcss/": "npm:/tailwindcss@3.3.5/",
+ "tailwindcss/plugin": "npm:/tailwindcss@3.3.5/plugin.js"
},
"compilerOptions": {
"jsx": "react-jsx",
diff --git a/tests/fixture_base_path/fresh.config.ts b/tests/fixture_base_path/fresh.config.ts
new file mode 100644
index 00000000..8668bf3c
--- /dev/null
+++ b/tests/fixture_base_path/fresh.config.ts
@@ -0,0 +1,9 @@
+import { defineConfig } from "$fresh/server.ts";
+import tailwind from "$fresh/plugins/tailwind.ts";
+
+export default defineConfig({
+ router: { basePath: "/foo/bar" },
+ plugins: [
+ tailwind(),
+ ],
+});
diff --git a/tests/fixture_base_path/main.ts b/tests/fixture_base_path/main.ts
index 7a3bb668..fc935921 100644
--- a/tests/fixture_base_path/main.ts
+++ b/tests/fixture_base_path/main.ts
@@ -6,5 +6,6 @@
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
+import config from "./fresh.config.ts";
-await start(manifest, { router: { basePath: "/foo/bar" } });
+await start(manifest, config);
diff --git a/tests/fixture_base_path/routes/_middleware.ts b/tests/fixture_base_path/routes/_middleware.ts
index e81dfc14..5870f432 100644
--- a/tests/fixture_base_path/routes/_middleware.ts
+++ b/tests/fixture_base_path/routes/_middleware.ts
@@ -4,7 +4,14 @@ export async function handler(
_req: Request,
ctx: FreshContext<{ data: string }>,
) {
- ctx.state.data = "it works";
+ if (ctx.url.pathname === "/foo/bar/middleware-only.css") {
+ return new Response(".foo-bar { color: red }", {
+ headers: {
+ "Content-Type": "text/css",
+ },
+ });
+ }
+ ctx.state.data = "middleware is working";
const resp = await ctx.next();
resp.headers.set("server", "fresh server");
return resp;
diff --git a/tests/fixture_base_path/routes/index.tsx b/tests/fixture_base_path/routes/index.tsx
index 2f5f11e9..ba73acc0 100644
--- a/tests/fixture_base_path/routes/index.tsx
+++ b/tests/fixture_base_path/routes/index.tsx
@@ -1,3 +1,10 @@
-export default function Home() {
- return
it works
;
-}
+import { defineRoute } from "$fresh/server.ts";
+
+export default defineRoute<{ data: string }>((req, ctx) => {
+ return (
+ <>
+ it works
+ {ctx.state.data}
+ >
+ );
+});
diff --git a/tests/fixture_base_path/static/styles.css b/tests/fixture_base_path/static/styles.css
new file mode 100644
index 00000000..b5c61c95
--- /dev/null
+++ b/tests/fixture_base_path/static/styles.css
@@ -0,0 +1,3 @@
+@tailwind base;
+@tailwind components;
+@tailwind utilities;
diff --git a/tests/fixture_base_path/tailwind.config.ts b/tests/fixture_base_path/tailwind.config.ts
new file mode 100644
index 00000000..63acf078
--- /dev/null
+++ b/tests/fixture_base_path/tailwind.config.ts
@@ -0,0 +1,7 @@
+import { Config } from "tailwindcss";
+
+export default {
+ content: [
+ "{routes,islands,components}/**/*.{ts,tsx}",
+ ],
+} as Config;
diff --git a/tests/fixture_base_path_build/deno.json b/tests/fixture_base_path_build/deno.json
new file mode 100644
index 00000000..d9ccbefa
--- /dev/null
+++ b/tests/fixture_base_path_build/deno.json
@@ -0,0 +1,13 @@
+{
+ "lock": false,
+ "imports": {
+ "$fresh/": "../../",
+ "preact": "https://esm.sh/preact@10.15.1",
+ "preact/": "https://esm.sh/preact@10.15.1/",
+ "tailwindcss": "npm:tailwindcss@3.3.5"
+ },
+ "compilerOptions": {
+ "jsx": "react-jsx",
+ "jsxImportSource": "preact"
+ }
+}
diff --git a/tests/fixture_base_path_build/dev.ts b/tests/fixture_base_path_build/dev.ts
new file mode 100755
index 00000000..1fe3e340
--- /dev/null
+++ b/tests/fixture_base_path_build/dev.ts
@@ -0,0 +1,6 @@
+#!/usr/bin/env -S deno run -A --watch=static/,routes/
+
+import dev from "$fresh/dev.ts";
+import config from "./fresh.config.ts";
+
+await dev(import.meta.url, "./main.ts", config);
diff --git a/tests/fixture_base_path_build/fresh.config.ts b/tests/fixture_base_path_build/fresh.config.ts
new file mode 100644
index 00000000..8668bf3c
--- /dev/null
+++ b/tests/fixture_base_path_build/fresh.config.ts
@@ -0,0 +1,9 @@
+import { defineConfig } from "$fresh/server.ts";
+import tailwind from "$fresh/plugins/tailwind.ts";
+
+export default defineConfig({
+ router: { basePath: "/foo/bar" },
+ plugins: [
+ tailwind(),
+ ],
+});
diff --git a/tests/fixture_base_path_build/fresh.gen.ts b/tests/fixture_base_path_build/fresh.gen.ts
new file mode 100644
index 00000000..e6643ad2
--- /dev/null
+++ b/tests/fixture_base_path_build/fresh.gen.ts
@@ -0,0 +1,19 @@
+// DO NOT EDIT. This file is generated by Fresh.
+// This file SHOULD be checked into source version control.
+// This file is automatically updated during development when running `dev.ts`.
+
+import * as $_app from "./routes/_app.tsx";
+import * as $index from "./routes/index.tsx";
+
+import { type Manifest } from "$fresh/server.ts";
+
+const manifest = {
+ routes: {
+ "./routes/_app.tsx": $_app,
+ "./routes/index.tsx": $index,
+ },
+ islands: {},
+ baseUrl: import.meta.url,
+} satisfies Manifest;
+
+export default manifest;
diff --git a/tests/fixture_base_path_build/main.ts b/tests/fixture_base_path_build/main.ts
new file mode 100644
index 00000000..fc935921
--- /dev/null
+++ b/tests/fixture_base_path_build/main.ts
@@ -0,0 +1,11 @@
+///
+///
+///
+///
+///
+
+import { start } from "$fresh/server.ts";
+import manifest from "./fresh.gen.ts";
+import config from "./fresh.config.ts";
+
+await start(manifest, config);
diff --git a/tests/fixture_base_path_build/routes/_app.tsx b/tests/fixture_base_path_build/routes/_app.tsx
new file mode 100644
index 00000000..2e225e49
--- /dev/null
+++ b/tests/fixture_base_path_build/routes/_app.tsx
@@ -0,0 +1,17 @@
+import { PageProps } from "$fresh/server.ts";
+
+export default function App({ Component }: PageProps) {
+ return (
+
+
+
+
+ My Fresh app
+
+
+
+
+
+
+ );
+}
diff --git a/tests/fixture_base_path_build/routes/index.tsx b/tests/fixture_base_path_build/routes/index.tsx
new file mode 100644
index 00000000..ee3f8f3c
--- /dev/null
+++ b/tests/fixture_base_path_build/routes/index.tsx
@@ -0,0 +1,3 @@
+export default function Page() {
+ return foo
;
+}
diff --git a/tests/fixture_base_path_build/static/styles.css b/tests/fixture_base_path_build/static/styles.css
new file mode 100644
index 00000000..b5c61c95
--- /dev/null
+++ b/tests/fixture_base_path_build/static/styles.css
@@ -0,0 +1,3 @@
+@tailwind base;
+@tailwind components;
+@tailwind utilities;
diff --git a/tests/fixture_base_path_build/tailwind.config.ts b/tests/fixture_base_path_build/tailwind.config.ts
new file mode 100644
index 00000000..63acf078
--- /dev/null
+++ b/tests/fixture_base_path_build/tailwind.config.ts
@@ -0,0 +1,7 @@
+import { Config } from "tailwindcss";
+
+export default {
+ content: [
+ "{routes,islands,components}/**/*.{ts,tsx}",
+ ],
+} as Config;
diff --git a/tests/fixture_base_path_config/deno.json b/tests/fixture_base_path_config/deno.json
new file mode 100644
index 00000000..d9ccbefa
--- /dev/null
+++ b/tests/fixture_base_path_config/deno.json
@@ -0,0 +1,13 @@
+{
+ "lock": false,
+ "imports": {
+ "$fresh/": "../../",
+ "preact": "https://esm.sh/preact@10.15.1",
+ "preact/": "https://esm.sh/preact@10.15.1/",
+ "tailwindcss": "npm:tailwindcss@3.3.5"
+ },
+ "compilerOptions": {
+ "jsx": "react-jsx",
+ "jsxImportSource": "preact"
+ }
+}
diff --git a/tests/fixture_base_path_config/dev.ts b/tests/fixture_base_path_config/dev.ts
new file mode 100755
index 00000000..1fe3e340
--- /dev/null
+++ b/tests/fixture_base_path_config/dev.ts
@@ -0,0 +1,6 @@
+#!/usr/bin/env -S deno run -A --watch=static/,routes/
+
+import dev from "$fresh/dev.ts";
+import config from "./fresh.config.ts";
+
+await dev(import.meta.url, "./main.ts", config);
diff --git a/tests/fixture_base_path_config/foo/Foo.tsx b/tests/fixture_base_path_config/foo/Foo.tsx
new file mode 100644
index 00000000..965dddb6
--- /dev/null
+++ b/tests/fixture_base_path_config/foo/Foo.tsx
@@ -0,0 +1,3 @@
+export function Foo() {
+ return foo
;
+}
diff --git a/tests/fixture_base_path_config/fresh.config.ts b/tests/fixture_base_path_config/fresh.config.ts
new file mode 100644
index 00000000..8668bf3c
--- /dev/null
+++ b/tests/fixture_base_path_config/fresh.config.ts
@@ -0,0 +1,9 @@
+import { defineConfig } from "$fresh/server.ts";
+import tailwind from "$fresh/plugins/tailwind.ts";
+
+export default defineConfig({
+ router: { basePath: "/foo/bar" },
+ plugins: [
+ tailwind(),
+ ],
+});
diff --git a/tests/fixture_base_path_config/fresh.gen.ts b/tests/fixture_base_path_config/fresh.gen.ts
new file mode 100644
index 00000000..e6643ad2
--- /dev/null
+++ b/tests/fixture_base_path_config/fresh.gen.ts
@@ -0,0 +1,19 @@
+// DO NOT EDIT. This file is generated by Fresh.
+// This file SHOULD be checked into source version control.
+// This file is automatically updated during development when running `dev.ts`.
+
+import * as $_app from "./routes/_app.tsx";
+import * as $index from "./routes/index.tsx";
+
+import { type Manifest } from "$fresh/server.ts";
+
+const manifest = {
+ routes: {
+ "./routes/_app.tsx": $_app,
+ "./routes/index.tsx": $index,
+ },
+ islands: {},
+ baseUrl: import.meta.url,
+} satisfies Manifest;
+
+export default manifest;
diff --git a/tests/fixture_base_path_config/main.ts b/tests/fixture_base_path_config/main.ts
new file mode 100644
index 00000000..fc935921
--- /dev/null
+++ b/tests/fixture_base_path_config/main.ts
@@ -0,0 +1,11 @@
+///
+///
+///
+///
+///
+
+import { start } from "$fresh/server.ts";
+import manifest from "./fresh.gen.ts";
+import config from "./fresh.config.ts";
+
+await start(manifest, config);
diff --git a/tests/fixture_base_path_config/routes/_app.tsx b/tests/fixture_base_path_config/routes/_app.tsx
new file mode 100644
index 00000000..2e225e49
--- /dev/null
+++ b/tests/fixture_base_path_config/routes/_app.tsx
@@ -0,0 +1,17 @@
+import { PageProps } from "$fresh/server.ts";
+
+export default function App({ Component }: PageProps) {
+ return (
+
+
+
+
+ My Fresh app
+
+
+
+
+
+
+ );
+}
diff --git a/tests/fixture_base_path_config/routes/index.tsx b/tests/fixture_base_path_config/routes/index.tsx
new file mode 100644
index 00000000..d69dd510
--- /dev/null
+++ b/tests/fixture_base_path_config/routes/index.tsx
@@ -0,0 +1,5 @@
+import { Foo } from "../foo/Foo.tsx";
+
+export default function Page() {
+ return ;
+}
diff --git a/tests/fixture_base_path_config/static/styles.css b/tests/fixture_base_path_config/static/styles.css
new file mode 100644
index 00000000..b5c61c95
--- /dev/null
+++ b/tests/fixture_base_path_config/static/styles.css
@@ -0,0 +1,3 @@
+@tailwind base;
+@tailwind components;
+@tailwind utilities;
diff --git a/tests/fixture_base_path_config/tailwind.config.ts b/tests/fixture_base_path_config/tailwind.config.ts
new file mode 100644
index 00000000..eaef5a3a
--- /dev/null
+++ b/tests/fixture_base_path_config/tailwind.config.ts
@@ -0,0 +1,11 @@
+/** @type {import('tailwindcss').Config} */
+export default {
+ content: ["./foo/**/*.{ts,tsx}"],
+ theme: {
+ extend: {
+ colors: {
+ pp: "peachpuff",
+ },
+ },
+ },
+};
diff --git a/tests/test_utils.ts b/tests/test_utils.ts
index 0630c15e..429d345b 100644
--- a/tests/test_utils.ts
+++ b/tests/test_utils.ts
@@ -259,9 +259,13 @@ async function handleRequest(
// Follow redirects
while (res.headers.has("location")) {
- const loc = res.headers.get("location");
+ let loc = res.headers.get("location")!;
const hostname = conn.remoteAddr.hostname;
- res = await handler(new Request(`https://${hostname}${loc}`), conn);
+ if (!loc.startsWith("http://") && !loc.startsWith("https://")) {
+ loc = `https://${hostname}${loc}`;
+ }
+
+ res = await handler(new Request(loc), conn);
}
return res;