Keeping Useful Layout State Across Pages Router Navigation

I type lamp into the repair filter, open the workshop settings, and return to the list. The filter is empty. Nothing about visiting the settings should have changed what I was looking for.
Keeping that input around sounds like a small improvement. It becomes less helpful when the same value follows me into a different workspace, or survives a change of account. Both behaviours can come from the same component tree, with one badly chosen lifetime.
My example gives the sidebar filter a longer life than either page beneath it. It also gives that life an end. The useful question is which change should end it, rather than how much state I can persuade React to retain.
Decide What Belongs to the Layout
The repair list and workshop settings share a sidebar. Its filter belongs to the current user and workspace, so it should survive a move between those two pages.
The list also has a small temporary note. For this example, the note belongs only to that page visit. Leaving the page discards it, and the text beside the control says so. This is a deliberately limited scratch note, not an unsaved repair report masquerading as one.
I would give a valuable draft a different policy: save it, move its owner, or provide a deliberate departure flow. Keeping it accidentally because a component happened not to unmount is a poor substitute for deciding.
There is a third boundary. Switching workspaces or users must clear both controls. A filter from the previous workspace may merely be confusing; a note from the previous account is rather more difficult to explain.
I covered shared ownership in my article on lifting state up. A layout is a useful owner here because it spans the pages which need the filter. Moving the state further up into the application would give it a broader lifetime than the feature requires.
Keep the Component Tree Stable
Next's layout guidance for version 11 describes a getLayout convention. A page supplies a function which wraps its rendered content, and the custom App calls it.
The property is an arrangement between my pages and App. It is not a second routing system, and the name does not preserve anything by itself.
My TypeScript definitions make that arrangement visible:
import type { NextPage } from 'next';
import type { ReactElement, ReactNode } from 'react';
export type PageProps = {
user: string;
workspace: string;
};
export type PageWithLayout = NextPage<PageProps> & {
getLayout?: (page: ReactElement<PageProps>) => ReactNode;
};The App creates the page element and passes it to Component.getLayout when one is present. Both workspace pages return the same WorkspaceLayout component in the same place in the tree. React can therefore keep that component's filter state whilst replacing the page below it.
The repair list's note disappears when the settings page replaces the list. The sidebar does not disappear, so its filter remains. That difference is useful evidence that the state has the intended owners.
My custom App and Document article covers the wrappers themselves. This example keeps the shared layout in the App's rendered tree; wrapping each page inside an unrelated page‑specific component can change the tree above the layout and lose that continuity.
Make the Reset Boundary Explicit
Keeping the same layout type is only half the policy. React can also reuse the repair‑list component when a dynamic workspace parameter changes. Without an explicit boundary, both the filter and the temporary note may outlive the workspace they describe.
I use the user and workspace as the shared layout's key:
export function workspaceLayout(page: ReactElement<PageProps>) {
const { user, workspace } = page.props;
return (
<WorkspaceLayout
key={`${user}:${workspace}`}
user={user}
workspace={workspace}
>
{page}
</WorkspaceLayout>
);
}Moving between Repairs and Settings keeps that key. Changing either part creates a new layout subtree, which resets the sidebar and its children together.
A pathname key would reset the sidebar on every sibling navigation and recreate the original problem. A workspace‑only key would miss an account change at the same URL. I test that less convenient case directly rather than relying on logout to happen to remount the entire application.
The identity values come from the page's server‑provided props. In my lab, a fixed cookie chooses Alice or Bob so that the transition is easy to observe. That cookie is not authentication. A real application must use its validated session and authorised workspace membership; a React key neither proves identity nor prevents an API request.
Logging out takes the fixture to a different layout family. When the next user enters the workspace, the controls start empty. Revoking a session and clearing any external stores remain separate work in a real sign‑out flow.
Don't Accidentally Create a New Component Type
My deliberately broken example defines its layout component inside the getLayout function. Each time App renders, that function creates a new component type. The input starts empty again after navigation, even though the resulting HTML looks much the same.
Moving the component definition to module scope fixes that identity problem. Moving only the state variable would obscure it.
There is a subtle distinction here: the getLayout callback itself does not have to retain one function reference to preserve state. React reconciles the elements it returns. A fresh callback returning the same layout type, position, and key can still preserve that layout. A callback which invents a new component type cannot.
I keep the broken version in the acceptance suite and check that its input becomes empty after navigation. If I accidentally fix that version too, the assertion fails.
Include History and Focus in the Test
A successful click from Repairs to Settings is not enough. I enter a filter and note, move to Settings, go Back, and then go Forward. The filter remains throughout those sibling transitions. The page note is empty when the list returns, matching the warning beside it.
Browser history is not a collection of saved React state snapshots. Leaving the workspace layout entirely, returning from the public‑information page, or reloading creates fresh state. Direct entry into Settings starts with an empty sidebar too. Those are separate paths through the interface, and each belongs in the comparison.
I also switch the fixture user without changing the URL, then navigate through history. The server supplies the current identity. Going Back must not bring Alice's note into Bob's view merely because the URL appeared in Alice's history.
Preserved state does not guarantee useful keyboard focus. My destination headings have tabIndex={-1}, and the App moves focus to the heading after a route or identity change. Typing into the filter does not trigger that effect. The tests check document.activeElement, as well as the displayed values, so a visually correct transition cannot quietly leave focus on a removed control.
That is a focus check, not a claim about every screen reader's announcement. A real interface still needs its own keyboard and assistive‑technology review, especially if panels open without a route change or if the page has a more useful focus destination.
The final policy is quite small: preserve the sidebar within one user/workspace, discard the temporary page note on departure, and reset the subtree when its owner changes. I can explain each outcome before looking at the implementation. That is a more useful result than having a layout which remembers everything, including the things I wish it wouldn't.
Postscript
November 2026: I've added these layout checks to my shared Pages Router regression lab. They run against the September 2021 framework pins, Next.js 11.1.2, React 17.0.2, TypeScript 4.4.2, and Node 16.4.2, as well as the current Next.js 16.4.6 companion. Both runs use Chrome 157 through a modern test harness. The download keeps the unstable‑layout control, same‑URL account switch, history checks, and focus assertions; it doesn't turn the temporary note into saved draft storage or claim screen‑reader speech testing.