UNPKG

gatsby-source-wordpress

Version:

Source data from WordPress in an efficient and scalable way.

73 lines (68 loc) 3.05 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.sourceNodes = void 0; var _fetchNodeUpdates = _interopRequireDefault(require("./update-nodes/fetch-node-updates")); var _fetchNodes = require("./fetch-nodes/fetch-nodes"); var _constants = require("../../constants"); var _store = require("../../store"); var _fetchAndCreateNonNodeRootFields = _interopRequireDefault(require("./create-nodes/fetch-and-create-non-node-root-fields")); var _progressBarPromise = require("./create-nodes/create-remote-file-node/progress-bar-promise"); var _preview = require("../preview"); var _gatsbyFeatures = require("../../utils/gatsby-features"); const sourceNodes = async (helpers, pluginOptions) => { var _pluginOptions$schema, _pluginOptions$schema2; const { cache, webhookBody, refetchAll, actions } = helpers; const typePrefix = (_pluginOptions$schema = (_pluginOptions$schema2 = pluginOptions.schema) === null || _pluginOptions$schema2 === void 0 ? void 0 : _pluginOptions$schema2.typePrefix) !== null && _pluginOptions$schema !== void 0 ? _pluginOptions$schema : ``; if (_gatsbyFeatures.hasStatefulSourceNodes) { actions.enableStatefulSourceNodes(); } // fetch non-node root fields such as settings. // For now, we're refetching them on every build const nonNodeRootFieldsPromise = (0, _fetchAndCreateNonNodeRootFields.default)(); // if this is a preview we want to process it and return early if (webhookBody.token && webhookBody.userDatabaseId) { await (0, _preview.sourcePreviews)(helpers); await nonNodeRootFieldsPromise; return; } const now = Date.now(); const prefixedSourceTimeKey = (0, _store.withPluginKey)(_constants.LAST_COMPLETED_SOURCE_TIME); const lastCompletedSourceTime = webhookBody.refreshing && webhookBody.since ? webhookBody.since : await cache.get(prefixedSourceTimeKey); const { schemaWasChanged } = (0, _store.getStore)().getState().remoteSchema; const fetchEverything = !lastCompletedSourceTime || refetchAll || // don't refetch everything in development process.env.NODE_ENV !== `development` && // and the schema was changed schemaWasChanged; // If this is an uncached build, // or our initial build to fetch and cache everything didn't complete, // pull everything from WPGQL if (fetchEverything) { await (0, _fetchNodes.fetchAndCreateAllNodes)(); } // If we've already successfully pulled everything from WPGraphQL // just pull the latest changes else if (!fetchEverything) { await (0, _fetchNodeUpdates.default)({ since: lastCompletedSourceTime }); } await nonNodeRootFieldsPromise; (0, _progressBarPromise.allowFileDownloaderProgressBarToClear)(); await helpers.cache.set(prefixedSourceTimeKey, now); const { dispatch } = (0, _store.getStore)(); dispatch.remoteSchema.setSchemaWasChanged(false); dispatch.develop.resumeRefreshPolling(); }; exports.sourceNodes = sourceNodes; //# sourceMappingURL=index.js.map