fix: islands not working with basePath (#2496)

This commit is contained in:
Marvin Hagemeister
2024-06-05 09:02:27 +02:00
committed by GitHub
parent f8b60ddd15
commit d613c6a6ba
3 changed files with 41 additions and 9 deletions
+12 -5
View File
@@ -14,14 +14,21 @@ import { getBuildCache } from "../context.ts";
*/ */
export function staticFiles<T>(): MiddlewareFn<T> { export function staticFiles<T>(): MiddlewareFn<T> {
return async function freshStaticFiles(ctx) { return async function freshStaticFiles(ctx) {
const { req, url } = ctx; const { req, url, config } = ctx;
const buildCache = getBuildCache(ctx); const buildCache = getBuildCache(ctx);
let pathname = url.pathname;
if (config.basePath) {
pathname = pathname !== config.basePath
? pathname.slice(config.basePath.length)
: "/";
}
// Fast path bail out // Fast path bail out
const file = await buildCache.readFile(url.pathname); const file = await buildCache.readFile(pathname);
if (url.pathname === "/" || file === null) { if (pathname === "/" || file === null) {
// Optimization: Prevent long responses for favicon.ico requests // Optimization: Prevent long responses for favicon.ico requests
if (url.pathname === "/favicon.ico") { if (pathname === "/favicon.ico") {
return new Response(null, { status: 404 }); return new Response(null, { status: 404 });
} }
return ctx.next(); return ctx.next();
@@ -43,7 +50,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
}); });
} }
const ext = path.extname(url.pathname); const ext = path.extname(pathname);
const etag = file.hash; const etag = file.hash;
const contentType = getContentType(ext); const contentType = getContentType(ext);
+1 -2
View File
@@ -70,7 +70,6 @@ export class RenderState {
error: Error | null = null; error: Error | null = null;
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
slots: Array<{ id: number; name: string; vnode: VNode<any> } | null> = []; slots: Array<{ id: number; name: string; vnode: VNode<any> } | null> = [];
basePath = ""; // FIXME
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
islandProps: any[] = []; islandProps: any[] = [];
islands = new Set<Island>(); islands = new Set<Island>();
@@ -463,7 +462,7 @@ function FreshRuntimeScript() {
const named = island.exportName === "default" const named = island.exportName === "default"
? island.name ? island.name
: `{ ${island.exportName} }`; : `{ ${island.exportName} }`;
return `import ${named} from "${asset(chunk)}";`; return `import ${named} from "${asset(`${basePath}${chunk}`)}";`;
}).join(""); }).join("");
const islandObj = "{" + islandArr.map((island) => island.name) const islandObj = "{" + islandArr.map((island) => island.name)
+28 -2
View File
@@ -28,11 +28,12 @@ import { EscapeIsland } from "./fixtures_islands/EscapeIsland.tsx";
import * as path from "@std/path"; import * as path from "@std/path";
import { setBuildCache } from "../src/app.ts"; import { setBuildCache } from "../src/app.ts";
import { getBuildCache } from "../src/app.ts"; import { getBuildCache } from "../src/app.ts";
import type { FreshConfig } from "../src/config.ts";
await buildProd(allIslandApp); await buildProd(allIslandApp);
function testApp() { function testApp(config?: FreshConfig) {
const app = new App(); const app = new App(config);
setBuildCache(app, getBuildCache(allIslandApp)); setBuildCache(app, getBuildCache(allIslandApp));
return app; return app;
} }
@@ -575,6 +576,31 @@ Deno.test({
}, },
}); });
Deno.test({
name: "islands - in base path",
fn: async () => {
const selfCounter = getIsland("SelfCounter.tsx");
const app = testApp({ basePath: "/foo" })
.use(staticFiles())
.island(selfCounter, "SelfCounter", SelfCounter)
.get("/", (ctx) =>
ctx.render(
<Doc>
<SelfCounter />
</Doc>,
));
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/foo`, { waitUntil: "load" });
await page.locator(".ready").wait();
await page.locator(".increment").click();
await waitForText(page, ".output", "1");
});
},
});
Deno.test({ Deno.test({
name: "fsRoutes - load islands from group folder", name: "fsRoutes - load islands from group folder",
fn: async () => { fn: async () => {