Skip to content

Commit

Permalink
Fix Gatsby CMS Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Apr 7, 2022
1 parent 2b4e873 commit 2c72e4d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 19 deletions.
64 changes: 64 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Parallel Query Running

With [PQR](#https://www.gatsbyjs.com/docs/reference/release-notes/v3.10/#experimental-parallel-query-running), multiple processes run queries.

How to test this scenario locally: just run `gatsby build`.

In this scenario, we want to only fetch data once in the main process, cache the responses and reuse them inside the workers.

- `process.env.NODE_ENV` is set to `production`;
- A first process calls `createSchemaCustomization`;
- A number of subsequent processes (with `GATSBY_WORKER_ID` env variable set to a number) call `sourceNodes`.

# Deferred Static Generation

With [DSG](https://www.gatsbyjs.com/docs/reference/rendering-options/deferred-static-generation/), instead of generating every page up front, you can decide to generate certain pages at build time and others only when a user accesses the page for the first time. Subsequent page requests use the same HTML and JSON generated during the very first request to this page.

How to test this scenario locally: run `gatsby build` and `gatsby serve` from the command line, and make sure some pages are marked with `defer: true`.

In this scenario:

- page that has not been generated yet gets requested;
- `gatsby serve` will only call `createSchemaCustomization` (not `sourceNodes`);

# Develop preview mode

How to test this scenario locally: just run `gatsby develop`.

In this scenario:

- `process.env.NODE_ENV` is set to `development`;
- `createSchemaCustomization` gets called;
- `sourceNodes` gets called;

# Gatsby CMS Preview

This is the "CMS Preview" functionality offered by Gatsby Cloud.

How to test this scenario locally?

Run:

```
ENABLE_GATSBY_REFRESH_ENDPOINT=true GATSBY_EXPERIMENTAL_DISABLE_SCHEMA_REBUILD=true gatsby develop
```

The `GATSBY_EXPERIMENTAL_DISABLE_SCHEMA_REBUILD` above is important — and Gatsby Preview sets this env variable for us — because otherwise the `createSchemaCustomization` hook would be called, reloading everything from scratch.

Webhooks must trigger create/update/delete/publish events on items/assets to the `/__refresh` endpoint. The webhook must have the following custom body:

```
{
"event_type":"{{event_type}}",
"entity_id":"{{#entity}}{{id}}{{/entity}}",
"entity_type":"{{entity_type}}"
}
```

Also make sure to set `disableLiveReload: true` in plugin config to avoid nodes from being refreshed by Pusher notifications.

In this scenario:

- `process.env.NODE_ENV` is set to `development`;
- `createSchemaCustomization` gets called;
- `sourceNodes` gets called with the `context.webhookBody` set to the body of the incoming webhook;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-datocms",
"version": "3.0.19-0",
"version": "3.0.19-3",
"description": "Gatsby source plugin for building websites using DatoCMS as data source",
"main": "index.js",
"repository": {
Expand Down
16 changes: 8 additions & 8 deletions src/hooks/createSchemaCustomization/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ module.exports = async (
},
};

let activity;
if (!process.env.GATSBY_WORKER_ID) {
let activity;

activity = reporter.activityTimer(`loading DatoCMS schema`, {
parentSpan,
});
activity = reporter.activityTimer(`loading DatoCMS schema`, {
parentSpan,
});

activity.start();
activity.start();

if (!process.env.GATSBY_WORKER_ID) {
await loader.load();
await loader.saveStateToCache(cache);
}

activity.end();
activity.end();
}

createTypes(context);
};
24 changes: 14 additions & 10 deletions src/hooks/sourceNodes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ module.exports = async (
`DatoCms${instancePrefix ? pascalize(instancePrefix) : ''}${type}`,
};

if (webhookBody && Object.keys(webhookBody).length) {
// we need this both for Gatsby CMS Preview and live reload on local development

loader.entitiesRepo.addUpsertListener(entity => {
createNodeFromEntity(entity, context);
});

loader.entitiesRepo.addDestroyListener(entity => {
destroyEntityNode(entity, context);
});

const isWebhook = webhookBody && Object.keys(webhookBody).length > 0;

if (isWebhook) {
const {
entity_id: entityId,
entity_type: entityType,
Expand Down Expand Up @@ -139,16 +151,8 @@ module.exports = async (
});
});

const queue = new Queue(1, Infinity);

if (process.env.NODE_ENV !== `production` && !disableLiveReload) {
loader.entitiesRepo.addUpsertListener(entity => {
createNodeFromEntity(entity, context);
});

loader.entitiesRepo.addDestroyListener(entity => {
destroyEntityNode(entity, context);
});
const queue = new Queue(1, Infinity);

loader.watch(loadPromise => {
queue.add(async () => {
Expand Down

0 comments on commit 2c72e4d

Please sign in to comment.