Skip to content

Commit 5af3da8

Browse files
committedApr 29, 2024
fix #407 single flight without explicit keys
1 parent 1d0d4ff commit 5af3da8

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed
 

‎.changeset/red-pans-retire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
fix #407 single flight without explicit keys

‎src/data/action.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,22 @@ export function useSubmission<T extends Array<any>, U>(
5252

5353
export function useAction<T extends Array<any>, U>(action: Action<T, U>) {
5454
const r = useRouter();
55-
return (...args: Parameters<Action<T, U>>) => action.apply({r}, args);
55+
return (...args: Parameters<Action<T, U>>) => action.apply({ r }, args);
5656
}
5757

5858
export function action<T extends Array<any>, U = void>(
5959
fn: (...args: T) => Promise<U>,
6060
name?: string
6161
): Action<T, U> {
62-
function mutate(this: { r: RouterContext, f?: HTMLFormElement }, ...variables: T) {
62+
function mutate(this: { r: RouterContext; f?: HTMLFormElement }, ...variables: T) {
6363
const router = this.r;
6464
const form = this.f;
6565
const p = (
6666
router.singleFlight && (fn as any).withOptions
6767
? (fn as any).withOptions({ headers: { "X-Single-Flight": "true" } })
6868
: fn
6969
)(...variables);
70-
const [result, setResult] = createSignal<{ data?: U, error?: any }>();
70+
const [result, setResult] = createSignal<{ data?: U; error?: any }>();
7171
let submission: Submission<T, U>;
7272
function handler(error?: boolean) {
7373
return async (res: any) => {
@@ -144,22 +144,18 @@ const hashString = (s: string) =>
144144

145145
async function handleResponse(response: unknown, error: boolean | undefined, navigate: Navigator) {
146146
let data: any;
147+
let custom: any;
147148
let keys: string[] | undefined;
148-
let invalidateKeys: string[] | undefined;
149+
let flightKeys: string[] | undefined;
149150
if (response instanceof Response) {
150151
if (response.headers.has("X-Revalidate"))
151-
keys = invalidateKeys = response.headers.get("X-Revalidate")!.split(",");
152+
keys = response.headers.get("X-Revalidate")!.split(",");
152153
if ((response as any).customBody) {
153-
data = await (response as any).customBody();
154+
data = custom = await (response as any).customBody();
154155
if (response.headers.has("X-Single-Flight")) {
155-
keys || (keys = []);
156-
invalidateKeys || (invalidateKeys = []);
157-
Object.keys(data).forEach(key => {
158-
if (key === "_$value") return;
159-
keys!.push(key);
160-
cache.set(key, data[key]);
161-
});
162156
data = data._$value;
157+
delete custom._$value;
158+
flightKeys = Object.keys(custom);
163159
}
164160
}
165161
if (response.headers.has("Location")) {
@@ -173,7 +169,9 @@ async function handleResponse(response: unknown, error: boolean | undefined, nav
173169
} else if (error) return { error: response };
174170
else data = response;
175171
// invalidate
176-
cacheKeyOp(invalidateKeys, entry => (entry[0] = 0));
172+
cacheKeyOp(keys, entry => (entry[0] = 0));
173+
// set cache
174+
flightKeys && flightKeys.forEach(k => cache.set(k, custom[k]));
177175
// trigger revalidation
178176
await revalidate(keys, false);
179177
return data != null ? { data } : undefined;

0 commit comments

Comments
 (0)
Please sign in to comment.