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

Fix inconsistent handling for /index (#11643)

* Fix inconsistent handling for /index

* Add more test cases

* Add pages/index/index.js test
Co-authored-by: 's avatarJoe Haddad <joe.haddad@zeit.co>
parent bc7d1836
......@@ -37,7 +37,7 @@ export function getPagePath(
}
if (!pagesManifest[page]) {
const cleanedPage = page.replace(/\/index$/, '') || '/'
const cleanedPage = page.replace(/^\/index$/, '') || '/'
if (!pagesManifest[cleanedPage]) {
throw pageNotFoundError(page)
} else {
......
......@@ -14,8 +14,15 @@ export async function findPageFile(
const relativePagePath = `${normalizedPagePath}.${extension}`
const pagePath = join(rootDir, relativePagePath)
if (await isWriteable(pagePath)) {
foundPagePaths.push(relativePagePath)
// only /index and /sub/index when /sub/index/index.js is allowed
// see test/integration/route-indexes for expected index handling
if (
normalizedPagePath.startsWith('/index') ||
!normalizedPagePath.endsWith('/index')
) {
if (await isWriteable(pagePath)) {
foundPagePaths.push(relativePagePath)
}
}
const relativePagePathWithIndex = join(
......
......@@ -52,7 +52,7 @@ describe('Client Navigation', () => {
'/nav/as-path-using-router',
'/nav/url-prop-change',
'/nested-cdm/index',
'/nested-cdm',
]
await Promise.all(
prerender.map(route => renderViaHTTP(context.appPort, route))
......
......@@ -16,7 +16,7 @@ describe('Errors on conflict between public file and page file', () => {
it('Throws error during development', async () => {
const appPort = await findPort()
const app = await launchApp(appDir, appPort)
const conflicts = ['/another/conflict', '/another/index', '/hello']
const conflicts = ['/another/conflict', '/hello']
for (const conflict of conflicts) {
const html = await renderViaHTTP(appPort, conflict)
......
......@@ -29,7 +29,7 @@ app.prepare().then(() => {
}
if (/dashboard/.test(req.url)) {
return app.render(req, res, '/dashboard/index')
return app.render(req, res, '/dashboard')
}
if (/static\/hello\.text/.test(req.url)) {
......
const page = () => 'hello from index'
export default page
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import {
findPort,
launchApp,
killApp,
nextBuild,
nextStart,
fetchViaHTTP,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
let app
let appPort
const appDir = join(__dirname, '../')
const runTests = () => {
it('should handle / correctly', async () => {
const res = await fetchViaHTTP(appPort, '/')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from index')
})
it('should handle /index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/index')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from index')
})
it('should handle /index/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/index/index')
expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found')
})
}
describe('Route index handling', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
})
export default (req, res) => res.end('hi from sub id')
export default (req, res) => res.end('hi from sub index')
const page = () => 'hello from index'
export default page
export default () => 'hello from nested index'
const page = () => 'hello from sub id'
page.getInitialProps = () => ({ hello: 'hi' })
export default page
const page = () => 'hello from sub index'
page.getInitialProps = () => ({ hello: 'hi' })
export default page
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import {
findPort,
launchApp,
killApp,
nextBuild,
nextStart,
fetchViaHTTP,
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
let app
let appPort
const appDir = join(__dirname, '../')
const runTests = () => {
it('should handle / correctly', async () => {
const res = await fetchViaHTTP(appPort, '/')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from index')
})
it('should handle /index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/index')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from index')
})
it('should handle /index/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/index/index')
expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found')
})
it('should handle /nested-index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/nested-index')
expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found')
})
it('should handle /nested-index/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/nested-index/index')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from nested index')
})
it('should handle /nested-index/index/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/nested-index/index/index')
expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found')
})
it('should handle /sub correctly', async () => {
const res = await fetchViaHTTP(appPort, '/sub')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from sub index')
})
it('should handle /sub/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/sub/index')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from sub id')
})
it('should handle /sub/index/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/sub/index/index')
expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found')
})
it('should handle /sub/another correctly', async () => {
const res = await fetchViaHTTP(appPort, '/sub/another')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from sub id')
})
it('should handle /sub/another/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/sub/another/index')
expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found')
})
it('should handle /api/sub correctly', async () => {
const res = await fetchViaHTTP(appPort, '/api/sub')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hi from sub index')
})
it('should handle /api/sub/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/api/sub/index')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hi from sub id')
})
it('should handle /api/sub/index/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/api/sub/index/index')
expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found')
})
it('should handle /api/sub/another correctly', async () => {
const res = await fetchViaHTTP(appPort, '/api/sub/another')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hi from sub id')
})
it('should handle /api/sub/another/index correctly', async () => {
const res = await fetchViaHTTP(appPort, '/api/sub/another/index')
expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found')
})
}
describe('Route indexes handling', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
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