Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-zebras-route.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

Fix API route matching when `server.baseURL` is configured.
3 changes: 3 additions & 0 deletions packages/start/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
"import.meta.env.START_CLIENT_ENTRY": JSON.stringify(handlers.client),
"import.meta.env.START_CLIENT_ENTRY_URL": JSON.stringify(clientEntryUrl),
"import.meta.env.START_DEV_OVERLAY": JSON.stringify(start.devOverlay),
"import.meta.env.SERVER_BASE_URL": JSON.stringify(
(config.server as { baseURL?: string } | undefined)?.baseURL ?? "",
),
"import.meta.env.SEROVAL_MODE": JSON.stringify(start.serialization?.mode || "json"),
},
builder: {
Expand Down
2 changes: 1 addition & 1 deletion packages/start/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface SolidStartMetaEnv {
START_CLIENT_ENTRY_URL: string;
START_ISLANDS: boolean;
// START_DEV_OVERLAY: boolean;
// SERVER_BASE_URL: string;
SERVER_BASE_URL: string;
}
5 changes: 3 additions & 2 deletions packages/start/src/server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { handleServerFunction } from "../fns/handler.ts";
import type { APIEvent, FetchEvent, HandlerOptions, PageEvent } from "./types.ts";
import { getExpectedRedirectStatus } from "./util.ts";
import { toWebReadableStream } from "./web-stream.ts";
import { stripPathBase } from "./strip-path-base.ts";

const SERVER_FN_BASE = "/_server";

Expand Down Expand Up @@ -252,6 +253,6 @@ function produceResponseWithEventHeaders(res: Response) {
}

function stripBaseUrl(path: string) {
if (import.meta.env.BASE_URL === "/" || import.meta.env.BASE_URL === "") return path;
return path.slice(import.meta.env.BASE_URL.length);
const base = import.meta.env.SERVER_BASE_URL || import.meta.env.BASE_URL || "/";
return stripPathBase(path, base);
}
23 changes: 23 additions & 0 deletions packages/start/src/server/strip-path-base.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from "vitest";

import { stripPathBase } from "./strip-path-base.ts";

describe("stripPathBase", () => {
it("leaves paths unchanged for an empty or root base", () => {
expect(stripPathBase("/api/hello", "")).toBe("/api/hello");
expect(stripPathBase("/api/hello", "/")).toBe("/api/hello");
});

it("strips the base from paths below it", () => {
expect(stripPathBase("/app/api/hello", "/app")).toBe("/api/hello");
expect(stripPathBase("/app/api/hello", "/app/")).toBe("/api/hello");
});

it("maps an exact base path to root", () => {
expect(stripPathBase("/app", "/app")).toBe("/");
});

it("does not strip a partial path segment match", () => {
expect(stripPathBase("/application/api/hello", "/app")).toBe("/application/api/hello");
});
});
9 changes: 9 additions & 0 deletions packages/start/src/server/strip-path-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function stripPathBase(path: string, base: string) {
if (!base || base === "/") return path;

const normalizedBase = base.endsWith("/") ? base.slice(0, -1) : base;
if (path === normalizedBase) return "/";
if (path.startsWith(`${normalizedBase}/`)) return path.slice(normalizedBase.length);

return path;
}
Loading