Skip to content

fix(router-core): useBlocker navigation issues for 404 pages and external URLs #4917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jiji-hoon96
Copy link
Contributor

@jiji-hoon96 jiji-hoon96 commented Aug 11, 2025

Summary

Fixes two critical navigation issues with the useBlocker hook:

  1. 404 Page Navigation: Previously, when a user was on a 404 page with an active blocker, attempting to navigate to any other page would throw an error: "Error: No route found for location /foo"

  2. External URL ignoreBlocker: The ignoreBlocker option was not respected for external URLs, causing blockers to still prevent navigation even when explicitly ignored

Changes

404 Page Navigation Fix

  • Modified getLocation function in useBlocker.tsx to handle 404 routes gracefully
  • Added special handling for routes where foundRoute is undefined by returning routeId: '__notFound__'
  • Implemented logic to allow navigation from 404 pages to valid routes without blocking

External URL ignoreBlocker Fix

  • Modified navigate function in router.ts to respect ignoreBlocker option for external URLs
  • Added manual blocker checking for external URLs when ignoreBlocker is false
  • Made navigate function async to properly handle blocker promises

Test Coverage

  • Added comprehensive tests to verify both 404 page navigation and external URL behavior
  • Verified all existing tests pass (661 tests) to ensure no regressions

Technical Details

404 Page Navigation

  1. Root Cause: The blocker was trying to resolve 404 paths as valid routes, causing errors when getMatchedRoutes returned undefined for foundRoute.

  2. Solution: The getLocation function now:

    • Returns a special location object with routeId: '__notFound__' for unmatched routes
    • Allows navigation from 404 pages to valid routes by checking route IDs
    • Maintains normal blocking behavior for valid-to-valid route navigation

External URL ignoreBlocker

  1. Root Cause: External URL navigation bypassed the normal blocker flow and didn't check the ignoreBlocker option.

  2. Solution: The navigate function now:

    • Checks blockers manually for external URLs unless ignoreBlocker: true
    • Iterates through all registered blockers and awaits their blockerFn
    • Cancels navigation if any blocker returns true, proceeds if all allow
    • Preserves existing behavior for internal URLs

Breaking Changes

None. All existing functionality is preserved and the API remains unchanged.

Fixes

Copy link

nx-cloud bot commented Aug 11, 2025

View your CI Pipeline Execution ↗ for commit 4ddec20

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 5m 7s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 32s View ↗

☁️ Nx Cloud last updated this comment at 2025-08-11 12:34:06 UTC

Copy link

pkg-pr-new bot commented Aug 11, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@4917

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@4917

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@4917

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@4917

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@4917

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@4917

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@4917

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@4917

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@4917

@tanstack/react-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-plugin@4917

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@4917

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@4917

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@4917

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@4917

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@4917

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@4917

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@4917

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@4917

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@4917

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@4917

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@4917

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@4917

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@4917

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@4917

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@4917

@tanstack/solid-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-plugin@4917

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@4917

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@4917

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@4917

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@4917

@tanstack/start-server-functions-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-client@4917

@tanstack/start-server-functions-fetcher

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-fetcher@4917

@tanstack/start-server-functions-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-server@4917

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@4917

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@4917

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@4917

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@4917

commit: 4ddec20

@jiji-hoon96 jiji-hoon96 changed the title feat: allow navigation from 404 pages when blocker is active feat: fix useBlocker navigation issues for 404 pages and external URLs Aug 11, 2025
@jiji-hoon96 jiji-hoon96 changed the title feat: fix useBlocker navigation issues for 404 pages and external URLs fix(router-core): useBlocker navigation issues for 404 pages and external URLs Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant