fix(embed-shims): soft-navigate in next/navigation router fallback#1448
Conversation
The unregistered `useRouter().push/replace` fallback used `window.location.assign/replace`, so a same-origin `?search=` write from `useApiParams` hard-reloaded the document. Route push/replace through the History API (`pushState`/`replaceState` + synthetic `popstate`) for same-origin URLs so query-param writes stay SPA; cross-origin/malformed hrefs still fall back to a real `window.location` navigation. Add regression tests covering fallback push/replace, cross-origin and malformed fallbacks, back/forward, and the registered host-router path.
|
Warning Review limit reached
Next review available in: 39 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe ChangesNavigation fallback behavior
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Application
participant NextNavigationShim
participant HistoryAPI
participant Window
Application->>NextNavigationShim: call router.push or router.replace
NextNavigationShim->>HistoryAPI: update same-origin URL
NextNavigationShim->>Window: dispatch synthetic popstate
NextNavigationShim->>Window: assign cross-origin or malformed URL
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@openframe-frontend-core/src/embed-shims/next-navigation.tsx`:
- Around line 102-134: Update softNavigate so cross-origin and malformed href
fallbacks use window.location.replace when mode is 'replace' and
window.location.assign otherwise, preserving the existing same-origin behavior.
Update the next-navigation tests to expect replace for cross-origin replace
calls and add coverage for malformed hrefs invoked through replace().
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 5b3316da-5bae-4234-90c7-35c5edea11a7
📒 Files selected for processing (2)
openframe-frontend-core/src/embed-shims/__tests__/next-navigation.test.tsopenframe-frontend-core/src/embed-shims/next-navigation.tsx
…n fallback Cross-origin and malformed hrefs ignored the push/replace mode and always called window.location.assign, so useRouter().replace() left a back-button entry — contradicting the replace contract. Route replace-mode fallbacks through window.location.replace. Add test coverage for cross-origin push, and malformed href via replace().
Проблема
В шиме
src/embed-shims/next-navigation.tsxнезарегистрированный фолбэкuseRouter().push/replaceиспользовалwindow.location.assign/replace. Из-за этого same-origin запись?search=(например, изuseApiParams) вызывала полную перезагрузку документа вместо SPA-навигации — в том числе когда реальный роутер не зарегистрирован либо регистрация попала в другой инстанс модуля (дубликат библиотеки / ESM-CJS dual package).Решение
softNavigate()роутит same-origin push/replace через History API (pushState/replaceState+ синтетическийpopstate), поэтому запись query-параметров остаётся SPA и подписчики этого файла (usePathname/useSearchParams) ре-рендерятся. Cross-origin и malformed href по-прежнему уходят в реальнуюwindow.location-навигацию — единственно корректное поведение там.Тесты
Добавлены регрессионные тесты (
__tests__/next-navigation.test.ts), покрывающие фолбэк push/replace, cross-origin и malformed фолбэки, back/forward и путь с зарегистрированным host-роутером. Все 6 проходят.Summary by CodeRabbit