Skip to content

fix(app-router): restore shallow pathname on history traversal - #2829

Open
GtechGovind wants to merge 5 commits into
cloudflare:mainfrom
GtechGovind:agent/preserve-shallow-history-pathname
Open

fix(app-router): restore shallow pathname on history traversal#2829
GtechGovind wants to merge 5 commits into
cloudflare:mainfrom
GtechGovind:agent/preserve-shallow-history-pathname

Conversation

@GtechGovind

Copy link
Copy Markdown

Summary

  • persist the destination URL on external history.pushState and history.replaceState entries
  • treat marked history traversal entries as shallow URL restores so App Router keeps the current rendered tree
  • synchronize usePathname and useSearchParams when popstate takes the same-route fast path
  • add Next.js-compatible browser back/forward regression coverage

Root cause

External History API calls updated Vinext's navigation-hook URL cache, but the App Router popstate fast path only restored history metadata and scroll position. It did not resynchronize the hooks when traversing back to the rendered route. The pushed entry also lacked a marker that would let forward traversal reuse the current tree instead of attempting an RSC navigation.

The new internal shallow URL metadata identifies those entries, allowing popstate to synchronize the URL-backed hooks while preserving the current App Router tree.

Fixes #1541.

Validation

  • ./node_modules/.bin/vp check
  • ./node_modules/.bin/vp test run tests/shims.test.ts -t 'preserves App Router history metadata when external history calls provide caller state'
  • PLAYWRIGHT_PROJECT=app-router ./node_modules/.bin/playwright test tests/e2e/app-router/advanced.spec.ts -g 'Shallow Routing' (4 passed, 1 existing fixme skipped)

@GtechGovind
GtechGovind marked this pull request as ready for review August 6, 2026 17:45

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ee5604e238

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/vinext/src/server/app-browser-entry.ts Outdated
@GtechGovind
GtechGovind marked this pull request as draft August 8, 2026 05:57
@GtechGovind
GtechGovind marked this pull request as ready for review August 8, 2026 06:42
@GtechGovind

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 126007f9e4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/vinext/src/server/app-browser-history-controller.ts Outdated
Comment thread packages/vinext/src/shims/navigation.ts Outdated
@GtechGovind
GtechGovind marked this pull request as draft August 8, 2026 11:34
@GtechGovind
GtechGovind marked this pull request as ready for review August 8, 2026 12:18

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e4bdabd84

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/vinext/src/server/app-history-state.ts
Comment thread packages/vinext/src/server/app-browser-entry.ts
Comment thread packages/vinext/src/server/app-history-state.ts
@GtechGovind
GtechGovind marked this pull request as draft August 9, 2026 06:13
@GtechGovind
GtechGovind marked this pull request as ready for review August 9, 2026 06:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f54ae1f078

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if (isSameAppRoutePopstateTarget(href)) {
notifyAppRouterTransitionStart(href, "traverse");
historyController.commitTraversalIndexFromHistoryState(event.state);
commitClientNavigationState();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore snapshots before taking the same-route shortcut

When traversal skips intermediate entries, the target URL can match the currently rendered route while its saved tree differs. For example, create a shallow /page entry, navigate through /other to a newly rendered /page, then call history.go(-2): this branch only synchronizes URL hooks and leaves the newer /page tree mounted instead of restoring the shallow entry's snapshot. Check for a restorable shallow snapshot before taking this pathname/search shortcut.

Useful? React with 👍 / 👎.


remember(options: { bfcacheVersion: number; historyIndex: number | null; state: TState }): void {
pruneAfter(historyIndex: number | null): void {
if (historyIndex === null) return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prune forward snapshots when the current index is unknown

Fresh evidence beyond the earlier retention fix: traversing to a metadata-less entry sets the current traversal index to null, so a subsequent shallow push calls this method with null and returns without pruning even though the browser has discarded the entire forward branch. Repeating Back to that metadata-less entry followed by pushState() leaves another unreachable durable React tree in memory each time, and general cache invalidation does not clear durable snapshots. Prune using explicit branch ownership even when the previous index is unknown.

AGENTS.md reference: AGENTS.md:L483-L483

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App Router: history.pushState with new pathname not reflected after back/forward

1 participant