Skip to content

fix(react-router): handle leading double slashes in static router encodeLocation() - #15805

Merged
brophdawg11 merged 1 commit into
remix-run:mainfrom
adityajai119:fix/static-router-encode-location-double-slash
Nov 20, 2026
Merged

brophdawg11 merged 1 commit into
remix-run:mainfrom
adityajai119:fix/static-router-encode-location-double-slash

Conversation

@adityajai119

Copy link
Copy Markdown
Contributor

Fixes #15804

On the client, createBrowserHistory encodes locations via createBrowserURLImpl (packages/react-router/lib/router/history.ts), which prepends base when PROTOCOL_RELATIVE_URL_REGEX.test(href) is true before calling new URL(href, base) (#13510, #15176).

On the server (<ServerRouter>, <StaticRouterProvider>, <StaticRouter>, and createStaticRouter), encodeLocation in packages/react-router/lib/dom/server.tsx checked ABSOLUTE_URL_REGEX.test(href) and called new URL(href) without a base URL:

let encoded = ABSOLUTE_URL_REGEX.test(href)
  ? new URL(href)
  : new URL(href, "http://localhost");

Because ABSOLUTE_URL_REGEX (/^(?:[a-z][a-z0-9+.-]*:|[\\/]{2})/i) matches leading // and \\, any location starting with two slashes or backslashes was passed to new URL(href) with no base, throwing TypeError: Invalid URL. For example, when an incoming SSR request has a leading double slash (GET http://localhost// or GET http://localhost//about), location.pathname is "//" or "//about", so rendering <NavLink to="?tab=1"> or <NavLink to="#section"> resolved path against location.pathname and called navigator.encodeLocation(path), crashing the SSR render.

This aligns encodeLocation in packages/react-router/lib/dom/server.tsx with createBrowserURLImpl:

function encodeLocation(to: To): Path {
  let base = "http://localhost";
  let href = typeof to === "string" ? to : createPath(to);
  // Treating this as a full URL will strip any trailing spaces so we need to
  // pre-encode them since they might be part of a matching splat param from
  // an ancestor route
  href = href.replace(/ $/, "%20");
  if (PROTOCOL_RELATIVE_URL_REGEX.test(href)) {
    href = base + href;
  }
  let encoded = new URL(href, base);
  return {
    pathname: encoded.pathname,
    search: encoded.search,
    hash: encoded.hash,
  };
}

Tests:

  • packages/react-router/__tests__/dom/data-static-router-test.tsx: <StaticRouterProvider> and router.encodeLocation() with // and //about request URLs rendering query-only and hash-only <NavLink>s, plus mixed leading separators (\\about, /\about, \/about) in router.encodeLocation().
  • packages/react-router/__tests__/dom/static-location-test.tsx: <StaticRouter> with location="//" and location="//about" rendering <NavLink to="?tab=1">.

All 3 new tests fail on main:

TypeError: Invalid URL: //?tab=1
Tests: 3 failed, 30 passed, 33 total

and pass with this change. Also run locally on Windows 11 with Node 24.22.1:

  • pnpm test packages/react-router/ (--maxWorkers=2, plus special-characters-test.tsx with --testTimeout=30000): all 125 suites passed (3050 passed, 15 skipped)
  • pnpm --filter react-router exec tsc --noEmit: clean
  • pnpm exec eslint and pnpm exec prettier --check on changed files: clean
  • pnpm run changes:validate: valid
  • pnpm run --filter react-router build and the standalone reproduction script from Static router encodeLocation() throws TypeError: Invalid URL on leading double-slash URLs during SSR #15804 against dist/production/index.js: router.encodeLocation(), <StaticRouterProvider>, and <StaticRouter> all succeed and render active <NavLink> markup

AI-assisted: drafted with an AI coding agent; the tests above were run locally.

…odeLocation()

On the client, createBrowserURLImpl prepends base when
PROTOCOL_RELATIVE_URL_REGEX matches href so new URL(href, base) does not
treat leading double slashes or backslashes as protocol-relative URLs.

Static router encodeLocation() in lib/dom/server.tsx checked
ABSOLUTE_URL_REGEX and called new URL(href) without a base URL, throwing
TypeError: Invalid URL on paths starting with // or \\ (such as
query-only or hash-only <NavLink>s rendered during SSR on a request URL
with a leading double slash).

Align encodeLocation() in lib/dom/server.tsx with createBrowserURLImpl.

Fixes remix-run#15804

@brophdawg11 brophdawg11 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.

Reviewed the current head. The static router URL encoding now matches the existing browser history behavior for leading slash/backslash pairs, preserving double-slash paths during SSR. The scoped fix and regression coverage have no blocking findings.

Authored by an AI agent on behalf of @brophdawg11

@brophdawg11
brophdawg11 merged commit e7c3549 into remix-run:main Nov 20, 2026
12 checks passed
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.

Static router encodeLocation() throws TypeError: Invalid URL on leading double-slash URLs during SSR

2 participants