Reputation: 1
I'm building a site in Gatsby using Prismic as my CMS and so far it's been great. Although I've hit a bit of an issue I can't figure out.
I want to be able to have all the posts pulled in from Prismic be filterable by tags and show all by default.
I've managed to setup an array which stores the tags the user has selected on the page and I've figured out I can pass a static array of values into the query to filter the posts at build time.
But, what I cannot figure out is how to get all posts at build time and display all of them when no filters are selected. But when tags are selected and in the active tags array only show the posts that contain those tags.
To my understanding Gatsby does not allow for queries to be run during runtime and will only run them during build. I've also played with the idea of conditionally rendering the template component and have it change depending if the tags of that post is included in the active tags array but no luck there either.
I tried asking prismic support but they said they haven't had a user with this use case before.
Any ideas would be greatly appreciated.
Upvotes: 0
Views: 237
Reputation: 80041
Because Gatsby is static, there's no server running to respond to requests.
You can use Prismic’s GraphQL API client-side if you wanted to fetch filtered data live, then you can pass that information into the relevant component for display. This data wouldn't go through Gatsby’s GraphQL layer, though, so any modifications, extensions, or side effects that Gatsby is performing (such as image transformations, custom resolutions, remark, etc) would not be available.
If you’re already loading the data onto the client (i.e. you’re fetching all post metadata that you would filter on anyways), an easier path is to simply filter the data client-side.
Upvotes: 1