Skip to content
Closed
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
125 changes: 125 additions & 0 deletions packages/react-router-dom/__tests__/use-blocker-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("navigation blocking with useBlocker", () => {
let node: HTMLDivElement;
let router: Router;
let blocker: Blocker | null = null;
let secondBlocker: Blocker | null = null;
let root: ReactDOM.Root;

beforeEach(() => {
Expand Down Expand Up @@ -518,6 +519,130 @@ describe("navigation blocking with useBlocker", () => {
expect(h1?.textContent).toBe("Home");
});
});

describe.only("exiting from blocked state with 2 blockers", () => {
beforeEach(() => {
let initialEntries = ["/"];
let initialIndex = 0;
router = createMemoryRouter(
[
{
element: React.createElement(() => {
let b = useBlocker(true);
blocker = b;
let b2 = useBlocker(false);
secondBlocker = b2;
return (
<div>
<NavLink to="/">Home</NavLink>
<NavLink to="/about">About</NavLink>
{b.state === "blocked" && (
<div>
<button data-action="proceed" onClick={b.proceed}>
Proceed
</button>
<button data-action="reset" onClick={b.reset}>
Reset
</button>
</div>
)}
{b2.state === "blocked" && (
<div>
<button data-action="proceed-2" onClick={b2.proceed}>
Proceed 2
</button>
<button data-action="reset-2" onClick={b2.reset}>
Reset 2
</button>
</div>
)}
<Outlet />
</div>
);
}),
children: [
{
path: "/",
element: <h1>Home</h1>,
},
{
path: "/about",
element: <h1>About</h1>,
loader: slowLoader,
},
],
},
],
{
initialEntries,
initialIndex,
}
);
React.act(() => {
root = ReactDOM.createRoot(node);
root.render(<RouterProvider router={router} />);
});
});

afterEach(() => {
React.act(() => root.unmount());
});

it("gets a 'proceeding' blocker after proceeding navigation starts", async () => {
React.act(() => {
click(node.querySelector("a[href='/about']"));
});
React.act(() => {
click(node.querySelector("[data-action='proceed']"));
});
expect(blocker).toEqual({
state: "proceeding",
proceed: undefined,
reset: undefined,
location: expect.any(Object),
});
expect(secondBlocker).toEqual({
state: "unblocked",
proceed: undefined,
reset: undefined,
location: undefined,
});
});

it("gets an 'unblocked' blocker after proceeding navigation completes", async () => {
React.act(() => {
click(node.querySelector("a[href='/about']"));
});
await React.act(async () => {
click(node.querySelector("[data-action='proceed']"));
await sleep(LOADER_LATENCY_MS);
});
expect(blocker).toEqual({
state: "unblocked",
proceed: undefined,
reset: undefined,
location: undefined,
});
expect(secondBlocker).toEqual({
state: "unblocked",
proceed: undefined,
reset: undefined,
location: undefined,
});
});

it("navigates after proceeding navigation completes", async () => {
React.act(() => {
click(node.querySelector("a[href='/about']"));
});
await React.act(async () => {
click(node.querySelector("[data-action='proceed']"));
await sleep(LOADER_LATENCY_MS);
});
let h1 = node.querySelector("h1");
expect(h1?.textContent).toBe("About");
});
})
});

describe("on <Link replace> navigation", () => {
Expand Down
133 changes: 66 additions & 67 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,40 +1045,43 @@ export function createRouter(init: RouterInit): Router {
"createHashRouter and the user manually changes the URL."
);

let blockerKey = shouldBlockNavigation({
let blockerKeys = shouldBlockNavigation({
currentLocation: state.location,
nextLocation: location,
historyAction,
});

if (blockerKey && delta != null) {
if (blockerKeys.length > 0 && delta != null) {
// Restore the URL to match the current UI, but don't update router state
let nextHistoryUpdatePromise = new Promise<void>((resolve) => {
unblockBlockerHistoryUpdate = resolve;
});
init.history.go(delta * -1);

// Put the blocker into a blocked state
updateBlocker(blockerKey, {
state: "blocked",
location,
proceed() {
updateBlocker(blockerKey!, {
state: "proceeding",
proceed: undefined,
reset: undefined,
location,
});
// Re-do the same POP navigation we just blocked, after the url
// restoration is also complete. See:
// https://github.com/remix-run/react-router/issues/11613
nextHistoryUpdatePromise.then(() => init.history.go(delta));
},
reset() {
let blockers = new Map(state.blockers);
blockers.set(blockerKey!, IDLE_BLOCKER);
updateState({ blockers });
},
blockerKeys.forEach((blockerKey) => {
// Put the blocker into a blocked state
updateBlocker(blockerKey, {
state: "blocked",
location,
proceed() {
updateBlocker(blockerKey!, {
state: "proceeding",
proceed: undefined,
reset: undefined,
location,
});
// Re-do the same POP navigation we just blocked, after the url
// restoration is also complete. See:
// https://github.com/remix-run/react-router/issues/11613
// TODO : do we need a condition here to check that this is the last blocker
nextHistoryUpdatePromise.then(() => init.history.go(delta));
},
reset() {
let blockers = new Map(state.blockers);
blockers.set(blockerKey!, IDLE_BLOCKER);
updateState({ blockers });
},
});
});
return;
}
Expand Down Expand Up @@ -1409,33 +1412,36 @@ export function createRouter(init: RouterInit): Router {

let flushSync = (opts && opts.flushSync) === true;

let blockerKey = shouldBlockNavigation({
let blockerKeys = shouldBlockNavigation({
currentLocation,
nextLocation,
historyAction,
});

if (blockerKey) {
// Put the blocker into a blocked state
updateBlocker(blockerKey, {
state: "blocked",
location: nextLocation,
proceed() {
updateBlocker(blockerKey!, {
state: "proceeding",
proceed: undefined,
reset: undefined,
location: nextLocation,
});
// Send the same navigation through
navigate(to, opts);
},
reset() {
let blockers = new Map(state.blockers);
blockers.set(blockerKey!, IDLE_BLOCKER);
updateState({ blockers });
},
});
if (blockerKeys.length > 0) {
blockerKeys.forEach((blockerKey) => {
// Put the blocker into a blocked state
updateBlocker(blockerKey, {
state: "blocked",
location: nextLocation,
proceed() {
updateBlocker(blockerKey!, {
state: "proceeding",
proceed: undefined,
reset: undefined,
location: nextLocation,
});
// Send the same navigation through
// TODO Do we need to add a condition here to check this is the last blocker
navigate(to, opts);
},
reset() {
let blockers = new Map(state.blockers);
blockers.set(blockerKey!, IDLE_BLOCKER);
updateState({ blockers });
},
});
})
return;
}

Expand Down Expand Up @@ -3114,32 +3120,25 @@ export function createRouter(init: RouterInit): Router {
currentLocation: Location;
nextLocation: Location;
historyAction: HistoryAction;
}): string | undefined {
}): string[] {
if (blockerFunctions.size === 0) {
return;
}

// We ony support a single active blocker at the moment since we don't have
// any compelling use cases for multi-blocker yet
if (blockerFunctions.size > 1) {
warning(false, "A router only supports one blocker at a time");
return [];
}

let entries = Array.from(blockerFunctions.entries());
let [blockerKey, blockerFunction] = entries[entries.length - 1];
let blocker = state.blockers.get(blockerKey);

if (blocker && blocker.state === "proceeding") {
// If the blocker is currently proceeding, we don't need to re-check
// it and can let this navigation continue
return;
}
return Array.from(blockerFunctions.entries()).map(([blockerKey, blockerFunction]) => {
let blocker = state.blockers.get(blockerKey);
if (blocker && blocker.state === "proceeding") {
// If the blocker is currently proceeding, we don't need to re-check
// it and can let this navigation continue
return;
}

// At this point, we know we're unblocked/blocked so we need to check the
// user-provided blocker function
if (blockerFunction({ currentLocation, nextLocation, historyAction })) {
return blockerKey;
}
// At this point, we know we're unblocked/blocked so we need to check the
// user-provided blocker function
if (blockerFunction({ currentLocation, nextLocation, historyAction })) {
return blockerKey;
}
}).filter((blockerKey) => blockerKey !== undefined)
}

function handleNavigational404(pathname: string) {
Expand Down
Loading