prerenderRoutes
prerenderRoutes 提示 Nitro 预渲染附加路由。
预渲染时,你可以提示 Nitro 预渲染其他路径,即使这些路径的 URL 未显示在生成页面的 HTML 中。
¥When prerendering, you can hint to Nitro to prerender additional paths, even if their URLs do not show up in the HTML of the generated page.
prerenderRoutes
只能在 Nuxt context 中调用。prerenderRoutes
必须在预渲染期间执行。如果 prerenderRoutes
用于未预渲染的动态页面/路由,则不会执行。const route = useRoute()
prerenderRoutes('/')
prerenderRoutes(['/', '/about'])
在浏览器中,或者在预渲染之外调用时,
prerenderRoutes
将不起作用。你甚至可以预渲染 API 路由,这对于完全静态生成的网站 (SSG) 特别有用,因为你可以像拥有可用服务器一样预渲染数据!
¥You can even prerender API routes which is particularly useful for full statically generated sites (SSG) because you can then $fetch
data as if you have an available server!
prerenderRoutes('/api/content/article/name-of-article')
// Somewhere later in App
const articleContent = await $fetch('/api/content/article/name-of-article', {
responseType: 'json',
})
生产环境中预渲染的 API 路由可能无法返回预期的响应标头,具体取决于你部署到的提供程序。例如,JSON 响应可能使用
application/octet-stream
内容类型。获取预渲染的 API 路由时,请务必手动设置 responseType
。¥Prerendered API routes in production may not return the expected response headers, depending on the provider you deploy to. For example, a JSON response might be served with an application/octet-stream
content type.
Always manually set responseType
when fetching prerendered API routes.