Skip to content

Commit

Permalink
fix #1649 append rather than override headers with flash message
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Oct 31, 2024
1 parent aa30192 commit ec69889
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-pandas-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

fix #1649 append rather than override headers with flash message
54 changes: 29 additions & 25 deletions packages/start/src/runtime/server-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ async function handleServerFunction(h3Event: HTTPEvent) {
// workaround for https://github.com/unjs/nitro/issues/1721
// (issue only in edge runtimes)
parsed.push(
await(
isH3EventBodyStreamLocked
? request
: new Request(request, { ...request, body: requestBody })
await (isH3EventBodyStreamLocked
? request
: new Request(request, { ...request, body: requestBody })
).formData()
);
// what should work when #1721 is fixed
Expand Down Expand Up @@ -235,31 +234,36 @@ async function handleServerFunction(h3Event: HTTPEvent) {
function handleNoJS(result: any, request: Request, parsed: any[], thrown?: boolean) {
const url = new URL(request.url);
const isError = result instanceof Error;
let redirectUrl = new URL(request.headers.get("referer")!).toString();
let statusCode = 302;
if (result instanceof Response && result.headers.has("Location")) {
redirectUrl = new URL(
result.headers.get("Location")!,
url.origin + import.meta.env.SERVER_BASE_URL
).toString();
statusCode = getExpectedRedirectStatus(result);
let headers;
if (result instanceof Response) {
headers = new Headers(result.headers);
if (result.headers.has("Location")) {
headers.set(
`Location`,
new URL(
result.headers.get("Location")!,
url.origin + import.meta.env.SERVER_BASE_URL
).toString()
);
statusCode = getExpectedRedirectStatus(result);
}
} else headers = new Headers({ Location: new URL(request.headers.get("referer")!).toString() });
if (result) {
headers.append(
"Set-Cookie",
`flash=${JSON.stringify({
url: url.pathname + encodeURIComponent(url.search),
result: isError ? result.message : result,
thrown: thrown,
error: isError,
input: [...parsed.slice(0, -1), [...parsed[parsed.length - 1].entries()]]
})}; Secure; HttpOnly;`
);
}
return new Response(null, {
status: statusCode,
headers: {
Location: redirectUrl,
...(result
? {
"Set-Cookie": `flash=${JSON.stringify({
url: url.pathname + encodeURIComponent(url.search),
result: isError ? result.message : result,
thrown: thrown,
error: isError,
input: [...parsed.slice(0, -1), [...parsed[parsed.length - 1].entries()]]
})}; Secure; HttpOnly;`
}
: {})
}
headers
});
}

Expand Down

0 comments on commit ec69889

Please sign in to comment.