Skip to content

refactor(router-core): redirect skips creating a URL object if href is not a string - #4986

Merged
Sheraff merged 1 commit into
mainfrom
refactor-router-core-redirect-skip-url-object
Aug 18, 2025
Merged

refactor(router-core): redirect skips creating a URL object if href is not a string#4986
Sheraff merged 1 commit into
mainfrom
refactor-router-core-redirect-skip-url-object

Conversation

@Sheraff

@SheraffSheraff commented Aug 17, 2025

Copy link
Copy Markdown
Collaborator

Creating a URL object is a little expensive, and we know it's going to throw if href is not a string. So we can skip it entirely in that case (which is probably most cases?).

Summary by CodeRabbit

  • Bug Fixes
    • Improved redirect handling to avoid errors when a URL is missing or invalid.
    • Added safeguards to prevent runtime crashes when redirect data is null.
    • Enhanced URL parsing reliability during redirects.
    • No changes to public APIs.

@coderabbitai

coderabbitaiBot commented Aug 17, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The redirect() function updates its URL handling to guard on string href and constructs URLs directly. parseRedirect() adds a null check before accessing properties. No exported/public signatures changed.

Changes

Cohort / File(s)Summary of Changes
Redirect utilities
packages/router-core/src/redirect.ts
redirect(): run reload check only when href is a string; use new URL(opts.href) instead of string interpolation. parseRedirect(): add non-null guard before reading isSerializedRedirect. No public API signature changes.

Sequence Diagram(s)

sequenceDiagram
participant Caller
participant redirect
participant URL
Caller->>redirect: redirect(opts)
alt opts.href is string
redirect->>URL: new URL(opts.href)
URL-->>redirect: URL instance
redirect-->>Caller: proceed with reload logic
else
redirect-->>Caller: skip URL construction/reload check
end
Loading
sequenceDiagram
participant Caller
participant parseRedirect
Caller->>parseRedirect: parseRedirect(obj)
alt obj is non-null and has isSerializedRedirect
parseRedirect-->>Caller: parsed redirect
else
parseRedirect-->>Caller: safe fallback (no property access)
end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

package: router-core

Poem

A bunny bounds through routes so bright,
Checks for nulls to dodge a fright.
If href’s string, we hop to URL,
Otherwise, we pause a spell.
With tidy guards and cleaner sight—
Redirects now land just right. 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor-router-core-redirect-skip-url-object

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@nx-cloud

nx-cloudBot commented Aug 17, 2025

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit 8ce3e79

CommandStatusDurationResult
nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded4m 37sView ↗
nx run-many --target=build --exclude=examples/*...✅ Succeeded1m 32sView ↗

☁️ Nx Cloud last updated this comment at 2025-08-17 20:04:19 UTC

@pkg-pr-new

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

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

@tanstack/directive-functions-plugin

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

@tanstack/eslint-plugin-router

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

@tanstack/history

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

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

@tanstack/react-start-client

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

@tanstack/react-start-plugin

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

@tanstack/react-start-server

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

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/server-functions-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-start

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

@tanstack/solid-start-client

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

@tanstack/solid-start-plugin

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

@tanstack/solid-start-server

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

@tanstack/start-client-core

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

@tanstack/start-plugin-core

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

@tanstack/start-server-core

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

@tanstack/start-server-functions-client

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

@tanstack/start-server-functions-fetcher

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

@tanstack/start-server-functions-server

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

@tanstack/start-storage-context

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

@tanstack/valibot-adapter

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

@tanstack/virtual-file-routes

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

@tanstack/zod-adapter

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

commit: 8ce3e79

@coderabbitaicoderabbitaiBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/router-core/src/redirect.ts (1)

68-73: Good guard on href type; consider avoiding try/catch cost with a scheme check

The string guard addresses the runtime error and avoids constructing URL objects for non-strings. If you'd like to further reduce cost for string hrefs, you can skip new URL entirely by checking for an absolute scheme via a regex. This keeps behavior consistent (protocol-relative URLs like // still won’t set reloadDocument, same as the current try/catch path).

- if (!opts.reloadDocument && typeof opts.href === 'string') {- try {- new URL(opts.href)- opts.reloadDocument = true- } catch {}- }+ if (!opts.reloadDocument && typeof opts.href === 'string') {+ // Fast-path: mark as document reload only for absolute URLs (scheme:...).+ if (/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(opts.href)) {+ opts.reloadDocument = true+ }+ }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between fa3f8fa and 8ce3e79.

📒 Files selected for processing (1)
  • packages/router-core/src/redirect.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Preview
  • GitHub Check: Test
🔇 Additional comments (1)
packages/router-core/src/redirect.ts (1)

106-111: Null/object guard prevents NPE in parseRedirect

The additional null check before accessing isSerializedRedirect is correct and avoids runtime errors when null is passed.

@Sheraff
Sheraff merged commit 2c1f8b9 into mainAug 18, 2025
6 checks passed
@Sheraff
Sheraff deleted the refactor-router-core-redirect-skip-url-object branch August 18, 2025 07:55
Sign up for freeto 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

@Sheraff