Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: retry failed revalidation caused by HDR #6287

Merged
merged 6 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/remix-react/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ declare global {
};
var __remixRouteModules: RouteModules;
var __remixManifest: EntryContext["manifest"];
var __remixRevalidation: number | undefined;
var $RefreshRuntime$: {
performReactRefresh: () => void;
};
Expand Down Expand Up @@ -139,6 +140,7 @@ if (import.meta && import.meta.hot) {
}, 1);
}
});
window.__remixRevalidation = (window.__remixRevalidation || 0) + 1;
router.revalidate();
}
);
Expand Down
24 changes: 21 additions & 3 deletions packages/remix-react/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function isCatchResponse(response: any): boolean {
);
}

export function isErrorResponse(response: any): boolean {
export function isErrorResponse(response: any): response is Response {
return (
response instanceof Response &&
response.headers.get("X-Remix-Error") != null
Expand All @@ -40,7 +40,8 @@ export function isDeferredResponse(response: any): boolean {

export async function fetchData(
request: Request,
routeId: string
routeId: string,
retry = 0
): Promise<Response | Error> {
let url = new URL(request.url);
url.searchParams.set("_data", routeId);
Expand All @@ -59,7 +60,24 @@ export async function fetchData(
: await request.formData();
}

let response = await fetch(url.href, init);
if (retry > 0) {
// Retry up to seven times waiting 20, 40, 80, 160, 320, 640, and 1280 ms
// between retries for a total of 2540 ms before giving up.
await new Promise((resolve) => setTimeout(resolve, 2 ** retry * 10));
}

let revalidation = window.__remixRevalidation;
let response = await fetch(url.href, init).catch((error) => {
if (
typeof revalidation === "number" &&
revalidation === window.__remixRevalidation &&
error?.name === "TypeError" &&
retry <= 7
) {
return fetchData(request, routeId, retry + 1);
}
throw error;
});

if (isErrorResponse(response)) {
let data = await response.json();
Expand Down