Skip to content

Commit

Permalink
fix #1645 flash encoding, and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Oct 31, 2024
1 parent ec69889 commit 715d563
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-days-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

fix #1645 flash encoding, and improve error handling
2 changes: 1 addition & 1 deletion examples/with-auth/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const getUser = query(async () => {
return { id: user.id, username: user.username };
} catch {
await logoutSession();
redirect("/login");
throw redirect("/login");
}
}, "user");

Expand Down
6 changes: 3 additions & 3 deletions packages/start/src/runtime/server-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boole
if (result) {
headers.append(
"Set-Cookie",
`flash=${JSON.stringify({
url: url.pathname + encodeURIComponent(url.search),
`flash=${encodeURIComponent(JSON.stringify({
url: url.pathname + url.search,
result: isError ? result.message : result,
thrown: thrown,
error: isError,
input: [...parsed.slice(0, -1), [...parsed[parsed.length - 1].entries()]]
})}; Secure; HttpOnly;`
}))}; Secure; HttpOnly;`
);
}
return new Response(null, {
Expand Down
38 changes: 21 additions & 17 deletions packages/start/src/server/pageEvent.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import {
getCookie,
setCookie
} from "vinxi/http";
import { getCookie, setCookie } from "vinxi/http";
import { createRoutes } from "../router/FileRoutes";
import { FetchEvent, PageEvent } from "./types";

function initFromFlash(ctx: FetchEvent) {
const flash = getCookie(ctx.nativeEvent, "flash");
if (!flash) return;
let param = JSON.parse(flash);
if (!param || !param.result) return;
const input = [...param.input.slice(0, -1), new Map(param.input[param.input.length - 1])];
setCookie(ctx.nativeEvent, "flash", "", { maxAge: 0 });
const result = param.error ? new Error(param.result) : param.result;
return {
input,
url: param.url,
pending: false,
result: param.thrown ? undefined : result,
error: param.thrown ? result : undefined,
};
try {
let param = JSON.parse(flash);
if (!param || !param.result) return;
const input = [...param.input.slice(0, -1), new Map(param.input[param.input.length - 1])];
const result = param.error ? new Error(param.result) : param.result;
return {
input,
url: param.url,
pending: false,
result: param.thrown ? undefined : result,
error: param.thrown ? result : undefined
};
} catch (e) {
console.error(e);
} finally {
setCookie(ctx.nativeEvent, "flash", "", { maxAge: 0 });
}
}

export async function createPageEvent(ctx: FetchEvent) {
Expand All @@ -32,7 +34,9 @@ export async function createPageEvent(ctx: FetchEvent) {
manifest: await clientManifest.json(),
assets: [
...(await clientManifest.inputs[clientManifest.handler]!.assets()),
...(import.meta.env.DEV ? await clientManifest.inputs[import.meta.env.START_APP]!.assets(): []),
...(import.meta.env.DEV
? await clientManifest.inputs[import.meta.env.START_APP]!.assets()
: []),
...(import.meta.env.START_ISLANDS
? (await serverManifest.inputs[serverManifest.handler]!.assets()).filter(
s => (s as any).attrs.rel !== "modulepreload"
Expand Down

0 comments on commit 715d563

Please sign in to comment.