Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

not-found.ts

41 lines1.3 KB

1import type { RouteIds } from './routeInfo'
2import type { RegisteredRouter } from './router'
3​
4export type NotFoundError = {
5 /**
6 @deprecated
7 Use `routeId: rootRouteId` instead
8 */
9 global?: boolean
10 /**
11 @private
12 Do not use this. It's used internally to indicate a path matching error
13 */
14 _global?: boolean
15 data?: any
16 throw?: boolean
17 routeId?: RouteIds<RegisteredRouter['routeTree']>
18 headers?: HeadersInit
20​
21/**
22 * Create a not-found error object recognized by TanStack Router.
23 *
24 * Throw this from loaders/actions to trigger the nearest `notFoundComponent`.
25 * Use `routeId` to target a specific route's not-found boundary. If `throw`
26 * is true, the error is thrown instead of returned.
27 *
28 * @param options Optional settings including `routeId`, `headers`, and `throw`.
29 * @returns A not-found error object that can be thrown or returned.
30 * @link https://tanstack.com/router/latest/docs/router/framework/react/api/router/notFoundFunction
31 */
32export function notFound(options: NotFoundError = {}) {
33 ;(options as any).isNotFound = true
34 if (options.throw) throw options
35 return options
37​
38/** Determine if a value is a TanStack Router not-found error. */
39export function isNotFound(obj: any): obj is NotFoundError {
40 return !!obj?.isNotFound