I'm trying Nuxt because I need an app that create routes with data from an API.
For my example I got a fake data by using "nuxt/content" and I got 2 companies inside:
[
{
"id": "1",
"name": "company1"
},
{
"id": "2",
"name": "company2"
}
]
With that, I would like to generate 2 routes so (because I got 2 companies):
- /company1
- /company2
Everytime a new company will be added in the API, I want that Nuxt create a new route.
I tried with static mode and generate function in nuxt.config.js
with success. But in static mode, I have a generate static files every time a new data is added in the API.
Here what I did for static mode:
nuxt.config.js file:
export default {
target: 'static',
generate: {
async routes() {
const { $content } = require('@nuxt/content')
const files = await $content('companies').fetch()
return files.map(company => {
return {
route: '/' + company.name,
payload: company
}
})
}
},
_slug.vue file (in pages folder):
<template>
<div>
<h1>test</h1>
<p>Path: {{ $route.path }}</p>
data : {{ company }}
</div>
</template>
<script>
export default {
asyncData({ payload }) {
if (payload) return { company: payload}
else {
return '-1'
}
},
mounted() {
console.log(this.$router)
}
}
</script>
This solution worked great in Static because I saw the generated routes in the console and in the dist folder.
But I think I need the SSR solution to not to have to re-generate the static files all the time and re-upload them.
I didn't find the way to do that, I tried to only remove the "target: 'static'" from the nuxt.config file but the routes aren't generated (or I don't event know where to look to check if it's working). I have see only "_slug" and "index" route if I check the console.log(this.$router). Same if I check the dist folder (and routes.json file).
How do I do that in SSR mode? And will I be a good solution for the SEO?