-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-node.js
48 lines (38 loc) · 1.44 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { store } = require("gatsby/dist/redux");
const { storeStaticQueries } = require("./src/static-query");
const whenQuiet = require("./src/when-quiet");
const debounceInterval = 200;
async function storeStaticQueriesFromState({ reporter, parentSpan }) {
const activity = reporter.activityTimer(`[gatsby-plugin-testing] stored static queries`, {
parentSpan,
});
activity.start();
const staticQueries = store.getState().staticQueryComponents;
if (staticQueries && staticQueries.size) {
await storeStaticQueries(staticQueries);
} else if (!staticQueries) {
reporter.error(
`Could not find static queries in store. This might mean gatsby-plugin-testing is out of date.`
);
} else {
reporter.warn(`No static queries found.`);
}
activity.end();
}
function watchForChanges({ reporter, parentSpan }) {
const onQueryRun = whenQuiet(storeStaticQueriesFromState, debounceInterval);
store.subscribe(async () => {
if (store.getState().lastAction.type == "PAGE_QUERY_RUN") {
onQueryRun({ reporter, parentSpan, state: store.getState() });
}
});
}
const isGatsbyBuild = process.env.NODE_ENV == "production";
if (isGatsbyBuild) {
// during gatsby build, we only want to write once.
exports.onPreBuild = storeStaticQueriesFromState;
} else {
// during gatsby develop, onPreBuild is not available and anyway
// we want to write every time there is a change
exports.onPostBootstrap = watchForChanges;
}