gatsby-plugin-loadable-components-ssr-fix
Version:
Server-side rendering loadable components in your gatsby application
37 lines (26 loc) • 1.25 kB
JavaScript
;
exports.__esModule = true;
exports.onRenderBody = exports.wrapRootElement = void 0;
var _fs = require("fs");
var _server = require("@loadable/server");
var _constants = require("./constants");
const extractor = new _server.ChunkExtractor({
// Read the stats file generated by webpack loadable plugin.
// The file will not exist in develop stages.
stats: (0, _fs.existsSync)(_constants.statsPath) ? JSON.parse((0, _fs.readFileSync)(_constants.statsPath, 'utf8')) : {},
entrypoints: []
}); // extractor.collectChunks() will wrap the application in a ChunkExtractorManager
const wrapRootElement = ({
element
}) => extractor.collectChunks(element);
exports.wrapRootElement = wrapRootElement;
const onRenderBody = ({
setPostBodyComponents,
setHeadComponents
}) => {
// Set link rel="preload" tags in the head to start the request asap. This will NOT parse the assets fetched
setHeadComponents(extractor.getLinkElements()); // Set script and style tags at the end of the document to parse the assets.
setPostBodyComponents([...extractor.getScriptElements(), ...extractor.getStyleElements()]); // Reset collected chunks after each page is rendered
extractor.chunks = [];
};
exports.onRenderBody = onRenderBody;