Skip to content

Commit 76724af

Browse files
committedFeb 6, 2024
pass revalidate through json helper
1 parent 0eca5a1 commit 76724af

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed
 

‎.changeset/tidy-lizards-explain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
pass revalidate through `json` helper

‎src/data/response.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type RouterResponseInit = ResponseInit & { revalidate?: string | string[] };
1+
export type RouterResponseInit = Omit<ResponseInit, "body"> & { revalidate?: string | string[] };
22

33
export function redirect(url: string, init: number | RouterResponseInit = 302) {
44
let responseInit: ResponseInit;
@@ -24,21 +24,25 @@ export function redirect(url: string, init: number | RouterResponseInit = 302) {
2424
return response as never;
2525
}
2626

27-
export function reload(init: RouterResponseInit) {
27+
export function reload(init: RouterResponseInit = {}) {
2828
const { revalidate, ...responseInit } = init;
29+
const headers = new Headers(responseInit.headers);
30+
revalidate && headers.set("X-Revalidate", revalidate.toString());
31+
2932
return new Response(null, {
3033
...responseInit,
31-
...(revalidate
32-
? { headers: new Headers(responseInit.headers).set("X-Revalidate", revalidate.toString()) }
33-
: {})
34+
headers
3435
}) as never;
3536
}
3637

37-
export function json<T>(data: T, init?: Omit<ResponseInit, "body">) {
38-
const headers = new Headers((init || {}).headers);
38+
export function json<T>(data: T, init: RouterResponseInit = {}) {
39+
const { revalidate, ...responseInit } = init;
40+
const headers = new Headers(responseInit.headers);
41+
revalidate && headers.set("X-Revalidate", revalidate.toString());
3942
headers.set("Content-Type", "application/json");
43+
4044
const response = new Response(JSON.stringify(data), {
41-
...init,
45+
...responseInit,
4246
headers
4347
});
4448
(response as any).customBody = () => data;

0 commit comments

Comments
 (0)