UNPKG

@dark-engine/platform-server

Version:
29 lines (28 loc) 890 B
import { STATE_SCRIPT_TYPE, $$scope } from '@dark-engine/core'; import { render } from '../render'; import { unmount } from '../create-root'; import { illegal } from '../utils'; function hydrateRoot(container, element) { const tag = container; render(element, tag, hydrate); return { unmount: () => unmount(tag), }; } function hydrate() { const element = document.querySelector(`script[type="${STATE_SCRIPT_TYPE}"]`); if (!element) return; try { const resources = parse(element.textContent); const $scope = $$scope(); for (const key of Object.keys(resources)) { $scope.setResource(Number(key), resources[key]); } element.remove(); } catch (error) { illegal(`Can't hydrate app state from the server!`); } } const parse = x => JSON.parse(window.atob(x.replaceAll('"', ''))); export { hydrateRoot }; //# sourceMappingURL=hydrate-root.js.map