mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Only show 404 when the page does not exist (#3976)
* Only show 404 when the page does not exist * Do async filesystem check
This commit is contained in:
parent
4757836a67
commit
7e9f4f9327
|
@ -55,8 +55,10 @@ async function doRender (req, res, pathname, query, {
|
||||||
|
|
||||||
const documentPath = join(dir, dist, 'dist', 'bundles', 'pages', '_document')
|
const documentPath = join(dir, dist, 'dist', 'bundles', 'pages', '_document')
|
||||||
|
|
||||||
let Component = requirePage(page, {dir, dist})
|
let [Component, Document] = await Promise.all([
|
||||||
let Document = require(documentPath)
|
requirePage(page, {dir, dist}),
|
||||||
|
require(documentPath)
|
||||||
|
])
|
||||||
Component = Component.default || Component
|
Component = Component.default || Component
|
||||||
Document = Document.default || Document
|
Document = Document.default || Document
|
||||||
const asPath = req.url
|
const asPath = req.url
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import {join, parse, normalize, sep} from 'path'
|
import {join, parse, normalize, sep} from 'path'
|
||||||
|
import fs from 'mz/fs'
|
||||||
|
|
||||||
export function pageNotFoundError (page) {
|
export function pageNotFoundError (page) {
|
||||||
const err = new Error(`Cannot find module for page: ${page}`)
|
const err = new Error(`Cannot find module for page: ${page}`)
|
||||||
|
@ -53,19 +54,12 @@ export function getPagePath (page, {dir, dist}) {
|
||||||
return pagePath
|
return pagePath
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function requirePage (page, {dir, dist}) {
|
export default async function requirePage (page, {dir, dist}) {
|
||||||
const pagePath = getPagePath(page, {dir, dist})
|
const pagePath = getPagePath(page, {dir, dist}) + '.js'
|
||||||
|
const fileExists = await fs.exists(pagePath)
|
||||||
try {
|
if (!fileExists) {
|
||||||
return require(pagePath)
|
throw pageNotFoundError(page)
|
||||||
} catch (err) {
|
|
||||||
if (err.code === 'MODULE_NOT_FOUND') {
|
|
||||||
throw pageNotFoundError(page)
|
|
||||||
}
|
|
||||||
console.error(err)
|
|
||||||
// If this is not a MODULE_NOT_FOUND error,
|
|
||||||
// it should be something with the content of the page.
|
|
||||||
// So, Next.js rendering system will catch it and process.
|
|
||||||
throw err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return require(pagePath)
|
||||||
}
|
}
|
||||||
|
|
4
test/isolated/_resolvedata/dist/bundles/pages/non-existent-child.js
vendored
Normal file
4
test/isolated/_resolvedata/dist/bundles/pages/non-existent-child.js
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
const nonExistent = require('./non-existent-module')
|
||||||
|
module.exports = {
|
||||||
|
test: nonExistent
|
||||||
|
}
|
|
@ -57,26 +57,42 @@ describe('getPagePath', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('requirePage', () => {
|
describe('requirePage', () => {
|
||||||
it('Should require /index.js when using /', () => {
|
it('Should require /index.js when using /', async () => {
|
||||||
const page = requirePage('/', {dir: __dirname, dist: '_resolvedata'})
|
const page = await requirePage('/', {dir: __dirname, dist: '_resolvedata'})
|
||||||
expect(page.test).toBe('hello')
|
expect(page.test).toBe('hello')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should require /index.js when using /index', () => {
|
it('Should require /index.js when using /index', async () => {
|
||||||
const page = requirePage('/index', {dir: __dirname, dist: '_resolvedata'})
|
const page = await requirePage('/index', {dir: __dirname, dist: '_resolvedata'})
|
||||||
expect(page.test).toBe('hello')
|
expect(page.test).toBe('hello')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should require /world.js when using /world', () => {
|
it('Should require /world.js when using /world', async () => {
|
||||||
const page = requirePage('/world', {dir: __dirname, dist: '_resolvedata'})
|
const page = await requirePage('/world', {dir: __dirname, dist: '_resolvedata'})
|
||||||
expect(page.test).toBe('world')
|
expect(page.test).toBe('world')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should throw when using /../../test.js', () => {
|
it('Should throw when using /../../test.js', async () => {
|
||||||
expect(() => requirePage('/../../test.js', {dir: __dirname, dist: '_resolvedata'})).toThrow()
|
try {
|
||||||
|
await requirePage('/../../test', {dir: __dirname, dist: '_resolvedata'})
|
||||||
|
} catch (err) {
|
||||||
|
expect(err.code).toBe('ENOENT')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should throw when using non existent pages like /non-existent.js', () => {
|
it('Should throw when using non existent pages like /non-existent.js', async () => {
|
||||||
expect(() => requirePage('/non-existent.js', {dir: __dirname, dist: '_resolvedata'})).toThrow()
|
try {
|
||||||
|
await requirePage('/non-existent', {dir: __dirname, dist: '_resolvedata'})
|
||||||
|
} catch (err) {
|
||||||
|
expect(err.code).toBe('ENOENT')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should bubble up errors in the child component', async () => {
|
||||||
|
try {
|
||||||
|
await requirePage('/non-existent-child', {dir: __dirname, dist: '_resolvedata'})
|
||||||
|
} catch (err) {
|
||||||
|
expect(err.code).toBe('MODULE_NOT_FOUND')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue