Skip to content

Commit dd40610

Browse files
authored
Handle base url in api routes (#2029)
1 parent 6cbba24 commit dd40610

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

.changeset/nasty-ladybugs-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
Handle base url in api routes

packages/start/src/server/handler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ export function createBaseHandler(
3232
handler: decorateHandler(async (e: H3Event) => {
3333
const event = getRequestEvent()!;
3434
const url = new URL(event.request.url);
35-
const pathname = url.pathname;
35+
const pathname = stripBaseUrl(url.pathname);
3636

37-
const serverFunctionTest = join(import.meta.env.BASE_URL, SERVER_FN_BASE);
38-
if (pathname.startsWith(serverFunctionTest)) {
37+
if (pathname.startsWith(SERVER_FN_BASE)) {
3938
const serverFnResponse = await handleServerFunction(e);
4039

4140
if (serverFnResponse instanceof Response)
@@ -265,3 +264,8 @@ function produceResponseWithEventHeaders(res: Response) {
265264

266265
return ret
267266
}
267+
268+
function stripBaseUrl(path: string) {
269+
if(import.meta.env.BASE_URL === "/" || import.meta.env.BASE_URL === "") return path;
270+
return path.slice(import.meta.env.BASE_URL.length);
271+
}

0 commit comments

Comments
 (0)