Skip to content

Commit c8db245

Browse files
n8agrinbrophdawg11
andauthored
Omit search part of path when detecting if the route changed after downloading asset bundles (#6707)
Co-authored-by: Matt Brophy <matt@brophy.org>
1 parent 0eff92c commit c8db245

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

.changeset/fix-reload-loops.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@remix-run/react": patch
3+
"@remix-run/server-runtime": patch
4+
---
5+
6+
Fix reload loops in scenarios where CDNs ignore search params

packages/remix-react/browser.tsx

+10-8
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,19 @@ export function RemixBrowser(_props: RemixBrowserProps): ReactElement {
181181
},
182182
});
183183

184-
// Hard reload if the URL we tried to load is not the current URL.
185-
// This is usually the result of 2 rapid backwards/forward clicks from an
184+
// Hard reload if the path we tried to load is not the current path.
185+
// This is usually the result of 2 rapid back/forward clicks from an
186186
// external site into a Remix app, where we initially start the load for
187187
// one URL and while the JS chunks are loading a second forward click moves
188-
// us to a new URL
189-
let initialUrl = window.__remixContext.url;
190-
let hydratedUrl = window.location.pathname + window.location.search;
191-
if (initialUrl !== hydratedUrl) {
188+
// us to a new URL. Avoid comparing search params because of CDNs which
189+
// can be configured to ignore certain params and only pathname is relevant
190+
// towards determining the route matches.
191+
let initialPathname = window.__remixContext.url;
192+
let hydratedPathname = window.location.pathname;
193+
if (initialPathname !== hydratedPathname) {
192194
let errorMsg =
193-
`Initial URL (${initialUrl}) does not match URL at time of hydration ` +
194-
`(${hydratedUrl}), reloading page...`;
195+
`Initial URL (${initialPathname}) does not match URL at time of hydration ` +
196+
`(${hydratedPathname}), reloading page...`;
195197
console.error(errorMsg);
196198
window.location.reload();
197199
}

packages/remix-server-runtime/server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ async function handleDocumentRequestRR(
289289
routeModules: createEntryRouteModules(build.routes),
290290
staticHandlerContext: context,
291291
serverHandoffString: createServerHandoffString({
292-
url: context.location.pathname + context.location.search,
292+
url: context.location.pathname,
293293
state: {
294294
loaderData: context.loaderData,
295295
actionData: context.actionData,
@@ -335,7 +335,7 @@ async function handleDocumentRequestRR(
335335
...entryContext,
336336
staticHandlerContext: context,
337337
serverHandoffString: createServerHandoffString({
338-
url: context.location.pathname + context.location.search,
338+
url: context.location.pathname,
339339
state: {
340340
loaderData: context.loaderData,
341341
actionData: context.actionData,

0 commit comments

Comments
 (0)