Static website and GraphQL queries with Nuxt.js
Graphql client is king.
The problem
I encountered a nasty bug while using static generation with Nuxt and nuxt apollo client. I found the issue already reported on github.
It seems the module doesn’t handle static generation correctly with nuxt generate
.
I could find request to my local API url after the static generation. Moreover, it also seemed like <nuxt-link>
navigation was broken.
The solution 🙌
Fortunately, there is another Nuxt module that handles GraphQL requests!
Nuxt graphql request to the rescue!
The conf
The request
The best approach so far is to use asyncData
in pages and fetch
in components. Using fetch
in pages does not work well at all with nuxt generate
.
I also install the graphql-tag
package (only in devDependencies
) to be able to import directly .gql
files.
Query example:
Inside a page
Inside a component
It is safer to wait until fetch
has received a response before displaying anything. You can use $fetchState
to be sure (documentation).
Options
To pass options to the request, for example for a multilingual version with nuxt/i18n and/or a url parameter in a dynamic page: