Unverified Commit cefbcadf authored by Jan Potoms's avatar Jan Potoms Committed by GitHub

Make page path case sensitive in dev (#8848)

URL paths are case sensitive, MacOS file system is case insensitive. This PR makes sure development mode doesn't find pages that don't have the casing as specified in the URL.

Will clean up and write a test when agreed on this approach.

Fixes https://github.com/zeit/next.js/issues/8825
Fixes https://github.com/zeit/next.js/issues/8847
Fixes https://github.com/zeit/next.js/issues/9080
parent 7ec57c6c
import { join } from 'path' import { join, sep as pathSeparator, normalize } from 'path'
import chalk from 'next/dist/compiled/chalk' import chalk from 'next/dist/compiled/chalk'
import { isWriteable } from '../../build/is-writeable' import { isWriteable } from '../../build/is-writeable'
import { warn } from '../../build/output/log' import { warn } from '../../build/output/log'
import fs from 'fs'
import { promisify } from 'util'
const readdir = promisify(fs.readdir)
async function isTrueCasePagePath(pagePath: string, pagesDir: string) {
const pageSegments = normalize(pagePath).split(pathSeparator).filter(Boolean)
const segmentExistsPromises = pageSegments.map(async (segment, i) => {
const segmentParentDir = join(pagesDir, ...pageSegments.slice(0, i))
const parentDirEntries = await readdir(segmentParentDir)
return parentDirEntries.includes(segment)
})
return (await Promise.all(segmentExistsPromises)).every(Boolean)
}
export async function findPageFile( export async function findPageFile(
rootDir: string, rootDir: string,
...@@ -39,6 +55,10 @@ export async function findPageFile( ...@@ -39,6 +55,10 @@ export async function findPageFile(
return null return null
} }
if (!(await isTrueCasePagePath(foundPagePaths[0], rootDir))) {
return null
}
if (foundPagePaths.length > 1) { if (foundPagePaths.length > 1) {
warn( warn(
`Duplicate page detected. ${chalk.cyan( `Duplicate page detected. ${chalk.cyan(
......
...@@ -990,6 +990,15 @@ describe('Client Navigation', () => { ...@@ -990,6 +990,15 @@ describe('Client Navigation', () => {
await browser.close() await browser.close()
}) })
it('should 404 on wrong casing', async () => {
const browser = await webdriver(context.appPort, '/nAv/AbOuT')
expect(await browser.elementByCss('h1').text()).toBe('404')
expect(await browser.elementByCss('h2').text()).toBe(
'This page could not be found.'
)
await browser.close()
})
it('should 404 for <page>/', async () => { it('should 404 for <page>/', async () => {
const browser = await webdriver(context.appPort, '/nav/about/') const browser = await webdriver(context.appPort, '/nav/about/')
expect(await browser.elementByCss('h1').text()).toBe('404') expect(await browser.elementByCss('h1').text()).toBe('404')
......
...@@ -367,6 +367,12 @@ export default function (render, fetch) { ...@@ -367,6 +367,12 @@ export default function (render, fetch) {
expect($('h2').text()).toBe('This page could not be found.') expect($('h2').text()).toBe('This page could not be found.')
}) })
it('should 404 on wrong casing', async () => {
const $ = await get$('/NaV/aBoUt')
expect($('h1').text()).toBe('404')
expect($('h2').text()).toBe('This page could not be found.')
})
it('should 404 for <page>/', async () => { it('should 404 for <page>/', async () => {
const $ = await get$('/nav/about/') const $ = await get$('/nav/about/')
expect($('h1').text()).toBe('404') expect($('h1').text()).toBe('404')
......
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