-
Notifications
You must be signed in to change notification settings - Fork 414
fix(pages-router): normalize repeated URL slashes #3367
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
james-elicx
wants to merge
1
commit into
codex/fix-pages-object-as
from
codex/pages-router-url-normalization
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
packages/vinext/src/shims/internal/normalize-router-href.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { hasBasePath } from "../../utils/base-path.js"; | ||
| import { getWindowOrigin, isAbsoluteUrl } from "../url-utils.js"; | ||
|
|
||
| /** | ||
| * Warn about and normalize repeated path separators like Next.js `resolveHref`. | ||
| * The protocol separator is preserved and query strings are left untouched. | ||
| * | ||
| * Ported from Next.js: packages/next/src/client/resolve-href.ts | ||
| * https://github.com/vercel/next.js/blob/canary/packages/next/src/client/resolve-href.ts | ||
| */ | ||
| export function normalizeRouterHref(href: string, routePathname: string): string { | ||
| const protocol = href.match(/^[a-z][a-z0-9+.-]*:\/\//i)?.[0] ?? ""; | ||
| const withoutProtocol = protocol ? href.slice(protocol.length) : href; | ||
| if (!/(\/\/|\\)/.test(withoutProtocol.split("?", 1)[0] ?? "")) return href; | ||
|
|
||
| console.error( | ||
| `Invalid href '${href}' passed to next/router in page: '${routePathname}'. Repeated forward-slashes (//) or backslashes \\ are not valid in the href.`, | ||
| ); | ||
|
|
||
| const [pathname, ...query] = withoutProtocol.split("?"); | ||
| const normalizedPathname = pathname.replace(/\\/g, "/").replace(/\/\/+/g, "/"); | ||
| return protocol + normalizedPathname + (query[0] ? `?${query.join("?")}` : ""); | ||
| } | ||
|
|
||
| /** Resolve a Pages Router href against the route state used by Next.js Link. */ | ||
| export function resolvePagesRouterHref( | ||
| href: string, | ||
| router: { pathname: string; asPath: string }, | ||
| basePath: string, | ||
| ): string { | ||
| const normalized = normalizeRouterHref(href, router.pathname); | ||
|
|
||
| if (isAbsoluteUrl(normalized)) { | ||
| const origin = getWindowOrigin(); | ||
| if (!origin) return normalized; | ||
| try { | ||
| const target = new URL(normalized, origin); | ||
| if ( | ||
| target.origin !== origin || | ||
| (basePath !== "" && !hasBasePath(target.pathname, basePath)) | ||
| ) { | ||
| return normalized; | ||
| } | ||
| } catch { | ||
| return normalized; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| const base = new URL(normalized.startsWith("#") ? router.asPath : router.pathname, "http://n"); | ||
| const resolved = new URL(normalized, base); | ||
| return resolved.origin === base.origin | ||
| ? resolved.href.slice(resolved.origin.length) | ||
| : resolved.href; | ||
| } catch { | ||
| return normalized; | ||
| } | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.