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 all 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
5 changes: 5 additions & 0 deletions .changeset/hdr-revalidation-retry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

retry HDR revalidations in development mode to aid in 3rd party server race conditions
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 @@ -14,7 +14,7 @@ export function isCatchResponse(response: Response): boolean {
return response.headers.get("X-Remix-Catch") != null;
}

export function isErrorResponse(response: Response): boolean {
export function isErrorResponse(response: any): response is Response {
return response.headers.get("X-Remix-Error") != null;
}

Expand All @@ -28,7 +28,8 @@ export function isDeferredResponse(response: Response): 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 @@ -47,7 +48,24 @@ export async function fetchData(
: await request.formData();
}

let response = await fetch(url.href, init);
if (retry > 0) {
// Retry up to 3 times waiting 50, 250, 1250 ms
// between retries for a total of 1550 ms before giving up.
await new Promise((resolve) => setTimeout(resolve, 5 ** 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 < 3
) {
return fetchData(request, routeId, retry + 1);
}
throw error;
});

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