UNPKG

gatsby-plugin-workerize-loader

Version:
42 lines (32 loc) 1.12 kB
"use strict"; const React = require('react'); const { readdirSync } = require('fs'); const { PREFIX } = require('./constants'); const publicFolder = './public'; const isWorker = file => file.includes(PREFIX) && file.endsWith('.worker.js'); const shouldPreload = (file, preloads) => file.startsWith(PREFIX) && preloads.some(preload => file.includes(preload)); let preloadScripts = []; // For some reason, this does not work as expected on Chrome. // So for now, I'm not mentioning preloads option in README. // // Be aware that adding preloads will prevent Gatsby to use the `GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES` flag exports.onRenderBody = ({ setHeadComponents }, { preloads = [] } = {}) => { if (!preloads.length) return; if (!preloadScripts.length) { preloadScripts = readdirSync(publicFolder).filter(file => isWorker(file) && shouldPreload(file, preloads)).map(file => /*#__PURE__*/React.createElement("link", { key: file, as: "worker", crossOrigin: "anonymous", href: `/${file}`, rel: "preload" })); } setHeadComponents(preloadScripts); };