-
Notifications
You must be signed in to change notification settings - Fork 28.7k
fix: support both decoded and encoded url requests of conventioned files #56187
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
Merged
huozhi
merged 7 commits into
vercel:canary
from
omarmciver:issues/54008-Nginx-decoded-URIs-breaks-slugs-client-side
Oct 2, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac4cf54
Test if we have a path match if we encode the slug delimiters: [ and ]
omarmciver 687898c
encode the url and re-retrieve, add test
huozhi 0981f7a
Merge branch 'canary' into issues/54008-Nginx-decoded-URIs-breaks-slu…
huozhi 122843c
Merge branch 'canary' into issues/54008-Nginx-decoded-URIs-breaks-slu…
kodiakhq[bot] 49c9869
Merge branch 'canary' into issues/54008-Nginx-decoded-URIs-breaks-slu…
huozhi de8c7c7
Merge branch 'canary' into issues/54008-Nginx-decoded-URIs-breaks-slu…
huozhi 4bfe967
Merge branch 'canary' into issues/54008-Nginx-decoded-URIs-breaks-slu…
ijjk 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
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,78 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { renderViaHTTP } from 'next-test-utils' | ||
import cheerio from 'cheerio' | ||
import webdriver from 'next-webdriver' | ||
import { createNextDescribe } from 'e2e-utils' | ||
|
||
describe('Dynamic Route Interpolation', () => { | ||
let next: NextInstance | ||
|
||
beforeAll(async () => { | ||
next = await createNext({ | ||
files: { | ||
'pages/blog/[slug].js': ` | ||
import Link from "next/link" | ||
import { useRouter } from "next/router" | ||
|
||
export function getServerSideProps({ params }) { | ||
return { props: { slug: params.slug, now: Date.now() } } | ||
} | ||
createNextDescribe( | ||
'Dynamic Route Interpolation', | ||
{ | ||
files: __dirname, | ||
}, | ||
({ next, isNextStart }) => { | ||
it('should work', async () => { | ||
const $ = await next.render$('/blog/a') | ||
expect($('#slug').text()).toBe('a') | ||
}) | ||
|
||
export default function Page(props) { | ||
const router = useRouter() | ||
return ( | ||
<> | ||
<p id="slug">{props.slug}</p> | ||
<Link id="now" href={router.asPath}> | ||
{props.now} | ||
</Link> | ||
</> | ||
) | ||
} | ||
`, | ||
it('should work with parameter itself', async () => { | ||
const $ = await next.render$('/blog/[slug]') | ||
expect($('#slug').text()).toBe('[slug]') | ||
}) | ||
|
||
'pages/api/dynamic/[slug].js': ` | ||
export default function Page(req, res) { | ||
const { slug } = req.query | ||
res.end('slug: ' + slug) | ||
} | ||
`, | ||
}, | ||
dependencies: {}, | ||
it('should work with brackets', async () => { | ||
const $ = await next.render$('/blog/[abc]') | ||
expect($('#slug').text()).toBe('[abc]') | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('should work', async () => { | ||
const html = await renderViaHTTP(next.url, '/blog/a') | ||
const $ = cheerio.load(html) | ||
expect($('#slug').text()).toBe('a') | ||
}) | ||
it('should work with parameter itself in API routes', async () => { | ||
const text = await next.render('/api/dynamic/[slug]') | ||
expect(text).toBe('slug: [slug]') | ||
}) | ||
|
||
it('should work with parameter itself', async () => { | ||
const html = await renderViaHTTP(next.url, '/blog/[slug]') | ||
const $ = cheerio.load(html) | ||
expect($('#slug').text()).toBe('[slug]') | ||
}) | ||
it('should work with brackets in API routes', async () => { | ||
const text = await next.render('/api/dynamic/[abc]') | ||
expect(text).toBe('slug: [abc]') | ||
}) | ||
|
||
it('should work with brackets', async () => { | ||
const html = await renderViaHTTP(next.url, '/blog/[abc]') | ||
const $ = cheerio.load(html) | ||
expect($('#slug').text()).toBe('[abc]') | ||
}) | ||
it('should bust data cache', async () => { | ||
const browser = await next.browser('/blog/login') | ||
await browser.elementById('now').click() // fetch data once | ||
const text = await browser.elementById('now').text() | ||
await browser.elementById('now').click() // fetch data again | ||
await browser.waitForElementByCss(`#now:not(:text("${text}"))`) | ||
await browser.close() | ||
}) | ||
|
||
it('should work with parameter itself in API routes', async () => { | ||
const text = await renderViaHTTP(next.url, '/api/dynamic/[slug]') | ||
expect(text).toBe('slug: [slug]') | ||
}) | ||
it('should bust data cache with symbol', async () => { | ||
const browser = await next.browser('/blog/@login') | ||
await browser.elementById('now').click() // fetch data once | ||
const text = await browser.elementById('now').text() | ||
await browser.elementById('now').click() // fetch data again | ||
await browser.waitForElementByCss(`#now:not(:text("${text}"))`) | ||
await browser.close() | ||
}) | ||
|
||
it('should work with brackets in API routes', async () => { | ||
const text = await renderViaHTTP(next.url, '/api/dynamic/[abc]') | ||
expect(text).toBe('slug: [abc]') | ||
}) | ||
if (isNextStart) { | ||
it('should support both encoded and decoded nextjs reserved path convention characters in path', async () => { | ||
const $ = await next.render$('/blog/123') | ||
let pagePathScriptSrc | ||
for (const script of $('script').toArray()) { | ||
const { src } = script.attribs | ||
if (src.includes('slug') && src.includes('pages/blog')) { | ||
pagePathScriptSrc = src | ||
break | ||
} | ||
} | ||
|
||
it('should bust data cache', async () => { | ||
const browser = await webdriver(next.url, '/blog/login') | ||
await browser.elementById('now').click() // fetch data once | ||
const text = await browser.elementById('now').text() | ||
await browser.elementById('now').click() // fetch data again | ||
await browser.waitForElementByCss(`#now:not(:text("${text}"))`) | ||
await browser.close() | ||
}) | ||
// e.g. /_next/static/chunks/pages/blog/%5Bslug%5D-3d2fedc300f04305.js | ||
const { status: encodedPathReqStatus } = await next.fetch( | ||
pagePathScriptSrc | ||
) | ||
// e.g. /_next/static/chunks/pages/blog/[slug]-3d2fedc300f04305.js | ||
const { status: decodedPathReqStatus } = await next.fetch( | ||
decodeURI(pagePathScriptSrc) | ||
) | ||
|
||
it('should bust data cache with symbol', async () => { | ||
const browser = await webdriver(next.url, '/blog/@login') | ||
await browser.elementById('now').click() // fetch data once | ||
const text = await browser.elementById('now').text() | ||
await browser.elementById('now').click() // fetch data again | ||
await browser.waitForElementByCss(`#now:not(:text("${text}"))`) | ||
await browser.close() | ||
}) | ||
}) | ||
expect(encodedPathReqStatus).toBe(200) | ||
expect(decodedPathReqStatus).toBe(200) | ||
}) | ||
} | ||
} | ||
) |
4 changes: 4 additions & 0 deletions
4
test/e2e/dynamic-route-interpolation/pages/api/dynamic/[slug].js
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,4 @@ | ||
export default function Page(req, res) { | ||
const { slug } = req.query | ||
res.end('slug: ' + slug) | ||
} |
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,18 @@ | ||
import Link from 'next/link' | ||
import { useRouter } from 'next/router' | ||
|
||
export function getServerSideProps({ params }) { | ||
return { props: { slug: params.slug, now: Date.now() } } | ||
} | ||
|
||
export default function Page(props) { | ||
const router = useRouter() | ||
return ( | ||
<> | ||
<p id="slug">{props.slug}</p> | ||
<Link id="now" href={router.asPath}> | ||
{props.now} | ||
</Link> | ||
</> | ||
) | ||
} |
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.