Skip to content

Commit

Permalink
GET server functions, response returns, cache to use GET
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Jan 24, 2024
1 parent b5573d9 commit 24a4eb2
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-icons-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

GET server functions, response returns, cache to use GET
2 changes: 1 addition & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"@solidjs/meta": "^0.29.2",
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"solid-js": "^1.8.11",
"vinxi": "^0.1.4"
Expand Down
2 changes: 1 addition & 1 deletion examples/experiments/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"@solidjs/meta": "^0.29.2",
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"solid-js": "^1.8.11",
"vinxi": "^0.1.4"
Expand Down
13 changes: 10 additions & 3 deletions examples/experiments/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Title } from "@solidjs/meta";
import { clientOnly } from "@solidjs/start";
import { json } from "@solidjs/router";
import { clientOnly, GET } from "@solidjs/start";
import Counter from "~/components/Counter";
const BreaksOnServer = clientOnly(() => import("~/components/BreaksOnServer"));

async function hello(name: string) {
"use server";
return "hello " + name;
return json(
{ hello: new Promise<string>(r => setTimeout(() => r(name), 1000)) },
{ headers: { "cache-control": "max-age=60" } }
);
}

export default function Home() {
hello("John").then(console.log);
GET(hello, "John").then(async v => {
console.log(v);
console.log(await v.hello);
});
return (
<main>
<Title>Hello World</Title>
Expand Down
2 changes: 1 addition & 1 deletion examples/hackernews/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "vinxi start"
},
"dependencies": {
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"solid-js": "^1.8.11",
"vinxi": "^0.1.4"
Expand Down
2 changes: 1 addition & 1 deletion examples/todomvc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "vinxi start"
},
"dependencies": {
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"solid-js": "^1.8.11",
"unstorage": "1.10.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@types/node": "^20.10.1"
},
"dependencies": {
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"solid-js": "^1.8.11",
"unstorage": "1.10.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"@mdx-js/mdx": "^2.3.0",
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"@vinxi/plugin-mdx": "^3.6.7",
"solid-js": "^1.8.11",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@prisma/client": "^5.7.0",
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"prisma": "^5.7.0",
"solid-js": "^1.8.11",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-solid-styled/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"@solidjs/meta": "^0.29.2",
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"solid-js": "^1.8.11",
"solid-styled": "^0.8.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-tailwindcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "vinxi start"
},
"dependencies": {
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"autoprefixer": "^10.4.14",
"postcss": "^8.4.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@trpc/client": "^10.44.1",
"@trpc/server": "^10.44.1",
"@solidjs/meta": "^0.29.2",
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"solid-js": "^1.8.11",
"valibot": "^0.23.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type": "module",
"devDependencies": {
"@solidjs/meta": "^0.29.3",
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "^0.4.10",
"@solidjs/testing-library": "^0.8.5",
"@testing-library/jest-dom": "^6.1.5",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@solidjs/meta": "^0.29.0",
"@solidjs/router": "^0.10.9",
"@solidjs/router": "^0.10.10",
"@solidjs/start": "workspace:*",
"@tailwindcss/typography": "^0.5.9",
"@trpc/client": "^9.27.4",
Expand Down
46 changes: 39 additions & 7 deletions packages/start/config/server-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,33 @@ async function handleServerFunction(h3Event) {
}
}
try {
const result = await provideRequestEvent(event, () => {
let result = await provideRequestEvent(event, () => {
/* @ts-ignore */
sharedConfig.context = { event };
return action(...parsed);
});

// handle responses
if (result instanceof Response) {
if (result.status === 302) {
return new Response(null, {
status: instance ? 204 : 302,
headers: {
Location: result.headers.get("Location")
}
});
}
// forward headers
if (result.headers) {
for (const [key, value] of result.headers.entries()) {
setHeader(h3Event, key, value);
}
}
if (result.customBody) {
result = await result.customBody();
} else if (result.body == undefined) result = undefined;
}

// handle no JS success case
if (!instance) {
const isError = result instanceof Error;
Expand All @@ -142,13 +163,24 @@ async function handleServerFunction(h3Event) {
setHeader(h3Event, "content-type", "text/javascript");
return serializeToStream(instance, result);
} catch (x) {
if (x instanceof Response && x.status === 302) {
return new Response(null, {
status: instance ? 204 : 302,
headers: {
Location: x.headers.get("Location")
if (x instanceof Response) {
if (x.status === 302) {
return new Response(null, {
status: instance ? 204 : 302,
headers: {
Location: x.headers.get("Location")
}
});
}
// forward headers
if (x.headers) {
for (const [key, value] of x.headers.entries()) {
setHeader(h3Event, key, value);
}
});
}
if (x.customBody) {
x = await x.customBody();
} else if (x.body == undefined) x = undefined;
}
return x;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/start/config/server-runtime.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ function createRequest(base, id, instance, body, contentType) {
async function fetchServerFunction(base, id, method, args) {
const instance = `server-fn:${INSTANCE++}`;
const response = await (method === "GET"
? fetch(base + `/?args=${JSON.stringify(args)}`, {
? fetch(base + (args.length ? `&args=${JSON.stringify(args)}` : ""), {
headers: {
"x-server-id": id,
"x-server-instance": instance
}
})
Expand Down Expand Up @@ -184,7 +183,7 @@ export function createServerReference(fn, id, name) {
return `${baseURL}/_server?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
}
if (prop === "GET") {
return (...args) => fetchServerFunction(`${baseURL}/_server`, `${id}#${name}`, "GET", args);
return (...args) => fetchServerFunction(`${baseURL}/_server/?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`, `${id}#${name}`, "GET", args);
}
},
apply(target, thisArg, args) {
Expand Down
3 changes: 3 additions & 0 deletions packages/start/shared/GET.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function GET<T extends (...args: any[]) => any>(fn: T, ...args: Parameters<T>) {
return (fn as any).GET(...args) as ReturnType<T>;
}
3 changes: 2 additions & 1 deletion packages/start/shared/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as clientOnly } from "./clientOnly";
export { FileRoutes } from "./FileRoutes";
export { GET } from "./GET";
export { HttpHeader } from "./HttpHeader";
export { HttpStatusCode } from "./HttpStatusCode";
export { default as clientOnly } from "./clientOnly";

Loading

0 comments on commit 24a4eb2

Please sign in to comment.