diff --git a/.changeset/short-days-itch.md b/.changeset/short-days-itch.md new file mode 100644 index 000000000..39d9f920e --- /dev/null +++ b/.changeset/short-days-itch.md @@ -0,0 +1,5 @@ +--- +"@solidjs/start": patch +--- + +fix #1645 flash encoding, and improve error handling diff --git a/examples/with-auth/src/lib/index.ts b/examples/with-auth/src/lib/index.ts index 71f31f3ee..5193db938 100644 --- a/examples/with-auth/src/lib/index.ts +++ b/examples/with-auth/src/lib/index.ts @@ -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"); diff --git a/packages/start/src/runtime/server-handler.ts b/packages/start/src/runtime/server-handler.ts index 7dbd5c165..255283168 100644 --- a/packages/start/src/runtime/server-handler.ts +++ b/packages/start/src/runtime/server-handler.ts @@ -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, { diff --git a/packages/start/src/server/pageEvent.ts b/packages/start/src/server/pageEvent.ts index 4cb11d66d..c105673b0 100644 --- a/packages/start/src/server/pageEvent.ts +++ b/packages/start/src/server/pageEvent.ts @@ -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) { @@ -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"