@tanstack/react-router
Version:
Modern and scalable routing for React applications
1 lines • 3.42 kB
Source Map (JSON)
{"version":3,"file":"not-found.cjs","names":[],"sources":["../../src/not-found.tsx"],"sourcesContent":["import * as React from 'react'\nimport { isNotFound } from '@tanstack/router-core'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport { useStore } from '@tanstack/react-store'\nimport { CatchBoundary } from './CatchBoundary'\nimport { useRouter } from './useRouter'\nimport type { ErrorInfo } from 'react'\nimport type { NotFoundError } from '@tanstack/router-core'\n\nexport function CatchNotFound(props: {\n fallback?: (error: NotFoundError) => React.ReactElement\n onCatch?: (error: Error, errorInfo: ErrorInfo) => void\n children: React.ReactNode\n}) {\n const router = useRouter()\n\n if (isServer ?? router.isServer) {\n const pathname = router.stores.location.get().pathname\n const status = router.stores.status.get()\n const resetKey = `not-found-${pathname}-${status}`\n\n return (\n <CatchBoundary\n getResetKey={() => resetKey}\n onCatch={(error, errorInfo) => {\n if (isNotFound(error)) {\n props.onCatch?.(error, errorInfo)\n } else {\n throw error\n }\n }}\n errorComponent={({ error }) => {\n if (isNotFound(error)) {\n return props.fallback?.(error)\n } else {\n throw error\n }\n }}\n >\n {props.children}\n </CatchBoundary>\n )\n }\n\n // TODO: Some way for the user to programmatically reset the not-found boundary?\n // eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static\n const pathname = useStore(\n router.stores.location,\n (location) => location.pathname,\n )\n // eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static\n const status = useStore(router.stores.status, (status) => status)\n const resetKey = `not-found-${pathname}-${status}`\n\n return (\n <CatchBoundary\n getResetKey={() => resetKey}\n onCatch={(error, errorInfo) => {\n if (isNotFound(error)) {\n props.onCatch?.(error, errorInfo)\n } else {\n throw error\n }\n }}\n errorComponent={({ error }) => {\n if (isNotFound(error)) {\n return props.fallback?.(error)\n } else {\n throw error\n }\n }}\n >\n {props.children}\n </CatchBoundary>\n )\n}\n\nexport function DefaultGlobalNotFound() {\n return <p>Not Found</p>\n}\n"],"mappings":";;;;;;;;;;AASA,SAAgB,cAAc,OAI3B;CACD,MAAM,SAAS,kBAAA,UAAU;CAEzB,IAAI,+BAAA,YAAY,OAAO,UAAU;EAG/B,MAAM,WAAW,aAFA,OAAO,OAAO,SAAS,IAAI,EAAE,SAEP,GADxB,OAAO,OAAO,OAAO,IACM;EAE1C,OACE,iBAAA,GAAA,kBAAA,KAAC,sBAAA,eAAD;GACE,mBAAmB;GACnB,UAAU,OAAO,cAAc;IAC7B,KAAA,GAAA,sBAAA,YAAe,KAAK,GAClB,MAAM,UAAU,OAAO,SAAS;SAEhC,MAAM;GAEV;GACA,iBAAiB,EAAE,YAAY;IAC7B,KAAA,GAAA,sBAAA,YAAe,KAAK,GAClB,OAAO,MAAM,WAAW,KAAK;SAE7B,MAAM;GAEV;aAEC,MAAM;EACM,CAAA;CAEnB;CAUA,MAAM,WAAW,cAAA,GAAA,sBAAA,UALf,OAAO,OAAO,WACb,aAAa,SAAS,QAIK,EAAS,IAAA,GAAA,sBAAA,UADf,OAAO,OAAO,SAAS,WAAW,MAChB;CAE1C,OACE,iBAAA,GAAA,kBAAA,KAAC,sBAAA,eAAD;EACE,mBAAmB;EACnB,UAAU,OAAO,cAAc;GAC7B,KAAA,GAAA,sBAAA,YAAe,KAAK,GAClB,MAAM,UAAU,OAAO,SAAS;QAEhC,MAAM;EAEV;EACA,iBAAiB,EAAE,YAAY;GAC7B,KAAA,GAAA,sBAAA,YAAe,KAAK,GAClB,OAAO,MAAM,WAAW,KAAK;QAE7B,MAAM;EAEV;YAEC,MAAM;CACM,CAAA;AAEnB;AAEA,SAAgB,wBAAwB;CACtC,OAAO,iBAAA,GAAA,kBAAA,KAAC,KAAD,EAAA,UAAG,YAAY,CAAA;AACxB"}