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

Fix: Router.query contains _next when using middleware with dynamic routes #48753

Merged
merged 8 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion packages/next/src/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ function getMiddlewareData<T extends FetchDataOutput>(
) {
const parsedSource = getNextPathnameInfo(
parseRelativeUrl(source).pathname,
{ parseData: true }
{
nextConfig: process.env.__NEXT_HAS_REWRITES
? undefined
: nextConfig,
parseData: true,
}
)

as = addBasePath(parsedSource.pathname)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextResponse } from 'next/server'

export default function middleware(_) {
const res = NextResponse.next()
res.headers.set('X-From-Middleware', 'true')
return res
}

export const config = { matcher: '/random' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
basePath: '/base',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Link from 'next/link'
import { useRouter } from 'next/router'

const Index = ({ value }) => {
const router = useRouter()

return (
<section>
<h1>{value}</h1>
<p id="router-path">
<strong>{router.query.path}</strong>
</p>
<Link href="/another-page" id="linkelement">
Link to another page
</Link>
</section>
)
}

export async function getStaticPaths() {
return {
paths: [{ params: { path: 'another-page', pages: null } }],
fallback: true,
}
}

export async function getStaticProps() {
return {
props: {
value: 'Hello',
},
}
}

export default Index
55 changes: 55 additions & 0 deletions test/e2e/middleware-dynamic-basepath-matcher/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-env jest */
/* eslint-disable jest/no-standalone-expect */

import { join } from 'path'
import webdriver from 'next-webdriver'
import { fetchViaHTTP } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'

const itif = (condition: boolean) => (condition ? it : it.skip)

const isModeDeploy = process.env.NEXT_TEST_MODE === 'deploy'

describe('Middleware custom matchers basePath', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: new FileRef(join(__dirname, '../app')),
})
})
afterAll(() => next.destroy())

// FIXME
// See https://linear.app/vercel/issue/EC-170/middleware-rewrite-of-nextjs-with-basepath-does-not-work-on-vercel
itif(!isModeDeploy)('should match', async () => {
for (const path of [
'/base/default',
`/base/_next/data/${next.buildId}/default.json`,
]) {
const res = await fetchViaHTTP(next.url, path)
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBeDefined()
}
})

it.each(['/default', '/invalid/base/default'])(
'should not match',
async (path) => {
const res = await fetchViaHTTP(next.url, path)
expect(res.status).toBe(404)
}
)

// FIXME:
// See https://linear.app/vercel/issue/EC-160/header-value-set-on-middleware-is-not-propagated-on-client-request-of
itif(!isModeDeploy)('should match query path', async () => {
const browser = await webdriver(next.url, '/base/random')
const currentPath = await browser.elementById('router-path').text()
expect(currentPath).toBe('random')
await browser.elementById('linkelement').click()
const anotherPagePath = await browser.elementById('router-path').text()
expect(anotherPagePath).toBe('another-page')
})
})