Unverified Commit e1e6cfa1 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix page checking failing with trailingSlash (#16362)

This fixes page checking failing due to the trailing slash being present which causes pages to proxied by a rewrite when they shouldn't be. This also adds additional tests to ensure rewriting to an external resource is working correctly with `trailingSlash: true`

Fixes: https://github.com/vercel/next.js/issues/15700
parent 681fbbd0
...@@ -2,6 +2,7 @@ import { IncomingMessage, ServerResponse } from 'http' ...@@ -2,6 +2,7 @@ import { IncomingMessage, ServerResponse } from 'http'
import { UrlWithParsedQuery } from 'url' import { UrlWithParsedQuery } from 'url'
import pathMatch from './lib/path-match' import pathMatch from './lib/path-match'
import { removePathTrailingSlash } from '../../client/normalize-trailing-slash'
export const route = pathMatch() export const route = pathMatch()
...@@ -132,11 +133,13 @@ export default class Router { ...@@ -132,11 +133,13 @@ export default class Router {
requireBasePath: false, requireBasePath: false,
match: route('/:path*'), match: route('/:path*'),
fn: async (checkerReq, checkerRes, params, parsedCheckerUrl) => { fn: async (checkerReq, checkerRes, params, parsedCheckerUrl) => {
const { pathname } = parsedCheckerUrl let { pathname } = parsedCheckerUrl
pathname = removePathTrailingSlash(pathname || '/')
if (!pathname) { if (!pathname) {
return { finished: false } return { finished: false }
} }
if (await memoizedPageChecker(pathname)) { if (await memoizedPageChecker(pathname)) {
return this.catchAllRoute.fn( return this.catchAllRoute.fn(
checkerReq, checkerReq,
......
module.exports = {
trailingSlash: true,
async rewrites() {
return [
{
source: '/:path*/',
destination: '/:path*/',
},
{
source: '/:path*/',
destination: 'http://localhost:__EXTERNAL_PORT__/:path*',
},
]
},
}
export default function Page() {
return <p>catch-all</p>
}
export default function Index() {
return <p>index page</p>
}
export default function Products() {
return <p>a product</p>
}
export default function Products() {
return <p>some products</p>
}
const http = require('http')
const server = http.createServer((req, res) => {
console.log('got request', req.url)
res.end(req.url)
})
const port = process.env.PORT || 3000
server.listen(port, () => {
console.log('Ready on http://localhost:' + port)
})
/* eslint-env jest */
import { join } from 'path'
import {
findPort,
killApp,
nextBuild,
nextStart,
File,
initNextServerScript,
renderViaHTTP,
launchApp,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
let app
let appPort
let proxyPort
let proxyServer
const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))
const runTests = () => {
it('should resolve index page correctly', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toContain('index page')
})
it('should resolve products page correctly', async () => {
const html = await renderViaHTTP(appPort, '/products')
expect(html).toContain('some products')
})
it('should resolve a dynamic page correctly', async () => {
const html = await renderViaHTTP(appPort, '/products/first')
expect(html).toContain('a product')
})
it('should resolve a catch-all page correctly', async () => {
const html = await renderViaHTTP(appPort, '/catch-all/hello')
expect(html).toContain('catch-all')
})
it('should proxy non-existent page correctly', async () => {
const html = await renderViaHTTP(appPort, '/non-existent')
expect(html).toBe('/non-existent')
})
}
describe('Trailing Slash Rewrite Proxying', () => {
describe('production mode', () => {
beforeAll(async () => {
proxyPort = await findPort()
proxyServer = await initNextServerScript(
join(appDir, 'server.js'),
/ready on/i,
{
...process.env,
PORT: proxyPort,
}
)
nextConfig.replace('__EXTERNAL_PORT__', proxyPort)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
nextConfig.restore()
await killApp(app)
await killApp(proxyServer)
})
runTests()
})
describe('dev mode', () => {
beforeAll(async () => {
proxyPort = await findPort()
proxyServer = await initNextServerScript(
join(appDir, 'server.js'),
/ready on/i,
{
...process.env,
PORT: proxyPort,
}
)
nextConfig.replace('__EXTERNAL_PORT__', proxyPort)
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
nextConfig.restore()
await killApp(app)
await killApp(proxyServer)
})
runTests()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment