@sigil-dev/compiler 0.11.3 → 0.11.4
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/package.json +1 -1
- package/src/babel/jsx/element.ts +33 -0
- package/test/hydration.test.ts +43 -2
package/package.json
CHANGED
package/src/babel/jsx/element.ts
CHANGED
|
@@ -761,12 +761,45 @@ export function processElement(
|
|
|
761
761
|
),
|
|
762
762
|
]),
|
|
763
763
|
);
|
|
764
|
+
} else if (hydrate && realAttr === "innerHTML") {
|
|
765
|
+
// A claimed element already holds the server's HTML for this
|
|
766
|
+
// value: the first run reads it (so the effect tracks it) and
|
|
767
|
+
// leaves the DOM alone, instead of parsing it all again (a long
|
|
768
|
+
// highlighted file is thousands of nodes).
|
|
769
|
+
const kept = t.identifier(`${varName}_kept`);
|
|
770
|
+
const v = t.identifier(`${varName}_html`);
|
|
771
|
+
statements.push(
|
|
772
|
+
t.variableDeclaration("let", [
|
|
773
|
+
t.variableDeclarator(kept, isClaimedCall(varName)),
|
|
774
|
+
]),
|
|
775
|
+
);
|
|
776
|
+
const stmt = effect([
|
|
777
|
+
t.variableDeclaration("const", [t.variableDeclarator(v, expr)]),
|
|
778
|
+
t.ifStatement(
|
|
779
|
+
kept,
|
|
780
|
+
t.blockStatement([
|
|
781
|
+
t.expressionStatement(
|
|
782
|
+
t.assignmentExpression("=", kept, t.booleanLiteral(false)),
|
|
783
|
+
),
|
|
784
|
+
t.returnStatement(),
|
|
785
|
+
]),
|
|
786
|
+
),
|
|
787
|
+
t.expressionStatement(setPropCall(varName, realAttr, v)),
|
|
788
|
+
]);
|
|
789
|
+
(afterChildren ? liveAfter : statements).push(stmt);
|
|
764
790
|
} else {
|
|
765
791
|
const stmt = effect([
|
|
766
792
|
t.expressionStatement(setPropCall(varName, realAttr, expr)),
|
|
767
793
|
]);
|
|
768
794
|
(afterChildren ? liveAfter : statements).push(stmt);
|
|
769
795
|
}
|
|
796
|
+
} else if (hydrate && realAttr === "innerHTML") {
|
|
797
|
+
// see above: a claimed element keeps the server's HTML
|
|
798
|
+
const stmt = t.ifStatement(
|
|
799
|
+
t.unaryExpression("!", isClaimedCall(varName)),
|
|
800
|
+
t.expressionStatement(setPropCall(varName, realAttr, expr)),
|
|
801
|
+
);
|
|
802
|
+
(afterChildren ? initAfter : init).push(stmt);
|
|
770
803
|
} else {
|
|
771
804
|
const stmt = t.expressionStatement(
|
|
772
805
|
setPropCall(varName, realAttr, expr),
|
package/test/hydration.test.ts
CHANGED
|
@@ -219,6 +219,41 @@ describe("Hydration", () => {
|
|
|
219
219
|
expect(host.innerHTML).toBe(html);
|
|
220
220
|
});
|
|
221
221
|
|
|
222
|
+
test("innerHTML on a claimed element keeps the server's HTML, even with the value left out", async () => {
|
|
223
|
+
const src = `
|
|
224
|
+
export const App = (data) => (
|
|
225
|
+
<section><pre class="code" innerHTML={data.code} /></section>
|
|
226
|
+
);
|
|
227
|
+
`;
|
|
228
|
+
const html = String(
|
|
229
|
+
evalApp(transform(src, { mode: "ssr" }))({ code: "<b>big</b>" }),
|
|
230
|
+
);
|
|
231
|
+
const host = document.createElement("div");
|
|
232
|
+
host.innerHTML = html;
|
|
233
|
+
const pre = host.querySelector("pre")!;
|
|
234
|
+
const bold = pre.firstChild;
|
|
235
|
+
|
|
236
|
+
const app = evalApp(transform(src, { mode: "hydrate" })) as (
|
|
237
|
+
d: unknown,
|
|
238
|
+
) => unknown;
|
|
239
|
+
runtime.pushHydrationNodes(Array.from(host.childNodes));
|
|
240
|
+
// even a value the client never got: the server rendered it, hydration keeps it
|
|
241
|
+
app({});
|
|
242
|
+
runtime.popHydrationNodes();
|
|
243
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
244
|
+
expect(host.innerHTML).toBe(html);
|
|
245
|
+
// not parsed again: the same node
|
|
246
|
+
expect(pre.firstChild).toBe(bold);
|
|
247
|
+
|
|
248
|
+
// a fresh render (SPA navigation) still writes it
|
|
249
|
+
const fresh = document.createElement("div");
|
|
250
|
+
runtime.pushHydrationNodes([]);
|
|
251
|
+
runtime.insert(fresh, app({ code: "<i>nav</i>" }));
|
|
252
|
+
runtime.popHydrationNodes();
|
|
253
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
254
|
+
expect(fresh.querySelector("pre")!.innerHTML).toBe("<i>nav</i>");
|
|
255
|
+
});
|
|
256
|
+
|
|
222
257
|
test("component children render once on an empty pool (SPA navigation)", () => {
|
|
223
258
|
const src = `
|
|
224
259
|
function Wrap({ children }) { return <div class="wrap">{children}</div>; }
|
|
@@ -342,7 +377,11 @@ describe("Hydration", () => {
|
|
|
342
377
|
host.append(root);
|
|
343
378
|
const input = host.querySelector("input.q");
|
|
344
379
|
expect(host.querySelectorAll("a").length).toBe(2);
|
|
345
|
-
(globalThis as { __keyedSet?: (v: string[]) => void }).__keyedSet!([
|
|
380
|
+
(globalThis as { __keyedSet?: (v: string[]) => void }).__keyedSet!([
|
|
381
|
+
"a",
|
|
382
|
+
"b",
|
|
383
|
+
"c",
|
|
384
|
+
]);
|
|
346
385
|
await new Promise((r) => setTimeout(r, 0));
|
|
347
386
|
expect(host.querySelectorAll("a").length).toBe(3);
|
|
348
387
|
expect(host.querySelector("input.q")).toBe(input);
|
|
@@ -366,7 +405,9 @@ describe("Hydration", () => {
|
|
|
366
405
|
const html = String(evalApp(transform(src, { mode: "ssr" }))(props));
|
|
367
406
|
const host = document.createElement("div");
|
|
368
407
|
host.innerHTML = html;
|
|
369
|
-
const app = evalApp(transform(src, { mode: "hydrate" })) as (
|
|
408
|
+
const app = evalApp(transform(src, { mode: "hydrate" })) as (
|
|
409
|
+
p: typeof props,
|
|
410
|
+
) => unknown;
|
|
370
411
|
runtime.pushHydrationNodes(Array.from(host.childNodes));
|
|
371
412
|
app(props);
|
|
372
413
|
runtime.popHydrationNodes();
|