Repository navigation
fix(react-router): handle leading double slashes in static router encodeLocation() - #15805
Merged
brophdawg11 merged 1 commit intoNov 20, 2026
Conversation
…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
approved these changes
Nov 20, 2026
brophdawg11
left a comment
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15804
On the client,
createBrowserHistoryencodes locations viacreateBrowserURLImpl(packages/react-router/lib/router/history.ts), which prependsbasewhenPROTOCOL_RELATIVE_URL_REGEX.test(href)is true before callingnew URL(href, base)(#13510, #15176).On the server (
<ServerRouter>,<StaticRouterProvider>,<StaticRouter>, andcreateStaticRouter),encodeLocationinpackages/react-router/lib/dom/server.tsxcheckedABSOLUTE_URL_REGEX.test(href)and callednew URL(href)without a base URL:Because
ABSOLUTE_URL_REGEX(/^(?:[a-z][a-z0-9+.-]*:|[\\/]{2})/i) matches leading//and\\, any location starting with two slashes or backslashes was passed tonew URL(href)with no base, throwingTypeError: Invalid URL. For example, when an incoming SSR request has a leading double slash (GET http://localhost//orGET http://localhost//about),location.pathnameis"//"or"//about", so rendering<NavLink to="?tab=1">or<NavLink to="#section">resolvedpathagainstlocation.pathnameand callednavigator.encodeLocation(path), crashing the SSR render.This aligns
encodeLocationinpackages/react-router/lib/dom/server.tsxwithcreateBrowserURLImpl:Tests:
packages/react-router/__tests__/dom/data-static-router-test.tsx:<StaticRouterProvider>androuter.encodeLocation()with//and//aboutrequest URLs rendering query-only and hash-only<NavLink>s, plus mixed leading separators (\\about,/\about,\/about) inrouter.encodeLocation().packages/react-router/__tests__/dom/static-location-test.tsx:<StaticRouter>withlocation="//"andlocation="//about"rendering<NavLink to="?tab=1">.All 3 new tests fail on
main:and pass with this change. Also run locally on Windows 11 with Node 24.22.1:
pnpm test packages/react-router/(--maxWorkers=2, plusspecial-characters-test.tsxwith--testTimeout=30000): all 125 suites passed (3050 passed, 15 skipped)pnpm --filter react-router exec tsc --noEmit: cleanpnpm exec eslintandpnpm exec prettier --checkon changed files: cleanpnpm run changes:validate: validpnpm run --filter react-router buildand the standalone reproduction script from Static router encodeLocation() throws TypeError: Invalid URL on leading double-slash URLs during SSR #15804 againstdist/production/index.js:router.encodeLocation(),<StaticRouterProvider>, and<StaticRouter>all succeed and render active<NavLink>markupAI-assisted: drafted with an AI coding agent; the tests above were run locally.