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

Update handling for ENOENT from GSSP methods (#11302)

* Update handling for ENOENT from GSSP methods

* Add route to routes-manifest test
parent aadb31fa
...@@ -3,7 +3,7 @@ type CoalescedInvoke<T> = { ...@@ -3,7 +3,7 @@ type CoalescedInvoke<T> = {
value: T value: T
} }
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T export type UnwrapPromise<T> = T extends Promise<infer U> ? U : T
const globalInvokeCache = new Map<string, Promise<CoalescedInvoke<unknown>>>() const globalInvokeCache = new Map<string, Promise<CoalescedInvoke<unknown>>>()
......
...@@ -38,6 +38,8 @@ import { tryGetPreviewData, __ApiPreviewProps } from './api-utils' ...@@ -38,6 +38,8 @@ import { tryGetPreviewData, __ApiPreviewProps } from './api-utils'
import { getPageFiles } from './get-page-files' import { getPageFiles } from './get-page-files'
import { LoadComponentsReturnType, ManifestItem } from './load-components' import { LoadComponentsReturnType, ManifestItem } from './load-components'
import optimizeAmp from './optimize-amp' import optimizeAmp from './optimize-amp'
import { UnwrapPromise } from '../../lib/coalesced-function'
import { GetStaticProps, GetServerSideProps } from '../../types'
function noRouter() { function noRouter() {
const message = const message =
...@@ -497,12 +499,23 @@ export async function renderToHTML( ...@@ -497,12 +499,23 @@ export async function renderToHTML(
} }
if (isSSG && !isFallback) { if (isSSG && !isFallback) {
const data = await getStaticProps!({ let data: UnwrapPromise<ReturnType<GetStaticProps>>
...(pageIsDynamic ? { params: query as ParsedUrlQuery } : undefined),
...(previewData !== false try {
? { preview: true, previewData: previewData } data = await getStaticProps!({
: undefined), ...(pageIsDynamic ? { params: query as ParsedUrlQuery } : undefined),
}) ...(previewData !== false
? { preview: true, previewData: previewData }
: undefined),
})
} catch (err) {
// remove not found error code to prevent triggering legacy
// 404 rendering
if (err.code === 'ENOENT') {
delete err.code
}
throw err
}
const invalidKeys = Object.keys(data).filter( const invalidKeys = Object.keys(data).filter(
key => key !== 'revalidate' && key !== 'props' key => key !== 'revalidate' && key !== 'props'
...@@ -565,15 +578,26 @@ export async function renderToHTML( ...@@ -565,15 +578,26 @@ export async function renderToHTML(
} }
if (getServerSideProps && !isFallback) { if (getServerSideProps && !isFallback) {
const data = await getServerSideProps({ let data: UnwrapPromise<ReturnType<GetServerSideProps>>
req,
res, try {
query, data = await getServerSideProps({
...(pageIsDynamic ? { params: params as ParsedUrlQuery } : undefined), req,
...(previewData !== false res,
? { preview: true, previewData: previewData } query,
: undefined), ...(pageIsDynamic ? { params: params as ParsedUrlQuery } : undefined),
}) ...(previewData !== false
? { preview: true, previewData: previewData }
: undefined),
})
} catch (err) {
// remove not found error code to prevent triggering legacy
// 404 rendering
if (err.code === 'ENOENT') {
delete err.code
}
throw err
}
const invalidKeys = Object.keys(data).filter(key => key !== 'props') const invalidKeys = Object.keys(data).filter(key => key !== 'props')
......
export async function getServerSideProps() {
const error = new Error('oof')
error.code = 'ENOENT'
throw error
}
export default () => 'hi'
...@@ -78,6 +78,12 @@ const expectedManifestRoutes = () => [ ...@@ -78,6 +78,12 @@ const expectedManifestRoutes = () => [
), ),
page: '/default-revalidate', page: '/default-revalidate',
}, },
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/enoent.json$`
),
page: '/enoent',
},
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/invalid-keys.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/invalid-keys.json$`
...@@ -205,6 +211,19 @@ const runTests = (dev = false) => { ...@@ -205,6 +211,19 @@ const runTests = (dev = false) => {
expect(html).toMatch(/Post:.*?post-1/) expect(html).toMatch(/Post:.*?post-1/)
}) })
it('should handle throw ENOENT correctly', async () => {
const res = await fetchViaHTTP(appPort, '/enoent')
const html = await res.text()
if (dev) {
expect(html).toContain('oof')
} else {
expect(res.status).toBe(500)
expect(html).toContain('Internal Server Error')
expect(html).not.toContain('This page could not be found')
}
})
it('should have gssp in __NEXT_DATA__', async () => { it('should have gssp in __NEXT_DATA__', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html) const $ = cheerio.load(html)
......
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