fix: tailwind now respects basepath (#2138)
This commit is contained in:
+4
-1
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
+80
-1
@@ -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 },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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(),
|
||||
],
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
export default function Home() {
|
||||
return <h1>it works</h1>;
|
||||
}
|
||||
import { defineRoute } from "$fresh/server.ts";
|
||||
|
||||
export default defineRoute<{ data: string }>((req, ctx) => {
|
||||
return (
|
||||
<>
|
||||
<h1 class="text-red-600 block">it works</h1>
|
||||
{ctx.state.data}
|
||||
</>
|
||||
);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Config } from "tailwindcss";
|
||||
|
||||
export default {
|
||||
content: [
|
||||
"{routes,islands,components}/**/*.{ts,tsx}",
|
||||
],
|
||||
} as Config;
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
Executable
+6
@@ -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);
|
||||
@@ -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(),
|
||||
],
|
||||
});
|
||||
@@ -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;
|
||||
@@ -0,0 +1,11 @@
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
/// <reference lib="dom.asynciterable" />
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await start(manifest, config);
|
||||
@@ -0,0 +1,17 @@
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function App({ Component }: PageProps) {
|
||||
return (
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>My Fresh app</title>
|
||||
<link rel="stylesheet" href="/styles.css" />
|
||||
</head>
|
||||
<body>
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function Page() {
|
||||
return <h1 class="text-red-600 block">foo</h1>;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Config } from "tailwindcss";
|
||||
|
||||
export default {
|
||||
content: [
|
||||
"{routes,islands,components}/**/*.{ts,tsx}",
|
||||
],
|
||||
} as Config;
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
Executable
+6
@@ -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);
|
||||
@@ -0,0 +1,3 @@
|
||||
export function Foo() {
|
||||
return <h1 class="bold text-pp bg-black text-3xl">foo</h1>;
|
||||
}
|
||||
@@ -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(),
|
||||
],
|
||||
});
|
||||
@@ -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;
|
||||
@@ -0,0 +1,11 @@
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
/// <reference lib="dom.asynciterable" />
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await start(manifest, config);
|
||||
@@ -0,0 +1,17 @@
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function App({ Component }: PageProps) {
|
||||
return (
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>My Fresh app</title>
|
||||
<link rel="stylesheet" href="/styles.css" />
|
||||
</head>
|
||||
<body>
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { Foo } from "../foo/Foo.tsx";
|
||||
|
||||
export default function Page() {
|
||||
return <Foo />;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
@@ -0,0 +1,11 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
export default {
|
||||
content: ["./foo/**/*.{ts,tsx}"],
|
||||
theme: {
|
||||
extend: {
|
||||
colors: {
|
||||
pp: "peachpuff",
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
+6
-2
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user