-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix canonical url for dynamic routes (#49512)
The `pathname` in app-render was actually "page" (e.g. `/blog/[slug]`), with special url conventions. Instead we should use actual request url as pathname. Rename previous `pathname` arg to `pagePath` for better readability Also improved the url validation
- Loading branch information
Showing
5 changed files
with
43 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,11 +1,18 @@ | ||
const DUMMY_ORIGIN = 'http://n' | ||
const INVALID_URL_MESSAGE = 'Invalid request URL' | ||
|
||
export function validateURL(url: string | undefined): string { | ||
if (!url) { | ||
throw new Error('Invalid request URL') | ||
throw new Error(INVALID_URL_MESSAGE) | ||
} | ||
try { | ||
new URL(url, 'http://n') | ||
const parsed = new URL(url, DUMMY_ORIGIN) | ||
// Avoid origin change by extra slashes in pathname | ||
if (parsed.origin !== DUMMY_ORIGIN) { | ||
throw new Error(INVALID_URL_MESSAGE) | ||
} | ||
return url | ||
} catch { | ||
throw new Error('Invalid request URL') | ||
throw new Error(INVALID_URL_MESSAGE) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/metadata/app/alternates/child/[slug]/page.tsx
This file contains 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,3 @@ | ||
export default function Page() { | ||
return 'alternate-child-dynamic-slug' | ||
} |
This file contains 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 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,13 @@ | ||
import { validateURL } from 'next/dist/server/app-render/validate-url' | ||
|
||
describe('validateUrl', () => { | ||
it('should return valid pathname', () => { | ||
expect(validateURL('/')).toBe('/') | ||
expect(validateURL('/abc')).toBe('/abc') | ||
}) | ||
|
||
it('should throw for invalid pathname', () => { | ||
expect(() => validateURL('//**y/\\')).toThrow() | ||
expect(() => validateURL('//google.com')).toThrow() | ||
}) | ||
}) |