docs: mention possibility of prerendering api routes (#31234)

This commit is contained in:
Peter Radko 2025-03-19 02:23:40 +03:00 committed by GitHub
parent fc03d2a92b
commit b656c58b39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View File

@ -144,6 +144,7 @@ You can use this at runtime within a [Nuxt context](/docs/guide/going-further/nu
```vue [pages/index.vue] ```vue [pages/index.vue]
<script setup> <script setup>
prerenderRoutes(["/some/other/url"]); prerenderRoutes(["/some/other/url"]);
prerenderRoutes("/api/content/article/my-article");
</script> </script>
<template> <template>

View File

@ -28,3 +28,19 @@ prerenderRoutes(['/', '/about'])
::note ::note
In the browser, or if called outside prerendering, `prerenderRoutes` will have no effect. In the browser, or if called outside prerendering, `prerenderRoutes` will have no effect.
:: ::
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!
```js
prerenderRoutes('/api/content/article/name-of-article')
// Somewhere later in App
const articleContent = await $fetch('/api/content/article/name-of-article', {
responseType: 'json',
})
```
::warning
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.
::