From 668a33d85084dfefc044802311f5e1d80c3baace Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 13 Dec 2021 11:08:43 +0100 Subject: [PATCH] add test --- packages/next/server/normalize-page-path.ts | 3 +- .../dynamic-routing/test/index.test.js | 53 +++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/next/server/normalize-page-path.ts b/packages/next/server/normalize-page-path.ts index 0c25c4ad1f3f4..f9996331e3bec 100644 --- a/packages/next/server/normalize-page-path.ts +++ b/packages/next/server/normalize-page-path.ts @@ -1,4 +1,5 @@ import { posix } from 'path' +import { isDynamicRoute } from '../shared/lib/router/utils' export { normalizePathSep, denormalizePagePath } from './denormalize-page-path' @@ -6,7 +7,7 @@ export function normalizePagePath(page: string): string { // If the page is `/` we need to append `/index`, otherwise the returned directory root will be bundles instead of pages if (page === '/') { page = '/index' - } else if (/^\/index(\/|$)$/.test(page)) { + } else if (/^\/index(\/|$)/.test(page) && !isDynamicRoute(page)) { page = `/index${page}` } // Resolve on anything that doesn't start with `/` diff --git a/test/integration/dynamic-routing/test/index.test.js b/test/integration/dynamic-routing/test/index.test.js index 7be7e6b00a462..cc97e9a6b6b90 100644 --- a/test/integration/dynamic-routing/test/index.test.js +++ b/test/integration/dynamic-routing/test/index.test.js @@ -27,7 +27,7 @@ let buildId const appDir = join(__dirname, '../') const buildIdPath = join(appDir, '.next/BUILD_ID') -function runTests(dev) { +function runTests({ dev, serverless }) { if (dev) { it('should not have error after pinging WebSocket', async () => { const browser = await webdriver(appPort, '/') @@ -1256,6 +1256,14 @@ function runTests(dev) { helloworld: 'hello-world', }, }, + { + namedRegex: '^/index/(?.+?)(?:/)?$', + page: '/index/[...slug]', + regex: normalizeRegEx('^/index/(.+?)(?:/)?$'), + routeKeys: { + slug: 'slug', + }, + }, { namedRegex: `^/on\\-mount/(?[^/]+?)(?:/)?$`, page: '/on-mount/[post]', @@ -1338,6 +1346,43 @@ function runTests(dev) { ], }) }) + + if (!serverless) { + it('should output a pages-manifest correctly', async () => { + const manifest = await fs.readJson( + join(appDir, '.next/server/pages-manifest.json') + ) + + expect(manifest).toEqual({ + '/[name]/[comment]': 'pages/[name]/[comment].js', + '/[name]/comments': 'pages/[name]/comments.js', + '/[name]': 'pages/[name].js', + '/[name]/on-mount-redir': 'pages/[name]/on-mount-redir.html', + '/another': 'pages/another.html', + '/b/[123]': 'pages/b/[123].js', + '/blog/[name]/comment/[id]': 'pages/blog/[name]/comment/[id].js', + '/c/[alongparamnameshouldbeallowedeventhoughweird]': + 'pages/c/[alongparamnameshouldbeallowedeventhoughweird].js', + '/catchall-dash/[...hello-world]': + 'pages/catchall-dash/[...hello-world].html', + '/d/[id]': 'pages/d/[id].html', + '/dash/[hello-world]': 'pages/dash/[hello-world].html', + '/': 'pages/index.html', + '/index/[...slug]': 'pages/index/[...slug].html', + '/on-mount/[post]': 'pages/on-mount/[post].html', + '/p1/p2/all-ssg/[...rest]': 'pages/p1/p2/all-ssg/[...rest].js', + '/p1/p2/all-ssr/[...rest]': 'pages/p1/p2/all-ssr/[...rest].js', + '/p1/p2/nested-all-ssg/[...rest]': + 'pages/p1/p2/nested-all-ssg/[...rest].js', + '/p1/p2/predefined-ssg/[...rest]': + 'pages/p1/p2/predefined-ssg/[...rest].js', + '/_app': 'pages/_app.js', + '/_error': 'pages/_error.js', + '/_document': 'pages/_document.js', + '/404': 'pages/404.html', + }) + }) + } } } @@ -1354,7 +1399,7 @@ describe('Dynamic Routing', () => { }) afterAll(() => killApp(app)) - runTests(true) + runTests({ dev: true, serverless: false }) }) describe('production mode', () => { @@ -1369,7 +1414,7 @@ describe('Dynamic Routing', () => { }) afterAll(() => killApp(app)) - runTests() + runTests({ dev: false, serverless: false }) }) describe('serverless mode', () => { @@ -1389,6 +1434,6 @@ describe('Dynamic Routing', () => { await killApp(app) await fs.remove(nextConfig) }) - runTests() + runTests({ dev: false, serverless: true }) }) })