From 354de28422631bf2184a5e1faa4b7ab9ef15ea03 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 4 Nov 2019 23:55:35 -0600 Subject: [PATCH] Only add static files route if folder exists (#9305) * Only add static files route if folder exists * Remove extra import --- .../next/next-server/server/next-server.ts | 27 ++++--- .../static-page-name/pages/index.js | 10 +++ .../static-page-name/pages/static.js | 1 + .../static-page-name/test/index.test.js | 73 +++++++++++++++++++ 4 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 test/integration/static-page-name/pages/index.js create mode 100644 test/integration/static-page-name/pages/static.js create mode 100644 test/integration/static-page-name/test/index.test.js diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index 5bfbbf6bc4..fb3cc18f23 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -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) => { diff --git a/test/integration/static-page-name/pages/index.js b/test/integration/static-page-name/pages/index.js new file mode 100644 index 0000000000..ebfe1d0502 --- /dev/null +++ b/test/integration/static-page-name/pages/index.js @@ -0,0 +1,10 @@ +import Link from 'next/link' + +export default () => ( + <> +

Hello

+ + To static + + +) diff --git a/test/integration/static-page-name/pages/static.js b/test/integration/static-page-name/pages/static.js new file mode 100644 index 0000000000..86f458b63f --- /dev/null +++ b/test/integration/static-page-name/pages/static.js @@ -0,0 +1 @@ +export default () =>

hello from static page

diff --git a/test/integration/static-page-name/test/index.test.js b/test/integration/static-page-name/test/index.test.js new file mode 100644 index 0000000000..7ab9169f35 --- /dev/null +++ b/test/integration/static-page-name/test/index.test.js @@ -0,0 +1,73 @@ +/* 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() + }) +}) -- GitLab