Skip to content

Commit ccfdee6

Browse files
committed
fix(navigation): handle redirects by treating final response as GET result
1 parent a6de782 commit ccfdee6

2 files changed

Lines changed: 9 additions & 22 deletions

File tree

‎packages/void/src/pages/navigation-engine.ts‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -706,23 +706,13 @@ export async function createPageNavigationEngine(
706706
window.location.href = method === 'GET' ? url : response.url;
707707
return undefined;
708708
}
709-
commitTarget.fetchUrl = redirectUrl.pathname + redirectUrl.search;
709+
// The browser already followed the redirect: treat the final response as the GET
710+
// result for the destination instead of fetching it a second time.
711+
method = 'GET';
712+
fetchUrl = redirectUrl.pathname + redirectUrl.search;
713+
url = fetchUrl + redirectUrl.hash;
714+
commitTarget.fetchUrl = fetchUrl;
710715
commitTarget.hash = redirectUrl.hash;
711-
return loadNavigationShell(
712-
id,
713-
redirectUrl.pathname + redirectUrl.search + redirectUrl.hash,
714-
redirectUrl.pathname + redirectUrl.search,
715-
'GET',
716-
{
717-
preserveScroll: options.preserveScroll,
718-
preserveState: options.preserveState,
719-
replace: options.replace,
720-
viewTransition: options.viewTransition,
721-
},
722-
signal,
723-
commitTarget,
724-
deferredRegistry,
725-
);
726716
}
727717

728718
if (response && method !== 'GET' && !response.ok) {

‎packages/void/test/unit/pages-client.test.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,18 +1131,15 @@ describe('createVoidRouter other-origin visits', () => {
11311131

11321132
it('follows a same-host Capacitor action redirect in place', async () => {
11331133
const { router, fetchMock, applyUpdate } = await createRouter('capacitor://localhost');
1134-
const response = pageJson({});
1134+
const response = pageJson({ component: 'users', props: { users: [] } });
11351135
Object.defineProperties(response, {
11361136
redirected: { value: true },
11371137
url: { value: 'capacitor://localhost/users' },
11381138
});
11391139
fetchMock.mockResolvedValueOnce(response);
11401140
await router.visit('/submit', { method: 'POST' });
1141-
expect(fetchMock).toHaveBeenNthCalledWith(
1142-
2,
1143-
'/users',
1144-
expect.objectContaining({ method: 'GET' }),
1145-
);
1141+
// The redirect destination is rendered from the followed response, not fetched again.
1142+
expect(fetchMock).toHaveBeenCalledTimes(1);
11461143
expect(applyUpdate).toHaveBeenCalledTimes(1);
11471144
expect(window.location.href).toBeUndefined();
11481145
});

0 commit comments

Comments
 (0)