Skip to content

Commit

Permalink
fix: added temp error workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Jun 8, 2023
1 parent e5fe3f8 commit 62f9970
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
21 changes: 17 additions & 4 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
import { NEXT_QUERY_PARAM_PREFIX } from '../lib/constants'
import { isRouteMatch } from './future/route-matches/route-match'
import { NextRequestAdapter } from './web/spec-extension/adapters/next-request'
import { NotFoundError } from './future/route-modules/helpers/render-result-to-response'

export type FindComponentsResult = {
components: LoadComponentsReturnType
Expand Down Expand Up @@ -1794,12 +1795,24 @@ export default abstract class Server<ServerOptions extends Options = Options> {

// Handle the request using the module.
const request = NextRequestAdapter.fromBaseNextRequest(req)
const response = await module.handle(request, context)

// Send the response now that we have copied it into the cache.
await sendResponse(req, res, response)
try {
const response = await module.handle(request, context)

return null
// Send the response now that we have copied it into the cache.
await sendResponse(req, res, response)

return null
} catch (err) {
if (NotFoundError.isNotFoundError(err)) {
// If we couldn't find the page, we should return null so that
// the legacy render method will be used.
return { value: null, revalidate: err.metadata.revalidate }
}

// This was an unexpected error, so we should throw it again.
throw err
}
}
} else {
// If we didn't match a page, we should fallback to using the legacy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { RouteDefinition } from '../../route-definitions/route-definition'
import type { Redirect } from '../../../../../types'
import type RenderResult from '../../../render-result'
import type { NextRequest } from '../../../web/spec-extension/request'
import type { RenderResultMetadata } from '../../../render-result'

import fresh from 'next/dist/compiled/fresh'
import { normalizeRepeatedSlashes } from '../../../../shared/lib/utils'
Expand All @@ -27,6 +28,16 @@ type RenderResultToResponseModule = {
generateEtags: boolean | undefined
}

export class NotFoundError {
public readonly digest = 'NEXT_PAGES_NOT_FOUND'

constructor(public readonly metadata: RenderResultMetadata) {}

public static isNotFoundError(error: any): error is NotFoundError {
return error?.digest === 'NEXT_PAGES_NOT_FOUND'
}
}

/**
* Converts a `RenderResult` into a `Response` object.
*
Expand Down Expand Up @@ -127,7 +138,12 @@ export async function renderResultToResponse(

// If this is a not found, we can return the result immediately.
if (metadata.isNotFound) {
throw new Error("Invariant: 'isNotFound' should never be true here")
// TODO: re-enable this once error handling is inside the module
// throw new Error("Invariant: 'isNotFound' should never be true here")

// NOTE: this is a temporary workaround until we can get the error handling
// inside the module. This will trigger the not found page.
throw new NotFoundError(metadata)
}

// Get and set the content type on the response.
Expand Down
6 changes: 4 additions & 2 deletions packages/next/src/server/future/route-modules/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ export class PagesRouteModule extends RouteModule<
)
}

public render(
public async render(
req: IncomingMessage,
res: ServerResponse,
pathname: string,
query: NextParsedUrlQuery,
renderOpts: RenderOpts
): Promise<RenderResult> {
return renderToHTML(req, res, pathname, query, renderOpts)
const result = await renderToHTML(req, res, pathname, query, renderOpts)

return result
}
}

Expand Down

0 comments on commit 62f9970

Please sign in to comment.