[PR #468] [MERGED] fix: emit hashChangeStart/Complete and beforeHistoryChange router events #594

Closed
opened 2026-05-06 13:08:57 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/468
Author: @NathanDrake2406
Created: 3/11/2026
Status: Merged
Merged: 3/12/2026
Merged by: @james-elicx

Base: mainHead: fix/router-hash-beforehistorychange-events


📝 Commits (7)

  • 033e1d4 fix: emit hashChangeStart/Complete and beforeHistoryChange events
  • 5336ce3 fix: emit full URL in hash change events, not just fragment
  • 0276b52 fix: handle hash-only popstate and address review feedback
  • 3232c85 fix: pass { shallow } to routeChangeError, add goForward hash test
  • e318312 fix: include hash in popstate beforeHistoryChange URL, stabilize forward test
  • d05e0ac fix: strip basePath from full-path hash URLs, include hash in popstate event URLs
  • acce8f8 fix: update _lastPathnameAndSearch after beforePopState check

📊 Changes

3 files changed (+244 additions, -15 deletions)

View changed files

📝 packages/vinext/src/shims/router.ts (+96 -15)
📝 tests/e2e/pages-router/router-events.spec.ts (+123 -0)
📝 tests/fixtures/pages-basic/pages/router-events-test.tsx (+25 -0)

📄 Description

Summary

  • Hash-only navigation (router.push("#section")) now emits hashChangeStart before and hashChangeComplete after, matching Next.js behavior. Previously emitted zero events.
  • beforeHistoryChange now fires between routeChangeStart and history.pushState/replaceState on normal navigation. Previously never emitted.
  • All router events now pass { shallow } as the second argument, matching the Next.js contract.
  • Fixed in all 6 code paths: useRouter().push, useRouter().replace, Router.push, Router.replace, and the popstate handler.

Ported from Next.js event contract documented in: docs/02-pages/04-api-reference/03-functions/use-router.mdx

Test plan

  • E2E: beforeHistoryChange fires between routeChangeStart and routeChangeComplete on router.push()
  • E2E: hashChangeStart and hashChangeComplete fire on hash-only router.push("#section")
  • E2E: hashChangeStart fires before hashChangeComplete
  • E2E: hashChangeStart/Complete fire on router.replace("#section")
  • E2E: Hash-only navigation does NOT fire routeChangeStart/routeChangeComplete
  • Existing router event tests still pass (routeChangeStart/Complete ordering, Link clicks, multiple navigations)
  • Typecheck, lint, format all clean

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/468 **Author:** [@NathanDrake2406](https://github.com/NathanDrake2406) **Created:** 3/11/2026 **Status:** ✅ Merged **Merged:** 3/12/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `fix/router-hash-beforehistorychange-events` --- ### 📝 Commits (7) - [`033e1d4`](https://github.com/cloudflare/vinext/commit/033e1d4d255010353cdba86dc1e4bf89dd5bc3af) fix: emit hashChangeStart/Complete and beforeHistoryChange events - [`5336ce3`](https://github.com/cloudflare/vinext/commit/5336ce32363223dc1e545317e32f8fcfd2126e76) fix: emit full URL in hash change events, not just fragment - [`0276b52`](https://github.com/cloudflare/vinext/commit/0276b52d2f2833f733ae6b888ab77153a8662af8) fix: handle hash-only popstate and address review feedback - [`3232c85`](https://github.com/cloudflare/vinext/commit/3232c85313c13c7b0f42fd90c36ee17c6d8d17de) fix: pass { shallow } to routeChangeError, add goForward hash test - [`e318312`](https://github.com/cloudflare/vinext/commit/e318312039def6d0f0b089a928c1e4fef96aeddb) fix: include hash in popstate beforeHistoryChange URL, stabilize forward test - [`d05e0ac`](https://github.com/cloudflare/vinext/commit/d05e0ac8380b42481dd9a1d1d4b7c07c517a5ebd) fix: strip basePath from full-path hash URLs, include hash in popstate event URLs - [`acce8f8`](https://github.com/cloudflare/vinext/commit/acce8f835d85b8b143c350b23c2faefbb5451f9e) fix: update _lastPathnameAndSearch after beforePopState check ### 📊 Changes **3 files changed** (+244 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/shims/router.ts` (+96 -15) 📝 `tests/e2e/pages-router/router-events.spec.ts` (+123 -0) 📝 `tests/fixtures/pages-basic/pages/router-events-test.tsx` (+25 -0) </details> ### 📄 Description ## Summary - **Hash-only navigation** (`router.push("#section")`) now emits `hashChangeStart` before and `hashChangeComplete` after, matching Next.js behavior. Previously emitted zero events. - **`beforeHistoryChange`** now fires between `routeChangeStart` and `history.pushState`/`replaceState` on normal navigation. Previously never emitted. - All router events now pass `{ shallow }` as the second argument, matching the Next.js contract. - Fixed in all 6 code paths: `useRouter().push`, `useRouter().replace`, `Router.push`, `Router.replace`, and the popstate handler. Ported from Next.js event contract documented in: `docs/02-pages/04-api-reference/03-functions/use-router.mdx` ## Test plan - [x] E2E: `beforeHistoryChange` fires between `routeChangeStart` and `routeChangeComplete` on `router.push()` - [x] E2E: `hashChangeStart` and `hashChangeComplete` fire on hash-only `router.push("#section")` - [x] E2E: `hashChangeStart` fires before `hashChangeComplete` - [x] E2E: `hashChangeStart`/`Complete` fire on `router.replace("#section")` - [x] E2E: Hash-only navigation does NOT fire `routeChangeStart`/`routeChangeComplete` - [x] Existing router event tests still pass (routeChangeStart/Complete ordering, Link clicks, multiple navigations) - [x] Typecheck, lint, format all clean --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:08:57 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#594
No description provided.