xpine 0.0.28 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,16 +40,26 @@ async function getNewPageContent(href) {
40
40
  const dom = parser.parseFromString(text, "text/html");
41
41
  diffHead(dom);
42
42
  const newScripts = removeBodyScripts(dom);
43
- const clonedPersistentNodes = getPersistentNodesFromDocument(dom);
44
- const body = dom.body.innerHTML;
45
- document.body.innerHTML = body;
46
- for (const clonedNode of clonedPersistentNodes) {
47
- const newNode = document.body.querySelector(`[data-persistent="${clonedNode?.getAttribute("data-persistent")}"]`);
48
- newNode.replaceWith(clonedNode);
43
+ const { persistentArea, persistentElements } = replacePersistentNodesOnDocument(dom);
44
+ const documentRoot = document.body.querySelector("#xpine-root");
45
+ const domRoot = dom?.body?.querySelector("#xpine-root");
46
+ if (!documentRoot || !domRoot) {
47
+ throw {
48
+ message: "Every page must be wrapped in an #xpine-root tag",
49
+ documentRoot,
50
+ domRoot
51
+ };
49
52
  }
53
+ documentRoot?.replaceWith(domRoot);
54
+ for (const el of persistentElements) {
55
+ const newNode = document.body.querySelector(`[data-persistent="${el?.getAttribute("data-persistent")}"]`);
56
+ newNode.replaceWith(el);
57
+ }
58
+ persistentArea.remove();
50
59
  replaceAttributesOnDocumentBody(dom);
51
60
  replaceDocumentContentsWithLinkResponse();
52
- for (const script of newScripts) document.body.appendChild(script);
61
+ const newDocumentRoot = document.body.querySelector("#xpine-root");
62
+ for (const script of newScripts) newDocumentRoot.appendChild(script);
53
63
  const event = new CustomEvent("spa:updatePageContent", {
54
64
  detail: {
55
65
  href
@@ -100,7 +110,7 @@ function removeBodyScripts(dom) {
100
110
  const element = document.createElement("script");
101
111
  portAttributesToElement(script, element);
102
112
  element.innerHTML = script.innerHTML;
103
- dom.body.removeChild(script);
113
+ script.remove();
104
114
  newElements.push(element);
105
115
  }
106
116
  return newElements;
@@ -110,13 +120,25 @@ function portAttributesToElement(oldEl, newEl) {
110
120
  newEl.setAttribute(attribute.name, attribute.nodeValue);
111
121
  }
112
122
  }
113
- function getPersistentNodesFromDocument(dom) {
114
- const persistentNodesFromDom = [...dom.querySelectorAll("[data-persistent]")].map((el) => el.getAttribute("data-persistent"));
123
+ function replacePersistentNodesOnDocument(dom) {
124
+ const persistentArea = document.createElement("div");
125
+ persistentArea.style.display = "none";
126
+ document.body.appendChild(persistentArea);
127
+ const domPersistentNodes = [...dom.querySelectorAll("[data-persistent]")];
128
+ const persistentNodesFromDom = domPersistentNodes.map((el) => el.getAttribute("data-persistent"));
115
129
  const persistentNodesFromBody = [...document.body.querySelectorAll("[data-persistent]")];
116
- const validPersistentNodes = persistentNodesFromBody.filter((el) => {
117
- return persistentNodesFromDom.includes(el.getAttribute("data-persistent"));
130
+ const persistentElements = [];
131
+ persistentNodesFromBody.forEach((el) => {
132
+ const persistentAttribute = el.getAttribute("data-persistent");
133
+ if (!persistentNodesFromDom.includes(persistentAttribute)) return false;
134
+ dom.querySelector(`[data-persistent="${persistentAttribute}"]`).innerHTML = "";
135
+ persistentArea.appendChild(el);
136
+ persistentElements.push(el);
118
137
  });
119
- return validPersistentNodes.map((el) => el.cloneNode(true));
138
+ return {
139
+ persistentElements,
140
+ persistentArea
141
+ };
120
142
  }
121
143
  function replaceAttributesOnDocumentBody(dom) {
122
144
  while (document.body.attributes.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xpine",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "main": "dist/index.js",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-secrets-manager": "^3.758.0",
@@ -1,3 +0,0 @@
1
- import { ConfigFile } from "../../../types";
2
- export default function createOnInit(onInit: ConfigFile['onInit']): () => Promise<void>;
3
- //# sourceMappingURL=onInit.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"onInit.d.ts","sourceRoot":"","sources":["../../../../src/util/hooks/onInit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAI5C,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,uBAOhE"}