了解如何处理服务器 404 和丢失的路由。
可以使用 +not-found.tsx 路由处理 404。该路由匹配嵌套级别中所有不匹配的路由。这类似于 [...wild].tsx。但是,与深度动态路由不同,+not-found.tsx 在 API 路由之后匹配。
¥404s can be handled by using a +not-found.tsx route. This route matches all unmatched routes from a nested level. This is similar to [...wild].tsx. However, unlike the deep dynamic routes, +not-found.tsx is matched after API routes.
在网络上,服务器将首先按以下顺序提供文件:
¥On web, the server will first serve files in the following order:
公共目录中的静态文件。
¥Static files in the public directory.
应用目录中的标准和动态路由。
¥Standard and dynamic routes in the app directory.
应用目录中的 API 路由。
¥API routes in the app directory.
未找到的路由将最后提供,并带有 404 状态代码。
¥Not found routes will be served last with a 404 status code.
在没有服务器的原生上,路由将首先尝试执行客户端导航,如果没有路由匹配,则回退到 +not-found
路由。
¥On native, where there is no server, the router will first try to perform a client-navigation, then fall back on +not-found
routes if no route matches.
将来,+not-found
路由将与数据获取和暂挂集成,以允许在仅在线原生操作期间自定义 404 页面。
¥In the future, +not-found
routes will integrate with data fetching and suspense to allow for custom 404 pages during online-only native operations.