Skip to content
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

Support colons in Middleware NextResponse.rewrite path #32543

Merged
merged 25 commits into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a17a153
Add failing colon rewrite test
Dec 15, 2021
56a79c4
add test fixture
Dec 15, 2021
4e3f97b
better colon rewrite tests
Dec 15, 2021
eda9dfb
middleware rewrite colon tests with query parameters
Dec 15, 2021
39545b7
fix #31523
Dec 15, 2021
7fbece7
hack around prepareDestination to skip compiling x-middleware-rewrite
Dec 15, 2021
02c6e75
obviate prepareDestination for x-middleware-rewrite handling
Dec 15, 2021
3b3e89a
Merge branch 'canary' into support-colons-in-rewrite-paths
Dec 15, 2021
a3c134a
don't clobber rewrite query data
Dec 15, 2021
a5bb9ad
omit redundant type
Dec 15, 2021
8cce34b
Merge branch 'canary' into support-colons-in-rewrite-paths
Dec 19, 2021
80476d2
Merge branch 'canary' into support-colons-in-rewrite-paths
Dec 22, 2021
12820c6
Merge branch 'canary' into support-colons-in-rewrite-paths
Jan 5, 2022
323d03b
Merge branch 'canary' into support-colons-in-rewrite-paths
Jan 12, 2022
fbfa8d4
Merge branch 'canary' into support-colons-in-rewrite-paths
Feb 2, 2022
ab229f0
catch up to main
Feb 2, 2022
8788b63
Merge branch 'canary' into support-colons-in-rewrite-paths
Feb 2, 2022
7061517
Merge branch 'canary' into support-colons-in-rewrite-paths
Feb 4, 2022
f76f946
Merge branch 'canary' into support-colons-in-rewrite-paths
Feb 9, 2022
faa1135
Merge branch 'canary' into support-colons-in-rewrite-paths
Feb 17, 2022
31dd727
Merge branch 'canary' into support-colons-in-rewrite-paths
Feb 22, 2022
ba80dcb
Merge branch 'canary' into support-colons-in-rewrite-paths
Feb 28, 2022
6911783
Merge branch 'canary' into support-colons-in-rewrite-paths
Mar 2, 2022
aef1075
It looks like newUrl should contain only pathname
Mar 2, 2022
c3c6008
Merge branch 'canary' into support-colons-in-rewrite-paths
ijjk Mar 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/next/server/next-server.ts
Expand Up @@ -48,7 +48,7 @@ import { getExtension, serveStatic } from './serve-static'
import { ParsedUrlQuery } from 'querystring'
import { apiResolver } from './api-utils/node'
import { RenderOpts, renderToHTML } from './render'
import { ParsedUrl } from '../shared/lib/router/utils/parse-url'
import { ParsedUrl, parseUrl } from '../shared/lib/router/utils/parse-url'
import * as Log from '../build/output/log'

import BaseServer, {
Expand Down Expand Up @@ -1120,11 +1120,11 @@ export default class NextNodeServer extends BaseServer {
const rewritePath = result.response.headers.get(
'x-middleware-rewrite'
)!
const { newUrl, parsedDestination } = prepareDestination({
appendParamsToQuery: false,
destination: rewritePath,
params: _params,
const parsedDestination = parseUrl(rewritePath)
const newUrl = formatUrl({
...parsedDestination,
query: {},
search: '',
})
klarstrup marked this conversation as resolved.
Show resolved Hide resolved

// TODO: remove after next minor version current `v12.0.9`
Expand Down
12 changes: 12 additions & 0 deletions test/integration/middleware/core/pages/rewrites/[param].js
@@ -0,0 +1,12 @@
export const getServerSideProps = ({ params, query }) => {
return { props: { params, query } }
}

export default function Page({ params: { param }, query: { qp } }) {
return (
<>
<p id="props">{param}</p>
<p id="qp">{qp}</p>
</>
)
}
10 changes: 10 additions & 0 deletions test/integration/middleware/core/pages/rewrites/_middleware.js
Expand Up @@ -41,6 +41,16 @@ export async function middleware(request) {
return NextResponse.rewrite(url)
}

if (url.pathname === '/rewrites/rewrite-me-with-a-colon') {
url.pathname = '/rewrites/with:colon'
return NextResponse.rewrite(url)
}

if (url.pathname === '/rewrites/colon:here') {
url.pathname = '/rewrites/no-colon-here'
return NextResponse.rewrite(url)
}

if (url.pathname === '/rewrites/rewrite-me-to-vercel') {
return NextResponse.rewrite('https://vercel.com')
}
Expand Down
80 changes: 80 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Expand Up @@ -335,6 +335,86 @@ function rewriteTests(log, locale = '') {
expect($('.title').text()).toBe('About Page')
})

it(`${locale} support colons in path`, async () => {
const path = `${locale}/rewrites/not:param`
const res = await fetchViaHTTP(context.appPort, path)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#props').text()).toBe('not:param')
const browser = await webdriver(context.appPort, path)
try {
expect(await browser.eval(`window.location.pathname`)).toBe(path)
} finally {
await browser.close()
}
})

it(`${locale} can rewrite to path with colon`, async () => {
const path = `${locale}/rewrites/rewrite-me-with-a-colon`
const res = await fetchViaHTTP(context.appPort, path)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#props').text()).toBe('with:colon')
const browser = await webdriver(context.appPort, path)
try {
expect(await browser.eval(`window.location.pathname`)).toBe(path)
} finally {
await browser.close()
}
})

it(`${locale} can rewrite from path with colon`, async () => {
const path = `${locale}/rewrites/colon:here`
const res = await fetchViaHTTP(context.appPort, path)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#props').text()).toBe('no-colon-here')
const browser = await webdriver(context.appPort, path)
try {
expect(await browser.eval(`window.location.pathname`)).toBe(path)
} finally {
await browser.close()
}
})

it(`${locale} can rewrite from path with colon and retain query parameter`, async () => {
const path = `${locale}/rewrites/colon:here?qp=arg`
const res = await fetchViaHTTP(context.appPort, path)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#props').text()).toBe('no-colon-here')
expect($('#qp').text()).toBe('arg')
const browser = await webdriver(context.appPort, path)
try {
expect(
await browser.eval(
`window.location.href.replace(window.location.origin, '')`
)
).toBe(path)
} finally {
await browser.close()
}
})

it(`${locale} can rewrite to path with colon and retain query parameter`, async () => {
const path = `${locale}/rewrites/rewrite-me-with-a-colon?qp=arg`
const res = await fetchViaHTTP(context.appPort, path)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#props').text()).toBe('with:colon')
expect($('#qp').text()).toBe('arg')
const browser = await webdriver(context.appPort, path)
try {
expect(
await browser.eval(
`window.location.href.replace(window.location.origin, '')`
)
).toBe(path)
} finally {
await browser.close()
}
})

it(`${locale} should rewrite when not using localhost`, async () => {
const res = await fetchViaHTTP(
`http://localtest.me:${context.appPort}`,
Expand Down