apivue.jsasynchronousstrapigridsome

Gridsome doesn't refresh data after changes in API


I'm fetching data from a strapi Api as Collection, it works but if i make changes in Strapi the data doesn't changes in Gridsome and i have to restart the Gridsome server to get the new info.

Runing Api and Gridsome in local development

I already checked strapi is working and graphql doesn't get the new data.

my code:

gridsome.server.js

const axios = require('axios')

api.loadSource(async actions => {
const { data } = await axios.get('http://localhost:1337/events')

const collection = actions.addCollection({
   typeName: 'Event',
   path: 'event/:id'
})

 for (const event of data) {
 collection.addNode({
 id: event.id,
 title: event.title,
 description: event.description,
 price: event.price,
 date: event.date,
 location: event.location,
 duration: event.duration,
 coverName: event.cover.hash,
 coverThumbNail: event.cover.formats.thumbnail.url,
 coverMedium: event.cover.formats.medium.url,
 coverImage: event.cover.formats.large.url,
 category: event.categories,
 path: '/events/'+event.id
 })
}
})

my index.vue

<page-query> query { events: allEvent{ edges{ node { id title description price date location duration coverName coverThumbNail coverMedium coverImage category { id } } } } } </page-query>

 <script>
     export default {
          data: () => ({
          events: []
         }),
          mounted() {
         this.events = this.$page.events.edges
          },
         }

Thanks for your time


Solution

  • From documentation on strapi.io https://strapi.io/documentation/developer-docs/latest/developer-resources/content-api/integrations/gridsome.html#configure-gridsome

    Gridsome is a Static Site Generator (opens new window)and will fetch your content from Strapi at build time.

    Basically, that means the GraphQL of Gridsome is "filled" with data from Strapi on the build, and to get new posts, you should regenerate your page.

    If you don’t want to manually deploy your site every time you add a post, you should check Strapi's webhooks