Unverified Commit 29d267a1 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add handling for rewriting to /_next with catchall (#9466)

* Add handling for rewriting to /_next with catchall

* bump
parent 546bb68a
......@@ -290,7 +290,7 @@ export default class Server {
]
: []
const routes: Route[] = [
const topRoutes: Route[] = [
{
match: route('/_next/static/:path*'),
type: 'route',
......@@ -381,7 +381,10 @@ export default class Server {
}
},
},
...publicRoutes,
...staticFilesRoute,
]
const routes: Route[] = [...topRoutes]
if (this.customRoutes) {
const { redirects, rewrites } = this.customRoutes
......@@ -428,30 +431,27 @@ export default class Server {
} as Route
})
)
// make sure previous routes can still be rewritten to by
// custom routes e.g. /docs/_next/static -> /_next/static
routes.push(...topRoutes)
}
routes.push(
...([
...publicRoutes,
...staticFilesRoute,
{
match: route('/api/:path*'),
type: 'route',
name: 'API Route',
fn: async (req, res, params, parsedUrl) => {
const { pathname } = parsedUrl
await this.handleApiRequest(
req as NextApiRequest,
res as NextApiResponse,
pathname!
)
return {
finished: true,
}
},
},
] as Route[])
)
routes.push({
match: route('/api/:path*'),
type: 'route',
name: 'API Route',
fn: async (req, res, params, parsedUrl) => {
const { pathname } = parsedUrl
await this.handleApiRequest(
req as NextApiRequest,
res as NextApiResponse,
pathname!
)
return {
finished: true,
}
},
})
if (this.nextConfig.useFileSystemPublicRoutes) {
this.dynamicRoutes = this.getDynamicRoutes()
......
module.exports = {
experimental: {
rewrites() {
return [
{
source: '/docs/:path*',
destination: '/:path*',
},
]
},
},
}
import { useRouter } from 'next/router'
console.log('hello from hello.js')
const Page = () => {
const { query } = useRouter()
return (
<>
<h3>hello world</h3>
<span>{JSON.stringify(query)}</span>
</>
)
}
export default Page
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import {
launchApp,
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
let appDir = join(__dirname, '..')
let buildId
let appPort
let app
const runTests = () => {
it('should rewrite to /_next/static correctly', async () => {
// ensure the bundle is built
await renderViaHTTP(appPort, '/hello')
const bundlePath = await join(
'/docs/_next/static/',
buildId,
'pages/hello.js'
)
const data = await renderViaHTTP(appPort, bundlePath)
expect(data).toContain('hello from hello.js')
})
it('should rewrite and render page correctly', async () => {
const html = await renderViaHTTP(appPort, '/docs/hello')
expect(html).toMatch(/hello world/)
})
it('should rewrite to public/static correctly', async () => {
const data = await renderViaHTTP(appPort, '/docs/static/data.json')
expect(data).toContain('some data...')
})
it('should rewrite to public file correctly', async () => {
const data = await renderViaHTTP(appPort, '/docs/another.txt')
expect(data).toContain('some text')
})
}
describe('Custom routes', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
buildId = 'development'
})
afterAll(() => killApp(app))
runTests(true)
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
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