Skip to content

Commit

Permalink
feat: allow errors to be rendered by the route module
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Jun 15, 2023
1 parent 019ed1c commit f4b730b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
17 changes: 16 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ import * as Log from '../build/output/log'
import escapePathDelimiters from '../shared/lib/router/utils/escape-path-delimiters'
import { getUtils } from './server-utils'
import isError, { getProperError } from '../lib/is-error'
import { addRequestMeta, getRequestMeta } from './request-meta'
import {
addRequestMeta,
getRequestMeta,
removeRequestMeta,
} from './request-meta'

import { ImageConfigComplete } from '../shared/lib/image-config'
import { removePathPrefix } from '../shared/lib/router/utils/remove-path-prefix'
Expand Down Expand Up @@ -2529,6 +2533,17 @@ export default abstract class Server<ServerOptions extends Options = Options> {
)
}

// If the page has a route module, use it for the new match. If it doesn't
// have a route module, remove the match.
if (result.components.ComponentMod.routeModule) {
addRequestMeta(ctx.req, '_nextMatch', {
definition: result.components.ComponentMod.routeModule.definition,
params: undefined,
})
} else {
removeRequestMeta(ctx.req, '_nextMatch')
}

try {
return await this.renderToResponseWithComponents(
{
Expand Down
41 changes: 40 additions & 1 deletion packages/next/src/server/request-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export interface RequestMeta {
_nextMinimalMode?: boolean
}

/**
* Gets the request metadata. If no key is provided, the entire metadata object
* is returned.
*
* @param req the request to get the metadata from
* @param key the key to get from the metadata (optional)
* @returns the value for the key or the entire metadata object
*/
export function getRequestMeta(
req: NextIncomingMessage,
key?: undefined
Expand All @@ -58,11 +66,26 @@ export function getRequestMeta<K extends keyof RequestMeta>(
return typeof key === 'string' ? meta[key] : meta
}

/**
* Sets the request metadata.
*
* @param req the request to set the metadata on
* @param meta the metadata to set
* @returns the mutated request metadata
*/
export function setRequestMeta(req: NextIncomingMessage, meta: RequestMeta) {
req[NEXT_REQUEST_META] = meta
return getRequestMeta(req)
return meta
}

/**
* Adds a value to the request metadata.
*
* @param request the request to mutate
* @param key the key to set
* @param value the value to set
* @returns the mutated request metadata
*/
export function addRequestMeta<K extends keyof RequestMeta>(
request: NextIncomingMessage,
key: K,
Expand All @@ -73,6 +96,22 @@ export function addRequestMeta<K extends keyof RequestMeta>(
return setRequestMeta(request, meta)
}

/**
* Removes a key from the request metadata.
*
* @param request the request to mutate
* @param key the key to remove
* @returns the mutated request metadata
*/
export function removeRequestMeta<K extends keyof RequestMeta>(
request: NextIncomingMessage,
key: K
) {
const meta = getRequestMeta(request)
delete meta[key]
return setRequestMeta(request, meta)
}

type NextQueryMetadata = {
__nextNotFoundSrcPage?: string
__nextDefaultLocale?: string
Expand Down

0 comments on commit f4b730b

Please sign in to comment.