@ecopages/browser-router 0.7.0 → 0.7.1

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.
package/README.md CHANGED
@@ -166,6 +166,17 @@ document.addEventListener('eco:page-load', (e) => {
166
166
  });
167
167
  ```
168
168
 
169
+ `e.detail.newDocument` is the parsed incoming document and a listener may change it. Custom elements the router inserts or replaces are the nodes of that document themselves, so properties you set on them, and their nested nodes, are still there when the browser upgrades and connects them:
170
+
171
+ ```ts
172
+ document.addEventListener('eco:before-swap', (e) => {
173
+ const host = e.detail.newDocument.querySelector('my-host');
174
+ if (host) host.engine = engine;
175
+ });
176
+ ```
177
+
178
+ Elements the router keeps in place, such as persisted elements, ignore properties set on their incoming counterpart. A property set before an element upgrades shadows a class accessor of the same name and is reset by a class field of that name: the component should read the own value, delete it and assign it through the accessor in its constructor.
179
+
169
180
  ## Programmatic Navigation
170
181
 
171
182
  Use the router instance to navigate programmatically:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecopages/browser-router",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Client-side router for Ecopages with view transitions support",
5
5
  "keywords": [
6
6
  "ecopages",
@@ -41,7 +41,7 @@
41
41
  "morphdom": "^2.7.8"
42
42
  },
43
43
  "peerDependencies": {
44
- "@ecopages/core": "0.7.0"
44
+ "@ecopages/core": "0.7.1"
45
45
  },
46
46
  "types": "./src/index.d.ts"
47
47
  }
@@ -46,8 +46,11 @@ export declare function processDeclarativeShadowDOM(root: Element | Document | S
46
46
  * also remain the same nodes when their markup stays structurally compatible;
47
47
  * changed child markup can replace nodes without reconnecting the host, so
48
48
  * components must handle rebinding.
49
- * Light-DOM custom elements are fully replaced to trigger their
50
- * disconnectedCallback → connectedCallback lifecycle.
49
+ * Light-DOM and hydrated custom elements are replaced by the incoming element itself, which triggers the
50
+ * disconnectedCallback → connectedCallback lifecycle. Elements the morph adds or replaces are the parsed
51
+ * nodes, so properties set on them in `eco:before-swap`, and the identity of their descendants, are preserved.
52
+ * Elements the morph keeps in place (persisted, retained, or plain elements it morphs) ignore properties set
53
+ * on their incoming counterpart.
51
54
  *
52
55
  * On a reload of the current URL, an element whose server markup is the same in the previous and the incoming
53
56
  * document is kept with its state, its runtime attributes and its children, including custom elements and their
@@ -149,35 +149,22 @@ function isLightDomCustomElement(element) {
149
149
  * Deferring the replacement until after morphdom finishes avoids this.
150
150
  */
151
151
  function deferElementReplacement(fromEl, toEl, server, deferred) {
152
- deferred.push({
153
- from: fromEl,
154
- to: toEl.cloneNode(true),
155
- server,
156
- isCustomElement: fromEl.localName.includes('-'),
157
- });
152
+ deferred.push({ from: fromEl, to: toEl, server });
158
153
  return false;
159
154
  }
160
- function materializeCustomElement(element) {
161
- const materializedElement = document.createElement(element.localName);
162
- for (const attr of element.attributes) {
163
- materializedElement.setAttribute(attr.name, attr.value);
164
- }
165
- materializedElement.innerHTML = element.innerHTML;
166
- return materializedElement;
167
- }
168
155
  /**
169
156
  * @remarks
170
- * Custom elements are recreated via `document.createElement` so the browser fires
157
+ * The incoming element itself replaces the live one, so properties set on it and the identity of its descendants
158
+ * survive. The browser disconnects the live element and upgrades and connects the incoming one, which fires
171
159
  * their full lifecycle. Elements already removed during the morph pass are skipped.
172
160
  */
173
161
  function flushDeferredReplacements(deferred, serverMarkup) {
174
- for (const { from, to, server, isCustomElement } of deferred) {
162
+ for (const { from, to, server } of deferred) {
175
163
  if (!from.isConnected)
176
164
  continue;
177
- const newEl = isCustomElement ? materializeCustomElement(to) : to.cloneNode(true);
178
165
  if (serverMarkup && server)
179
- recordServerSubtree(serverMarkup, newEl, server);
180
- from.replaceWith(newEl);
166
+ recordServerSubtree(serverMarkup, to, server);
167
+ from.replaceWith(to);
181
168
  }
182
169
  }
183
170
  /**
@@ -215,8 +202,11 @@ export function processDeclarativeShadowDOM(root) {
215
202
  * also remain the same nodes when their markup stays structurally compatible;
216
203
  * changed child markup can replace nodes without reconnecting the host, so
217
204
  * components must handle rebinding.
218
- * Light-DOM custom elements are fully replaced to trigger their
219
- * disconnectedCallback → connectedCallback lifecycle.
205
+ * Light-DOM and hydrated custom elements are replaced by the incoming element itself, which triggers the
206
+ * disconnectedCallback → connectedCallback lifecycle. Elements the morph adds or replaces are the parsed
207
+ * nodes, so properties set on them in `eco:before-swap`, and the identity of their descendants, are preserved.
208
+ * Elements the morph keeps in place (persisted, retained, or plain elements it morphs) ignore properties set
209
+ * on their incoming counterpart.
220
210
  *
221
211
  * On a reload of the current URL, an element whose server markup is the same in the previous and the incoming
222
212
  * document is kept with its state, its runtime attributes and its children, including custom elements and their
@@ -246,11 +236,10 @@ export function morphBody(newDocument, persistAttribute, { serverMarkup, reload
246
236
  morphdom(document.body, newDocument.body, {
247
237
  getNodeKey: (node) => getBodyMorphKey(node, persistAttribute),
248
238
  onBeforeNodeAdded: (node) => {
249
- const added = node instanceof Element && isLightDomCustomElement(node) ? materializeCustomElement(node) : node;
250
239
  const server = node instanceof Element ? incomingServerMarkup.get(node) : undefined;
251
- if (serverMarkup && server && added instanceof Element)
252
- recordServerSubtree(serverMarkup, added, server);
253
- return added;
240
+ if (serverMarkup && server && node instanceof Element)
241
+ recordServerSubtree(serverMarkup, node, server);
242
+ return node;
254
243
  },
255
244
  onBeforeElUpdated: (fromEl, toEl) => {
256
245
  if (isPersisted(fromEl, persistAttribute)) {