Reputation: 1212
I'm building a site which uses only a combination of statically generated pages and JSON API routes, with no runtime SSR of templates. I would like Nuxt to build it accordingly:
/api
should be handled by a runtime serverHowever, I can't figure out how to configure Nuxt to do this particular combination:
nuxt build
generates the api routes correctly, but doesn't prerender the pagesnuxt generate
prerenders the pages, but doesn't output a server for the api routesI'm on the nitro cloudflare
preset. I've tried various combinations of routeRules
, but I can't figure out how to get the behavior I want. Any ideas?
Upvotes: 2
Views: 2754
Reputation: 1212
I was eventually able to get this working by configuring the underlying Nitro build in my nuxt.config.ts
!
nitro: {
prerender: {
crawlLinks: true,
routes: ['/'],
ignore: ["/api"]
}
},
This causes nuxt build
to crawl all links down from the front page and prerender, while still correctly outputting a worker.js
to handle the /api
routes at runtime.
Upvotes: 7
Reputation: 431
The simplest way is put your <div>
in the template in <clientOnly>
and use lazyfetch for getting data.
for generate,first make your _slug page and set in your generate. for example if you have 3 pages on blog route in nuxt 2 you should do this. I think in nuxt 3 is similar.
in nuxt.config
generate:{
dir: 'your directory of generate',
routes:()=>{
return[
'/blog/blog1_page',
'/blog/blog2_page',
'...'
]
}
Upvotes: 0