@ecopages/browser-router 0.7.0 → 0.8.0
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 +13 -0
- package/package.json +2 -2
- package/src/client/dom/body-morpher.d.ts +5 -2
- package/src/client/dom/body-morpher.js +14 -25
package/README.md
CHANGED
|
@@ -104,6 +104,8 @@ Call `router.stop()` before replacing the router script in long-lived sessions (
|
|
|
104
104
|
|
|
105
105
|
## Persistence
|
|
106
106
|
|
|
107
|
+
Unless persisted, a custom element that exists on both pages is replaced by the incoming element on navigation: it upgrades from its own server markup and does not keep the live element's state.
|
|
108
|
+
|
|
107
109
|
Mark elements with the same persistence key on each page to preserve them across navigations. When the incoming page keeps the element under a compatible ancestor structure, it stays connected and is never recreated; morphdom skips it entirely. This holds when view transitions are disabled and when the incoming page has `data-eco-rerun` scripts. Changing the element's ancestor tags can require replacing its containing path, so keep that structure stable when connection continuity matters.
|
|
108
110
|
|
|
109
111
|
On navigation to another URL, when an incoming page has a `data-eco-rerun` script, browser-router refreshes matching non-persisted elements before running it, except for the body, persisted elements, their ancestor path, and descendants of custom elements on that path. Those custom-element descendants stay in place because the connected host may have attached handlers to them. Keep handler-bound child markup structurally compatible across pages, or have the component rebind when its child markup changes; replacing a child does not reconnect the host. Event handlers installed on refreshed elements attach to new nodes instead of stacking on nodes retained by the morph. A rerun script that installs handlers on any retained node must make that registration idempotent, because those nodes remain attached across navigations.
|
|
@@ -166,6 +168,17 @@ document.addEventListener('eco:page-load', (e) => {
|
|
|
166
168
|
});
|
|
167
169
|
```
|
|
168
170
|
|
|
171
|
+
`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:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
document.addEventListener('eco:before-swap', (e) => {
|
|
175
|
+
const host = e.detail.newDocument.querySelector('my-host');
|
|
176
|
+
if (host) host.engine = engine;
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Elements the router keeps in place, such as persisted elements, ignore properties set on their incoming counterpart. Radiant `@prop` members adopt a value set before the element upgrades, so Radiant hosts need nothing. For other libraries, a property set before upgrade 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.
|
|
181
|
+
|
|
169
182
|
## Programmatic Navigation
|
|
170
183
|
|
|
171
184
|
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.
|
|
3
|
+
"version": "0.8.0",
|
|
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.
|
|
44
|
+
"@ecopages/core": "0.8.0"
|
|
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
|
|
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
|
-
*
|
|
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
|
|
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,
|
|
180
|
-
from.replaceWith(
|
|
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
|
|
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 &&
|
|
252
|
-
recordServerSubtree(serverMarkup,
|
|
253
|
-
return
|
|
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)) {
|