Skip to content

Commit 0bc9bc5

Browse files
committed
feat: add retry logic for load client data
1 parent 23cd290 commit 0bc9bc5

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

packages/qwik-router/src/runtime/src/use-endpoint.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { ClientPageData, RouteActionValue } from './types';
44
import { _deserialize } from '@qwik.dev/core/internal';
55
import { prefetchSymbols } from './client-navigate';
66

7+
const MAX_Q_DATA_RETRY_COUNT = 3;
8+
79
export const loadClientData = async (
810
url: URL,
911
element: unknown,
@@ -13,8 +15,9 @@ export const loadClientData = async (
1315
clearCache?: boolean;
1416
prefetchSymbols?: boolean;
1517
isPrefetch?: boolean;
16-
}
17-
) => {
18+
},
19+
retryCount: number = 0
20+
): Promise<ClientPageData | undefined> => {
1821
const pagePathname = url.pathname;
1922
const pageSearch = url.search;
2023
const clientDataPath = getClientDataPath(pagePathname, pageSearch, {
@@ -37,6 +40,12 @@ export const loadClientData = async (
3740
opts.action.data = undefined;
3841
}
3942
qData = fetch(clientDataPath, fetchOptions).then((rsp) => {
43+
if (rsp.status === 404 && opts?.loaderIds && retryCount < MAX_Q_DATA_RETRY_COUNT) {
44+
// retry if the q-data.json is not found with all options
45+
// we want to retry with all the loaders
46+
opts.loaderIds = undefined;
47+
return loadClientData(url, element, opts, retryCount + 1);
48+
}
4049
if (rsp.redirected) {
4150
const redirectedURL = new URL(rsp.url);
4251
const isQData = redirectedURL.pathname.endsWith('/q-data.json');

packages/qwik-router/src/runtime/src/utils.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ClientPageData, SimpleURL } from './types';
1+
import type { SimpleURL } from './types';
22

33
import { createAsyncComputed$, isBrowser } from '@qwik.dev/core';
44
import {
@@ -112,16 +112,9 @@ export const createLoaderSignal = (
112112
return createAsyncComputed$(
113113
async () => {
114114
if (isBrowser && loadersObject[loaderId] === _UNINITIALIZED) {
115-
let data: ClientPageData | undefined;
116-
try {
117-
// Try to load only the current loader data from the server
118-
data = await loadClientData(url, undefined, {
119-
loaderIds: [loaderId],
120-
});
121-
} catch (_) {
122-
// If request fails, we try to load all the loaders data from the server
123-
data = await loadClientData(url, undefined);
124-
}
115+
const data = await loadClientData(url, undefined, {
116+
loaderIds: [loaderId],
117+
});
125118
loadersObject[loaderId] = data?.loaders[loaderId] ?? _UNINITIALIZED;
126119
}
127120
return loadersObject[loaderId];

starters/e2e/qwikrouter/loaders.e2e.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ test.describe("loaders", () => {
181181
}
182182
});
183183

184-
test.only("should retry with all loaders if one fails", async ({
184+
test("should retry with all loaders if one fails", async ({
185185
page,
186186
javaScriptEnabled,
187187
}) => {
@@ -197,9 +197,9 @@ test.describe("loaders", () => {
197197
});
198198

199199
await page.route(
200-
"*/**/qwikrouter-test/loaders-serialization/q-data.json?qloaders=HEA0jeovonw",
200+
"*/**/qwikrouter-test/loaders-serialization/q-data.json?qloaders=*",
201201
async (route) => {
202-
await route.abort();
202+
await route.fulfill({ status: 404 });
203203
},
204204
);
205205
await page.goto("/qwikrouter-test/loaders-serialization/");

0 commit comments

Comments
 (0)