提交 354de284 编写于 作者: J JJ Kasper 提交者: Tim Neutkens

Only add static files route if folder exists (#9305)

* Only add static files route if folder exists

* Remove extra import
上级 b37fcbb5
......@@ -243,6 +243,21 @@ export default class Server {
const publicRoutes = fs.existsSync(this.publicDir)
? this.generatePublicRoutes()
: []
const staticFilesRoute = fs.existsSync(join(this.dir, 'static'))
? [
{
// It's very important to keep this route's param optional.
// (but it should support as many params as needed, separated by '/')
// Otherwise this will lead to a pretty simple DOS attack.
// See more: https://github.com/zeit/next.js/issues/2617
match: route('/static/:path*'),
fn: async (req, res, params, parsedUrl) => {
const p = join(this.dir, 'static', ...(params.path || []))
await this.serveStatic(req, res, p, parsedUrl)
},
} as Route,
]
: []
const routes: Route[] = [
{
......@@ -310,17 +325,7 @@ export default class Server {
},
},
...publicRoutes,
{
// It's very important to keep this route's param optional.
// (but it should support as many params as needed, separated by '/')
// Otherwise this will lead to a pretty simple DOS attack.
// See more: https://github.com/zeit/next.js/issues/2617
match: route('/static/:path*'),
fn: async (req, res, params, parsedUrl) => {
const p = join(this.dir, 'static', ...(params.path || []))
await this.serveStatic(req, res, p, parsedUrl)
},
},
...staticFilesRoute,
{
match: route('/api/:path*'),
fn: async (req, res, params, parsedUrl) => {
......
import Link from 'next/link'
export default () => (
<>
<h3>Hello</h3>
<Link href='/static'>
<a id='to-static'>To static</a>
</Link>
</>
)
export default () => <p id='static'>hello from static page</p>
/* eslint-env jest */
/* global jasmine */
import path from 'path'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import {
launchApp,
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = path.join(__dirname, '..')
const nextConfigPath = path.join(appDir, 'next.config.js')
let app
let appPort
const runTests = () => {
it('should render the page via SSR correctly', async () => {
const html = await renderViaHTTP(appPort, '/static')
expect(html).toMatch(/hello from static page/)
})
it('should navigate to static page name correctly', async () => {
const browser = await webdriver(appPort, '/')
await browser.elementByCss('#to-static').click()
await browser.waitForElementByCss('#static')
const html = await browser.eval(`document.documentElement.innerHTML`)
expect(html).toMatch(/hello from static page/)
})
}
describe('Static Page Name', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
appPort = await findPort()
await nextBuild(appDir)
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('serverless mode', () => {
beforeAll(async () => {
appPort = await findPort()
await fs.writeFile(
nextConfigPath,
'module.exports = { target: "serverless" }'
)
await nextBuild(appDir)
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfigPath)
})
runTests()
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册