@shopify/react-html
Version:
A component to render your react app with no static HTML.
61 lines (60 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
exports.SERIALIZE_ATTRIBUTE = 'data-serialized-id';
exports.MANAGED_ATTRIBUTE = 'data-react-html';
function getSerializationsFromDocument() {
var e_1, _a;
var serializations = new Map();
if (typeof document === 'undefined') {
return serializations;
}
try {
for (var _b = tslib_1.__values(document.querySelectorAll("[" + exports.SERIALIZE_ATTRIBUTE + "]")), _c = _b.next(); !_c.done; _c = _b.next()) {
var node = _c.value;
serializations.set(node.getAttribute(exports.SERIALIZE_ATTRIBUTE), getSerializedFromNode(node));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return serializations;
}
exports.getSerializationsFromDocument = getSerializationsFromDocument;
function getSerializedFromNode(node) {
var value = node.textContent;
try {
return value ? JSON.parse(value) : undefined;
}
catch (_a) {
return undefined;
}
}
function getSerialized(id) {
var node = document.querySelector("[" + exports.SERIALIZE_ATTRIBUTE + "=\"" + id + "\"]");
if (node == null) {
throw new Error("No serializations found for id \"" + id + "\"");
}
return getSerializedFromNode(node);
}
exports.getSerialized = getSerialized;
// We hide the document in development by default in order to prevent
// flashes of unstyled content. This function will show the document
// after the styles have loaded.
function showPage() {
// eslint-disable-next-line no-process-env
return process.env.NODE_ENV === 'development' &&
typeof document !== 'undefined'
? new Promise(function (resolve) {
setTimeout(function () {
document.body.style.visibility = '';
resolve();
}, 0);
})
: Promise.resolve();
}
exports.showPage = showPage;