Skip to content

Commit

Permalink
fix 404 route in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 2, 2022
1 parent d012198 commit 4fee739
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
4 changes: 3 additions & 1 deletion packages/next/build/entries.ts
Expand Up @@ -69,12 +69,14 @@ export function createPagesMapping({
pageExtensions,
pagePaths,
pagesType,
pagesDir,
}: {
hasServerComponents: boolean
isDev: boolean
pageExtensions: string[]
pagePaths: string[]
pagesType: 'pages' | 'root' | 'app'
pagesDir: string | undefined
}): { [page: string]: string } {
const previousPages: { [key: string]: string } = {}
const pages = pagePaths.reduce<{ [key: string]: string }>(
Expand Down Expand Up @@ -133,7 +135,7 @@ export function createPagesMapping({
// In development we always alias these to allow Webpack to fallback to
// the correct source file so that HMR can work properly when a file is
// added or removed.
const root = isDev ? PAGES_DIR_ALIAS : 'next/dist/pages'
const root = isDev && pagesDir ? PAGES_DIR_ALIAS : 'next/dist/pages'

return {
'/_app': `${root}/_app`,
Expand Down
3 changes: 3 additions & 0 deletions packages/next/build/index.ts
Expand Up @@ -494,6 +494,7 @@ export default async function build(
pageExtensions: config.pageExtensions,
pagesType: 'pages',
pagePaths: pagesPaths,
pagesDir,
})
)

Expand All @@ -509,6 +510,7 @@ export default async function build(
isDev: false,
pagesType: 'app',
pageExtensions: config.pageExtensions,
pagesDir: pagesDir,
})
)
}
Expand All @@ -521,6 +523,7 @@ export default async function build(
pageExtensions: config.pageExtensions,
pagePaths: rootPaths,
pagesType: 'root',
pagesDir: pagesDir,
})
}

Expand Down
27 changes: 14 additions & 13 deletions packages/next/server/dev/hot-reloader.ts
Expand Up @@ -421,19 +421,20 @@ export default class HotReloader {
])
)

this.pagesMapping = !this.pagesDir
? {}
: webpackConfigSpan.traceChild('create-pages-mapping').traceFn(() =>
createPagesMapping({
hasServerComponents: this.hasServerComponents,
isDev: true,
pageExtensions: this.config.pageExtensions,
pagesType: 'pages',
pagePaths: pagePaths.filter(
(i: string | null): i is string => typeof i === 'string'
),
})
)
this.pagesMapping = webpackConfigSpan
.traceChild('create-pages-mapping')
.traceFn(() =>
createPagesMapping({
hasServerComponents: this.hasServerComponents,
isDev: true,
pageExtensions: this.config.pageExtensions,
pagesType: 'pages',
pagePaths: pagePaths.filter(
(i: string | null): i is string => typeof i === 'string'
),
pagesDir: this.pagesDir,
})
)

const entrypoints = await webpackConfigSpan
.traceChild('create-entrypoints')
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/app-dir/rsc-basic.test.ts
Expand Up @@ -262,6 +262,14 @@ describe('app dir - react server components', () => {
expect(manipulated).toBe(undefined)
})

it('should render built-in 404 page for missing route if pagesDir is not presented', async () => {
const res = await fetchViaHTTP(next.url, '/does-not-exist')

expect(res.status).toBe(404)
const html = await res.text()
expect(html).toContain('This page could not be found')
})

it('should suspense next/image in server components', async () => {
const imageHTML = await renderViaHTTP(next.url, '/next-api/image')
const imageTag = getNodeBySelector(imageHTML, '#myimg')
Expand Down

0 comments on commit 4fee739

Please sign in to comment.