Skip to content

Commit 7bf97ef

Browse files
committed
handle absolute paths properly
1 parent ad7626b commit 7bf97ef

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

packages/start/src/server/server-fns-runtime.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { provideRequestEvent } from "solid-js/web/storage";
44
export function createServerReference(fn: Function, id: string) {
55
if (typeof fn !== "function")
66
throw new Error("Export from a 'use server' module must be a function");
7-
const baseURL = import.meta.env.BASE_URL ?? "";
7+
let baseURL = import.meta.env.BASE_URL ?? "/";
8+
if(!baseURL.endsWith("/")) baseURL += "/"
9+
810
return new Proxy(fn, {
911
get(target, prop, receiver) {
1012
if (prop === "url") {
11-
return `${baseURL}/_server?id=${encodeURIComponent(id)}`;
13+
return `${baseURL}_server?id=${encodeURIComponent(id)}`;
1214
}
1315
if (prop === "GET") return receiver;
1416
return (target as any)[prop];

packages/start/src/server/server-runtime.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @ts-ignore - seroval exports issue with NodeNext
2+
import { join } from "pathe";
23
import { deserialize, toJSONAsync } from "seroval";
34
import {
45
CustomEventPlugin,
@@ -193,19 +194,21 @@ async function fetchServerFunction(
193194
}
194195

195196
export function createServerReference(id: string) {
196-
const baseURL = import.meta.env.BASE_URL ?? "";
197-
const fn = (...args: any[]) => fetchServerFunction(`${baseURL}/_server`, id, {}, args);
197+
let baseURL = import.meta.env.BASE_URL ?? "/";
198+
if(!baseURL.endsWith("/")) baseURL += "/"
199+
200+
const fn = (...args: any[]) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
198201

199202
return new Proxy(fn, {
200203
get(target, prop, receiver) {
201204
if (prop === "url") {
202-
return `${baseURL}/_server?id=${encodeURIComponent(id)}`;
205+
return `${baseURL}_server?id=${encodeURIComponent(id)}`;
203206
}
204207
if (prop === "GET") {
205208
return receiver.withOptions({ method: "GET" });
206209
}
207210
if (prop === "withOptions") {
208-
const url = `${baseURL}/_server?id=${encodeURIComponent(id)}`;
211+
const url = `${baseURL}_server?id=${encodeURIComponent(id)}`;
209212
return (options: RequestInit) => {
210213
const fn = async (...args: any[]) => {
211214
const encodeArgs = options.method && options.method.toUpperCase() === "GET";
@@ -217,7 +220,7 @@ export function createServerReference(id: string) {
217220
JSON.stringify(await Promise.resolve(toJSONAsync(args, { plugins })))
218221
)}`
219222
: "")
220-
: `${baseURL}/_server`,
223+
: `${baseURL}_server`,
221224
id,
222225
options,
223226
encodeArgs ? [] : args

0 commit comments

Comments
 (0)