gatsby-plugin-fix-fouc
Version:
Gatsby Plugin Fix FOUC is a plugin that solves flicker of unstyled content
37 lines (36 loc) • 2.62 kB
JavaScript
;
exports.__esModule = true;
exports.onRenderBody = void 0;
var React = _interopRequireWildcard(require("react"));
var _ = require(".");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const onRenderBody = ({
setHeadComponents,
setBodyAttributes
}, pluginOptions) => {
var _pluginOptions$attrib, _pluginOptions$minWid, _pluginOptions$timeou;
const attributeName = (_pluginOptions$attrib = pluginOptions.attributeName) !== null && _pluginOptions$attrib !== void 0 ? _pluginOptions$attrib : _.defaultOptions.attributeName;
const minWidth = (_pluginOptions$minWid = pluginOptions.minWidth) !== null && _pluginOptions$minWid !== void 0 ? _pluginOptions$minWid : _.defaultOptions.minWidth;
const timeout = (_pluginOptions$timeou = pluginOptions.timeout) !== null && _pluginOptions$timeou !== void 0 ? _pluginOptions$timeou : _.defaultOptions.timeout;
setHeadComponents([/*#__PURE__*/React.createElement("style", {
key: "loading-screen-style",
dangerouslySetInnerHTML: {
__html: `body[data-${attributeName}]{opacity:1}` + `@media(min-width:${minWidth}px){body[data-${attributeName}]{opacity:0}}`
}
}), /*#__PURE__*/React.createElement("noscript", {
key: "loading-screen-noscript-style",
dangerouslySetInnerHTML: {
__html: `<style>` + `body[data-${attributeName}]{opacity:1!important}` + `</style>`
}
}), /*#__PURE__*/React.createElement("script", {
key: "loading-screen-fail-safe",
dangerouslySetInnerHTML: {
__html: `(function(){` + `setTimeout(function(){` + `try{` + `if(` + `document.body.dataset["${(0, _.toCamelCase)(attributeName)}"]!==undefined` + `){` + `delete document.body.dataset["${(0, _.toCamelCase)(attributeName)}"]` + `}` + `}catch(e){}` + `},${timeout})` + `})();`
}
})]);
setBodyAttributes({
[`data-${attributeName}`]: "true"
});
};
exports.onRenderBody = onRenderBody;