1

Do you know if it's possible to re-execute Gatsby page queries (normal queries) manually?
Note, This should happen in dev mode while gatsby develop runs.

Background info: I'm trying to set up a draft environment with Gatsby and a Headless CMS (Craft CMS in my case). I want gatsby develop to run on, say, heroku. The CMS requests a Gatsby page, passing a specific draft-token as an URL param, and then the page queries should be re-executed, using the token to re-fetch the draft content from the CMS rather than the published content.

I'm hooking into the token-request via a middleware defined in gatsby-config.js. This is all based on https://gist.github.com/monachilada/af7e92a86e0d27ba47a8597ac4e4b105

I tried

createSchemaCustomization({ refresh: true }).then(() => {
  sourceNodes()
})

but this completely re-creates all pages. I really only want the page queries to be extracted/executed.

urz0r
  • 462
  • 3
  • 8

2 Answers2

0

Probably you are looking for this. Basically, you need to set an environment variable (ENABLE_GATSBY_REFRESH_ENDPOINT) which opens and exposes a /__refresh webhook that is able to receive POST requests to refresh the sourced content. This exposed webhook can be triggered whenever remote data changes, which means you can update your data without re-launching the development server.

You can also trigger it manually using: curl -X POST http://localhost:8000/__refresh

If you need a detailed explanation of how to set .env variables in Gatsby just tell me and I will provide a detailed explanation. But you just need to create a .env file with your variables (ENABLE_GATSBY_REFRESH_ENDPOINT=true) and place this snippet in your gatsby-config.js:

require("dotenv").config({
  path: `.env.${activeEnv}`,
})

Of course, it will only work under the development environment but in this case, it fits your requirements.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • Thanks for your reply! I tried the refresh endpoint before, but this also rebuilds all pages while I only want the page queries to be rebuilt, unfortunately. – urz0r Apr 17 '20 at 11:22
0

Rebuild for all is needed f.e. when you have indexing pages.

It looks like you need some logic to conditionally call createPage (with all data refetched) or even conditionally fetch data for selected pages only.

If amount (of pages) is relatively not so big I would fetch for all data to get page update times. Then in loop conditionally (time within a few minutes - no needs to pass parameter) call createPage.

If develop doesn't call 'createPage' on /__refresh ... dive deeper into gatsby code and find logic and way to modify redux touched nodes.

... or search for other optimization techniques you can use for this scenario (queried data cached into json files?).

xadm
  • 8,219
  • 3
  • 14
  • 25