Skip to content
Open
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/fix-baseurl-api-routes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

Strip baseURL prefix before matching API routes.
5 changes: 4 additions & 1 deletion packages/start/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
},
};
},
async config(_, env) {
async config(config, env) {
const clientInput = [handlers.client];
if (env.command === "build") {
const clientRouter: BaseFileSystemRouter = (globalThis as any).ROUTERS.client;
Expand Down Expand Up @@ -143,6 +143,9 @@ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
"import.meta.env.START_CLIENT_ENTRY": JSON.stringify(handlers.client),
"import.meta.env.START_DEV_OVERLAY": JSON.stringify(start.devOverlay),
"import.meta.env.SEROVAL_MODE": JSON.stringify(start.serialization?.mode || "json"),
"import.meta.env.SERVER_BASE_URL": JSON.stringify(
(config as { server?: { baseURL?: string } })?.server?.baseURL ?? "",
),
},
builder: {
sharedPlugins: true,
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 @@ -13,5 +13,5 @@ interface SolidStartMetaEnv {
START_CLIENT_ENTRY: string;
START_ISLANDS: boolean;
// START_DEV_OVERLAY: boolean;
// SERVER_BASE_URL: string;
SERVER_BASE_URL: string;
}
6 changes: 4 additions & 2 deletions packages/start/src/server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { decorateHandler, decorateMiddleware } from "./fetchEvent.ts";
import { getSsrManifest } from "./manifest/ssr-manifest.ts";
import { matchAPIRoute } from "./routes.ts";
import { handleServerFunction } from "./server-functions-handler.ts";
import { stripPathBase } from "./strip-path-base.ts";
import type { APIEvent, FetchEvent, HandlerOptions, PageEvent } from "./types.ts";
import { getExpectedRedirectStatus } from "./util.ts";

Expand Down Expand Up @@ -254,6 +255,7 @@ 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);
}
26 changes: 26 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,26 @@
import { describe, expect, it } from "vitest";

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

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

it("strips base without trailing slash", () => {
expect(stripPathBase("/myapp/api/hello", "/myapp")).toBe("/api/hello");
});

it("strips base with trailing slash", () => {
expect(stripPathBase("/myapp/api/hello", "/myapp/")).toBe("/api/hello");
});

it("maps exact base match to /", () => {
expect(stripPathBase("/myapp", "/myapp")).toBe("/");
});

it("does not strip when path is not under base", () => {
expect(stripPathBase("/other/api", "/myapp")).toBe("/other/api");
});
});
7 changes: 7 additions & 0 deletions packages/start/src/server/strip-path-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** Strip deploy base path from a URL pathname so API and page routes match file routes. */
export function stripPathBase(path: string, base: string): string {
if (base === "/" || base === "") return path;
const normalizedBase = base.endsWith("/") ? base.slice(0, -1) : base;
if (normalizedBase === "") return path;
return path.startsWith(normalizedBase) ? path.slice(normalizedBase.length) || "/" : path;
}
Loading