Skip to content

Commit

Permalink
fix: fix race condition causing incorrect data to be written to file
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 13, 2020
1 parent 6685062 commit 753cd77
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
42 changes: 17 additions & 25 deletions plugins/gatsby-source-swiftype/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require('fs');
const search = require('./src/search');
const createRelatedResourceNode = require('./src/createRelatedResourceNode');
const getRelatedResources = require('./src/getRelatedResources');

const writeableData = {};

exports.onPreBootstrap = (_, pluginOptions) => {
const { file } = pluginOptions;
Expand All @@ -15,17 +17,9 @@ exports.onCreateNode = async (
pluginOptions
) => {
const { createNode } = actions;
const {
enabled,
filterNode = () => false,
getParams = () => ({}),
getPath,
pageLimit,
engineKey,
file,
} = pluginOptions;

if (!enabled || node.internal.type !== 'Mdx' || !filterNode({ node })) {
const { filterNode = () => false, getPath } = pluginOptions;

if (node.internal.type !== 'Mdx' || !filterNode({ node })) {
return;
}

Expand All @@ -35,30 +29,20 @@ exports.onCreateNode = async (
},
] = getNodesByType('Site');

const data = JSON.parse(fs.readFileSync(file));
const params = getParams({ node });
const pathname = getPath({ node });
const url = siteUrl + pathname;
const resources = await getRelatedResources({ node, siteUrl }, pluginOptions);

const resources = await search(url, params, {
engineKey,
pageLimit,
});
writeableData[pathname] = resources;

resources.forEach((resource) => {
createRelatedResourceNode({
parentPathname: pathname,
parent: node.id,
resource,
createContentDigest,
createNode,
createNodeId,
});
});

fs.writeFileSync(
file,
JSON.stringify({ ...data, [pathname]: resources }, null, 2, { flag: 'w' })
);
};

exports.createSchemaCustomization = ({ actions }) => {
Expand Down Expand Up @@ -89,3 +73,11 @@ exports.createResolvers = ({ createResolvers }, pluginOptions) => {
},
});
};

exports.onPostBootstrap = (_, pluginOptions) => {
const { enabled, file } = pluginOptions;

if (enabled) {
fs.writeFileSync(file, JSON.stringify(writeableData, null, 2));
}
};
11 changes: 5 additions & 6 deletions plugins/gatsby-source-swiftype/src/createRelatedResourceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ module.exports = ({
createNodeId,
createContentDigest,
resource,
parentPathname,
}) => {
parent,
}) =>
createNode({
...resource,
id: createNodeId(`RelatedResource-${resource.url}`),
parent: null,
title: resource.title,
url: resource.url,
parent,
children: [],
plugin: 'gatsby-source-swiftype',
parentPathname,
internal: {
type: 'RelatedResource',
content: JSON.stringify(resource),
contentDigest: createContentDigest(resource),
},
});
};
23 changes: 23 additions & 0 deletions plugins/gatsby-source-swiftype/src/getRelatedResources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const search = require('./search');

module.exports = async ({ node, siteUrl }, pluginOptions) => {
const {
enabled,
engineKey,
pageLimit,
file,
getParams = () => ({}),
getPath,
} = pluginOptions;

const data = JSON.parse(fs.readFileSync(file));
const params = getParams({ node });
const pathname = getPath({ node });

if (enabled) {
return search(siteUrl + pathname, params, { engineKey, pageLimit });
}

return data[pathname] || [];
};

0 comments on commit 753cd77

Please sign in to comment.