Skip to content

Commit 766f07d

Browse files
authored
Preserve view transitions through navigation redirects (#11925)
1 parent 5651176 commit 766f07d

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

.changeset/hot-insects-rescue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Preserve view transition through redirects

packages/router/__tests__/view-transition-test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IDLE_NAVIGATION } from "../router";
22
import { cleanup, setup } from "./utils/data-router-setup";
3+
import { createFormData } from "./utils/utils";
34

45
describe("view transitions", () => {
56
// Detect any failures inside the router navigate code
@@ -136,4 +137,39 @@ describe("view transitions", () => {
136137
unsubscribe();
137138
t.router.dispose();
138139
});
140+
141+
it("preserves pending view transitions through redirects", async () => {
142+
let t = setup({
143+
routes: [
144+
{ path: "/" },
145+
{ id: "a", path: "/a", action: true },
146+
{ path: "/b" },
147+
],
148+
});
149+
let spy = jest.fn();
150+
let unsubscribe = t.router.subscribe(spy);
151+
152+
let A = await t.navigate("/a", {
153+
formMethod: "post",
154+
formData: createFormData({}),
155+
unstable_viewTransition: true,
156+
});
157+
158+
await A.actions.a.redirect("/b");
159+
expect(spy).toHaveBeenLastCalledWith(
160+
expect.objectContaining({
161+
navigation: IDLE_NAVIGATION,
162+
location: expect.objectContaining({ pathname: "/b" }),
163+
}),
164+
expect.objectContaining({
165+
unstable_viewTransitionOpts: {
166+
currentLocation: expect.objectContaining({ pathname: "/" }),
167+
nextLocation: expect.objectContaining({ pathname: "/b" }),
168+
},
169+
})
170+
);
171+
172+
unsubscribe();
173+
t.router.dispose();
174+
});
139175
});

packages/router/router.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ export function createRouter(init: RouterInit): Router {
17841784
);
17851785
replace = location === state.location.pathname + state.location.search;
17861786
}
1787-
await startRedirectNavigation(request, result, {
1787+
await startRedirectNavigation(request, result, true, {
17881788
submission,
17891789
replace,
17901790
});
@@ -2037,7 +2037,7 @@ export function createRouter(init: RouterInit): Router {
20372037
revalidatingFetchers[redirect.idx - matchesToLoad.length].key;
20382038
fetchRedirectIds.add(fetcherKey);
20392039
}
2040-
await startRedirectNavigation(request, redirect.result, {
2040+
await startRedirectNavigation(request, redirect.result, true, {
20412041
replace,
20422042
});
20432043
return { shortCircuited: true };
@@ -2333,7 +2333,7 @@ export function createRouter(init: RouterInit): Router {
23332333
} else {
23342334
fetchRedirectIds.add(key);
23352335
updateFetcherState(key, getLoadingFetcher(submission));
2336-
return startRedirectNavigation(fetchRequest, actionResult, {
2336+
return startRedirectNavigation(fetchRequest, actionResult, false, {
23372337
fetcherSubmission: submission,
23382338
});
23392339
}
@@ -2454,7 +2454,11 @@ export function createRouter(init: RouterInit): Router {
24542454
revalidatingFetchers[redirect.idx - matchesToLoad.length].key;
24552455
fetchRedirectIds.add(fetcherKey);
24562456
}
2457-
return startRedirectNavigation(revalidationRequest, redirect.result);
2457+
return startRedirectNavigation(
2458+
revalidationRequest,
2459+
redirect.result,
2460+
false
2461+
);
24582462
}
24592463

24602464
// Process and commit output from loaders
@@ -2615,7 +2619,7 @@ export function createRouter(init: RouterInit): Router {
26152619
return;
26162620
} else {
26172621
fetchRedirectIds.add(key);
2618-
await startRedirectNavigation(fetchRequest, result);
2622+
await startRedirectNavigation(fetchRequest, result, false);
26192623
return;
26202624
}
26212625
}
@@ -2654,6 +2658,7 @@ export function createRouter(init: RouterInit): Router {
26542658
async function startRedirectNavigation(
26552659
request: Request,
26562660
redirect: RedirectResult,
2661+
isNavigation: boolean,
26572662
{
26582663
submission,
26592664
fetcherSubmission,
@@ -2740,8 +2745,11 @@ export function createRouter(init: RouterInit): Router {
27402745
...activeSubmission,
27412746
formAction: location,
27422747
},
2743-
// Preserve this flag across redirects
2748+
// Preserve these flags across redirects
27442749
preventScrollReset: pendingPreventScrollReset,
2750+
enableViewTransition: isNavigation
2751+
? pendingViewTransitionEnabled
2752+
: undefined,
27452753
});
27462754
} else {
27472755
// If we have a navigation submission, we will preserve it through the
@@ -2754,8 +2762,11 @@ export function createRouter(init: RouterInit): Router {
27542762
overrideNavigation,
27552763
// Send fetcher submissions through for shouldRevalidate
27562764
fetcherSubmission,
2757-
// Preserve this flag across redirects
2765+
// Preserve these flags across redirects
27582766
preventScrollReset: pendingPreventScrollReset,
2767+
enableViewTransition: isNavigation
2768+
? pendingViewTransitionEnabled
2769+
: undefined,
27592770
});
27602771
}
27612772
}

0 commit comments

Comments
 (0)