@shopify/react-html
Version:
A component to render your react app with no static HTML.
71 lines (70 loc) • 2.41 kB
JavaScript
;
var __values = (this && this.__values) || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SERIALIZE_ATTRIBUTE = 'data-serialized-id';
function getSerializationsFromDocument() {
var e_1, _a;
var serializations = new Map();
if (typeof document === 'undefined') {
return serializations;
}
try {
for (var _b = __values(document.querySelectorAll("[" + exports.SERIALIZE_ATTRIBUTE + "]")), _c = _b.next(); !_c.done; _c = _b.next()) {
var node = _c.value;
serializations.set(
// eslint-disable-next-line typescript/no-non-null-assertion
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() {
return (
// eslint-disable-next-line no-process-env
process.env.NODE_ENV === 'development' && typeof document !== 'undefined'
? new Promise(function (resolve) {
setTimeout(function () {
document.body.style.display = '';
resolve();
}, 0);
})
: Promise.resolve());
}
exports.showPage = showPage;