From b3a3bd1069faf010dee83d979874180da6d36bf0 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 5 Oct 2023 12:04:38 +0200 Subject: [PATCH] feat: add support for partial merging (#1863) --- src/runtime/entrypoints/main.ts | 61 ++++- tests/fixture_partials/fresh.gen.ts | 210 +++++++++--------- .../routes/head_merge/index.tsx | 28 +++ .../routes/head_merge/injected.tsx | 10 + .../routes/head_merge/update.tsx | 30 +++ tests/fixture_partials/static/other.css | 3 + tests/partials_test.ts | 35 ++- tests/test_utils.ts | 24 ++ 8 files changed, 297 insertions(+), 104 deletions(-) create mode 100644 tests/fixture_partials/routes/head_merge/index.tsx create mode 100644 tests/fixture_partials/routes/head_merge/injected.tsx create mode 100644 tests/fixture_partials/routes/head_merge/update.tsx create mode 100644 tests/fixture_partials/static/other.css diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index ec8bcc13..70c4abed 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -10,7 +10,7 @@ import { render, VNode, } from "preact"; -import { assetHashingHook } from "../utils.ts"; +import { assetHashingHook, INTERNAL_PREFIX } from "../utils.ts"; import { type SerializedState } from "../../server/rendering/fresh_tags.tsx"; import type { Signal } from "@preact/signals"; import { @@ -711,6 +711,65 @@ export async function applyPartials(res: Response): Promise { ); } + // Update + document.title = doc.title; + + // Needs to be converted to an array otherwise somehow -tags + // are missing. + Array.from(doc.head.childNodes).forEach((childNode) => { + const child = childNode as HTMLElement; + + if (child.nodeName === "TITLE") return; + if (child.nodeName === "META") { + const meta = child as HTMLMetaElement; + + // Ignore charset which is usually set site wide anyway + if (meta.hasAttribute("charset")) return; + + const name = meta.name; + if (name !== "") { + const existing = document.head.querySelector(`meta[name="${name}"]`) as + | HTMLMetaElement + | null; + if (existing !== null) { + if (existing.content !== meta.content) { + existing.content = meta.content; + } + } else { + document.head.appendChild(meta); + } + } else { + const property = child.getAttribute("property"); + const existing = document.head.querySelector( + `meta[property="${property}"]`, + ) as HTMLMetaElement | null; + if (existing !== null) { + if (existing.content !== meta.content) { + existing.content = meta.content; + } + } else { + document.head.appendChild(meta); + } + } + } else if (child.nodeName === "LINK") { + const link = child as HTMLLinkElement; + if (link.rel === "modulepreload") return; + if (link.rel === "stylesheet") { + if (document.head.querySelector(`link[href="${link.href}"]`) === null) { + document.head.appendChild(link); + } + } + } else if (child.nodeName === "SCRIPT") { + const script = child as HTMLScriptElement; + if (script.src === `${INTERNAL_PREFIX}/refresh.js`) return; + // TODO: What to do with script tags? + } else if (child.nodeName === "STYLE") { + const style = child as HTMLStyleElement; + // TODO: Do we need a smarter merging strategy? + document.head.appendChild(style); + } + }); + // Update all encountered partials for (let i = 0; i < encounteredPartials.length; i++) { const { vnode, marker } = encounteredPartials[i]; diff --git a/tests/fixture_partials/fresh.gen.ts b/tests/fixture_partials/fresh.gen.ts index 27b25e39..899ebf6a 100644 --- a/tests/fixture_partials/fresh.gen.ts +++ b/tests/fixture_partials/fresh.gen.ts @@ -31,57 +31,60 @@ import * as $25 from "./routes/form/index.tsx"; import * as $26 from "./routes/form/injected.tsx"; import * as $27 from "./routes/form/update.tsx"; import * as $28 from "./routes/fragment_nav.tsx"; -import * as $29 from "./routes/index.tsx"; -import * as $30 from "./routes/island_instance/index.tsx"; -import * as $31 from "./routes/island_instance/injected.tsx"; -import * as $32 from "./routes/island_instance/partial.tsx"; -import * as $33 from "./routes/island_instance/partial_remove.tsx"; -import * as $34 from "./routes/island_instance/partial_replace.tsx"; -import * as $35 from "./routes/island_instance_multiple/index.tsx"; -import * as $36 from "./routes/island_instance_multiple/injected.tsx"; -import * as $37 from "./routes/island_instance_multiple/partial.tsx"; -import * as $38 from "./routes/island_instance_multiple/partial_both.tsx"; -import * as $39 from "./routes/island_instance_nested/index.tsx"; -import * as $40 from "./routes/island_instance_nested/injected.tsx"; -import * as $41 from "./routes/island_instance_nested/partial.tsx"; -import * as $42 from "./routes/island_instance_nested/replace.tsx"; -import * as $43 from "./routes/island_props/index.tsx"; -import * as $44 from "./routes/island_props/injected.tsx"; -import * as $45 from "./routes/island_props/partial.tsx"; -import * as $46 from "./routes/island_props_signals/index.tsx"; -import * as $47 from "./routes/island_props_signals/injected.tsx"; -import * as $48 from "./routes/island_props_signals/partial.tsx"; -import * as $49 from "./routes/keys/index.tsx"; -import * as $50 from "./routes/keys/injected.tsx"; -import * as $51 from "./routes/keys/swap.tsx"; -import * as $52 from "./routes/keys_components/index.tsx"; -import * as $53 from "./routes/keys_components/injected.tsx"; -import * as $54 from "./routes/keys_components/swap.tsx"; -import * as $55 from "./routes/keys_dom/index.tsx"; -import * as $56 from "./routes/keys_dom/injected.tsx"; -import * as $57 from "./routes/keys_dom/swap.tsx"; -import * as $58 from "./routes/keys_outside/index.tsx"; -import * as $59 from "./routes/loading/index.tsx"; -import * as $60 from "./routes/loading/injected.tsx"; -import * as $61 from "./routes/loading/update.tsx"; -import * as $62 from "./routes/missing_partial/index.tsx"; -import * as $63 from "./routes/missing_partial/injected.tsx"; -import * as $64 from "./routes/missing_partial/update.tsx"; -import * as $65 from "./routes/mode/append.tsx"; -import * as $66 from "./routes/mode/index.tsx"; -import * as $67 from "./routes/mode/injected.tsx"; -import * as $68 from "./routes/mode/prepend.tsx"; -import * as $69 from "./routes/mode/replace.tsx"; -import * as $70 from "./routes/no_islands/index.tsx"; -import * as $71 from "./routes/no_islands/injected.tsx"; -import * as $72 from "./routes/no_islands/update.tsx"; -import * as $73 from "./routes/no_partial_response/index.tsx"; -import * as $74 from "./routes/no_partial_response/injected.tsx"; -import * as $75 from "./routes/no_partial_response/update.tsx"; -import * as $76 from "./routes/partial_slot_inside_island.tsx"; -import * as $77 from "./routes/scroll_restoration/index.tsx"; -import * as $78 from "./routes/scroll_restoration/injected.tsx"; -import * as $79 from "./routes/scroll_restoration/update.tsx"; +import * as $29 from "./routes/head_merge/index.tsx"; +import * as $30 from "./routes/head_merge/injected.tsx"; +import * as $31 from "./routes/head_merge/update.tsx"; +import * as $32 from "./routes/index.tsx"; +import * as $33 from "./routes/island_instance/index.tsx"; +import * as $34 from "./routes/island_instance/injected.tsx"; +import * as $35 from "./routes/island_instance/partial.tsx"; +import * as $36 from "./routes/island_instance/partial_remove.tsx"; +import * as $37 from "./routes/island_instance/partial_replace.tsx"; +import * as $38 from "./routes/island_instance_multiple/index.tsx"; +import * as $39 from "./routes/island_instance_multiple/injected.tsx"; +import * as $40 from "./routes/island_instance_multiple/partial.tsx"; +import * as $41 from "./routes/island_instance_multiple/partial_both.tsx"; +import * as $42 from "./routes/island_instance_nested/index.tsx"; +import * as $43 from "./routes/island_instance_nested/injected.tsx"; +import * as $44 from "./routes/island_instance_nested/partial.tsx"; +import * as $45 from "./routes/island_instance_nested/replace.tsx"; +import * as $46 from "./routes/island_props/index.tsx"; +import * as $47 from "./routes/island_props/injected.tsx"; +import * as $48 from "./routes/island_props/partial.tsx"; +import * as $49 from "./routes/island_props_signals/index.tsx"; +import * as $50 from "./routes/island_props_signals/injected.tsx"; +import * as $51 from "./routes/island_props_signals/partial.tsx"; +import * as $52 from "./routes/keys/index.tsx"; +import * as $53 from "./routes/keys/injected.tsx"; +import * as $54 from "./routes/keys/swap.tsx"; +import * as $55 from "./routes/keys_components/index.tsx"; +import * as $56 from "./routes/keys_components/injected.tsx"; +import * as $57 from "./routes/keys_components/swap.tsx"; +import * as $58 from "./routes/keys_dom/index.tsx"; +import * as $59 from "./routes/keys_dom/injected.tsx"; +import * as $60 from "./routes/keys_dom/swap.tsx"; +import * as $61 from "./routes/keys_outside/index.tsx"; +import * as $62 from "./routes/loading/index.tsx"; +import * as $63 from "./routes/loading/injected.tsx"; +import * as $64 from "./routes/loading/update.tsx"; +import * as $65 from "./routes/missing_partial/index.tsx"; +import * as $66 from "./routes/missing_partial/injected.tsx"; +import * as $67 from "./routes/missing_partial/update.tsx"; +import * as $68 from "./routes/mode/append.tsx"; +import * as $69 from "./routes/mode/index.tsx"; +import * as $70 from "./routes/mode/injected.tsx"; +import * as $71 from "./routes/mode/prepend.tsx"; +import * as $72 from "./routes/mode/replace.tsx"; +import * as $73 from "./routes/no_islands/index.tsx"; +import * as $74 from "./routes/no_islands/injected.tsx"; +import * as $75 from "./routes/no_islands/update.tsx"; +import * as $76 from "./routes/no_partial_response/index.tsx"; +import * as $77 from "./routes/no_partial_response/injected.tsx"; +import * as $78 from "./routes/no_partial_response/update.tsx"; +import * as $79 from "./routes/partial_slot_inside_island.tsx"; +import * as $80 from "./routes/scroll_restoration/index.tsx"; +import * as $81 from "./routes/scroll_restoration/injected.tsx"; +import * as $82 from "./routes/scroll_restoration/update.tsx"; import * as $$0 from "./islands/Counter.tsx"; import * as $$1 from "./islands/CounterA.tsx"; import * as $$2 from "./islands/CounterB.tsx"; @@ -128,57 +131,60 @@ const manifest = { "./routes/form/injected.tsx": $26, "./routes/form/update.tsx": $27, "./routes/fragment_nav.tsx": $28, - "./routes/index.tsx": $29, - "./routes/island_instance/index.tsx": $30, - "./routes/island_instance/injected.tsx": $31, - "./routes/island_instance/partial.tsx": $32, - "./routes/island_instance/partial_remove.tsx": $33, - "./routes/island_instance/partial_replace.tsx": $34, - "./routes/island_instance_multiple/index.tsx": $35, - "./routes/island_instance_multiple/injected.tsx": $36, - "./routes/island_instance_multiple/partial.tsx": $37, - "./routes/island_instance_multiple/partial_both.tsx": $38, - "./routes/island_instance_nested/index.tsx": $39, - "./routes/island_instance_nested/injected.tsx": $40, - "./routes/island_instance_nested/partial.tsx": $41, - "./routes/island_instance_nested/replace.tsx": $42, - "./routes/island_props/index.tsx": $43, - "./routes/island_props/injected.tsx": $44, - "./routes/island_props/partial.tsx": $45, - "./routes/island_props_signals/index.tsx": $46, - "./routes/island_props_signals/injected.tsx": $47, - "./routes/island_props_signals/partial.tsx": $48, - "./routes/keys/index.tsx": $49, - "./routes/keys/injected.tsx": $50, - "./routes/keys/swap.tsx": $51, - "./routes/keys_components/index.tsx": $52, - "./routes/keys_components/injected.tsx": $53, - "./routes/keys_components/swap.tsx": $54, - "./routes/keys_dom/index.tsx": $55, - "./routes/keys_dom/injected.tsx": $56, - "./routes/keys_dom/swap.tsx": $57, - "./routes/keys_outside/index.tsx": $58, - "./routes/loading/index.tsx": $59, - "./routes/loading/injected.tsx": $60, - "./routes/loading/update.tsx": $61, - "./routes/missing_partial/index.tsx": $62, - "./routes/missing_partial/injected.tsx": $63, - "./routes/missing_partial/update.tsx": $64, - "./routes/mode/append.tsx": $65, - "./routes/mode/index.tsx": $66, - "./routes/mode/injected.tsx": $67, - "./routes/mode/prepend.tsx": $68, - "./routes/mode/replace.tsx": $69, - "./routes/no_islands/index.tsx": $70, - "./routes/no_islands/injected.tsx": $71, - "./routes/no_islands/update.tsx": $72, - "./routes/no_partial_response/index.tsx": $73, - "./routes/no_partial_response/injected.tsx": $74, - "./routes/no_partial_response/update.tsx": $75, - "./routes/partial_slot_inside_island.tsx": $76, - "./routes/scroll_restoration/index.tsx": $77, - "./routes/scroll_restoration/injected.tsx": $78, - "./routes/scroll_restoration/update.tsx": $79, + "./routes/head_merge/index.tsx": $29, + "./routes/head_merge/injected.tsx": $30, + "./routes/head_merge/update.tsx": $31, + "./routes/index.tsx": $32, + "./routes/island_instance/index.tsx": $33, + "./routes/island_instance/injected.tsx": $34, + "./routes/island_instance/partial.tsx": $35, + "./routes/island_instance/partial_remove.tsx": $36, + "./routes/island_instance/partial_replace.tsx": $37, + "./routes/island_instance_multiple/index.tsx": $38, + "./routes/island_instance_multiple/injected.tsx": $39, + "./routes/island_instance_multiple/partial.tsx": $40, + "./routes/island_instance_multiple/partial_both.tsx": $41, + "./routes/island_instance_nested/index.tsx": $42, + "./routes/island_instance_nested/injected.tsx": $43, + "./routes/island_instance_nested/partial.tsx": $44, + "./routes/island_instance_nested/replace.tsx": $45, + "./routes/island_props/index.tsx": $46, + "./routes/island_props/injected.tsx": $47, + "./routes/island_props/partial.tsx": $48, + "./routes/island_props_signals/index.tsx": $49, + "./routes/island_props_signals/injected.tsx": $50, + "./routes/island_props_signals/partial.tsx": $51, + "./routes/keys/index.tsx": $52, + "./routes/keys/injected.tsx": $53, + "./routes/keys/swap.tsx": $54, + "./routes/keys_components/index.tsx": $55, + "./routes/keys_components/injected.tsx": $56, + "./routes/keys_components/swap.tsx": $57, + "./routes/keys_dom/index.tsx": $58, + "./routes/keys_dom/injected.tsx": $59, + "./routes/keys_dom/swap.tsx": $60, + "./routes/keys_outside/index.tsx": $61, + "./routes/loading/index.tsx": $62, + "./routes/loading/injected.tsx": $63, + "./routes/loading/update.tsx": $64, + "./routes/missing_partial/index.tsx": $65, + "./routes/missing_partial/injected.tsx": $66, + "./routes/missing_partial/update.tsx": $67, + "./routes/mode/append.tsx": $68, + "./routes/mode/index.tsx": $69, + "./routes/mode/injected.tsx": $70, + "./routes/mode/prepend.tsx": $71, + "./routes/mode/replace.tsx": $72, + "./routes/no_islands/index.tsx": $73, + "./routes/no_islands/injected.tsx": $74, + "./routes/no_islands/update.tsx": $75, + "./routes/no_partial_response/index.tsx": $76, + "./routes/no_partial_response/injected.tsx": $77, + "./routes/no_partial_response/update.tsx": $78, + "./routes/partial_slot_inside_island.tsx": $79, + "./routes/scroll_restoration/index.tsx": $80, + "./routes/scroll_restoration/injected.tsx": $81, + "./routes/scroll_restoration/update.tsx": $82, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/tests/fixture_partials/routes/head_merge/index.tsx b/tests/fixture_partials/routes/head_merge/index.tsx new file mode 100644 index 00000000..b1d4f3db --- /dev/null +++ b/tests/fixture_partials/routes/head_merge/index.tsx @@ -0,0 +1,28 @@ +import { Head, Partial } from "$fresh/runtime.ts"; +import { Fader } from "../../islands/Fader.tsx"; + +export default function SlotDemo() { + return ( +
+ + Head merge + + + + + +

Initial content

+
+
+

+ + update + +

+
+ ); +} diff --git a/tests/fixture_partials/routes/head_merge/injected.tsx b/tests/fixture_partials/routes/head_merge/injected.tsx new file mode 100644 index 00000000..a3fdc9c3 --- /dev/null +++ b/tests/fixture_partials/routes/head_merge/injected.tsx @@ -0,0 +1,10 @@ +import { defineRoute } from "$fresh/src/server/defines.ts"; + +export default defineRoute(() => { + return new Response("", { + status: 302, + headers: { + Location: "/head_merge", + }, + }); +}); diff --git a/tests/fixture_partials/routes/head_merge/update.tsx b/tests/fixture_partials/routes/head_merge/update.tsx new file mode 100644 index 00000000..fe7cf772 --- /dev/null +++ b/tests/fixture_partials/routes/head_merge/update.tsx @@ -0,0 +1,30 @@ +import { defineRoute } from "$fresh/src/server/defines.ts"; +import { RouteConfig } from "$fresh/server.ts"; +import { Head, Partial } from "$fresh/runtime.ts"; +import { Fader } from "../../islands/Fader.tsx"; + +export const config: RouteConfig = { + skipAppWrapper: true, + skipInheritedLayouts: true, +}; + +export default defineRoute((req, ctx) => { + return ( + <> + + Head merge updated + + + + + + + + +

updated

+

updated content

+
+
+ + ); +}); diff --git a/tests/fixture_partials/static/other.css b/tests/fixture_partials/static/other.css new file mode 100644 index 00000000..adc68fa6 --- /dev/null +++ b/tests/fixture_partials/static/other.css @@ -0,0 +1,3 @@ +h1 { + color: red; +} diff --git a/tests/partials_test.ts b/tests/partials_test.ts index 9b1fd169..07ae1701 100644 --- a/tests/partials_test.ts +++ b/tests/partials_test.ts @@ -1,6 +1,7 @@ import { assert, assertEquals, assertMatch } from "$std/testing/asserts.ts"; import { Page } from "./deps.ts"; import { + assertMetaContent, assertNoComments, assertNoPageComments, assertNotSelector, @@ -1026,4 +1027,36 @@ Deno.test("throws an error when response contains no partials", async () => { ); }); -// TODO Head merging +Deno.test("merges content", async () => { + await withPageName( + "./tests/fixture_partials/main.ts", + async (page, address) => { + await page.goto(`${address}/head_merge`); + await page.waitForSelector(".status-initial"); + + await page.click(".update-link"); + await page.waitForSelector(".status-updated"); + + await waitFor(async () => { + return (await page.title()) === "Head merge updated"; + }); + + const doc = parseHtml(await page.content()); + assertEquals(doc.title, "Head merge updated"); + + assertMetaContent(doc, "foo", "bar baz"); + assertMetaContent(doc, "og:foo", "og value foo"); + assertMetaContent(doc, "og:bar", "og value bar"); + + const color = await page.$eval("h1", (el) => { + return window.getComputedStyle(el).color; + }); + assertEquals(color, "rgb(255, 0, 0)"); + + const textColor = await page.$eval("p", (el) => { + return window.getComputedStyle(el).color; + }); + assertEquals(textColor, "rgb(0, 128, 0)"); + }, + ); +}); diff --git a/tests/test_utils.ts b/tests/test_utils.ts index 796219fa..62f14b89 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -562,3 +562,27 @@ export function assertNoComments(doc: Document) { ); } } + +export function assertMetaContent( + doc: Document, + nameOrProperty: string, + expected: string, +) { + let el = doc.querySelector(`meta[name="${nameOrProperty}"]`) as + | HTMLMetaElement + | null; + + if (el === null) { + el = doc.querySelector(`meta[property="${nameOrProperty}"]`) as + | HTMLMetaElement + | null; + } + + if (el === null) { + console.log(prettyDom(doc)); + throw new Error( + `No -tag found with content "${expected}"`, + ); + } + assertEquals(el.content, expected); +}