From e7bf6aca6230394c2102df4d6d3c53c1efea5bef Mon Sep 17 00:00:00 2001
From: Igor Brasileiro
Date: Fri, 12 Jan 2024 14:47:40 -0300
Subject: [PATCH] fix: Prevent update doc title from partial if not present
(#2244)
fixes #2243
---
src/runtime/entrypoints/main.ts | 4 ++-
tests/fixture_partials/fresh.gen.ts | 2 ++
.../routes/head_merge/index.tsx | 9 ++++++
.../routes/head_merge/without_title.tsx | 24 ++++++++++++++
tests/partials_test.ts | 31 +++++++++++++++++++
5 files changed, 69 insertions(+), 1 deletion(-)
create mode 100644 tests/fixture_partials/routes/head_merge/without_title.tsx
diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts
index 96e2f089..de401bfb 100644
--- a/src/runtime/entrypoints/main.ts
+++ b/src/runtime/entrypoints/main.ts
@@ -710,7 +710,9 @@ export async function applyPartials(res: Response): Promise {
}
// Update
- document.title = doc.title;
+ if (doc.title) {
+ document.title = doc.title;
+ }
// Needs to be converted to an array otherwise somehow -tags
// are missing.
diff --git a/tests/fixture_partials/fresh.gen.ts b/tests/fixture_partials/fresh.gen.ts
index 86ef52ab..d97abc56 100644
--- a/tests/fixture_partials/fresh.gen.ts
+++ b/tests/fixture_partials/fresh.gen.ts
@@ -51,6 +51,7 @@ import * as $head_merge_duplicate from "./routes/head_merge/duplicate.tsx";
import * as $head_merge_index from "./routes/head_merge/index.tsx";
import * as $head_merge_injected from "./routes/head_merge/injected.tsx";
import * as $head_merge_update from "./routes/head_merge/update.tsx";
+import * as $head_merge_without_title from "./routes/head_merge/without_title.tsx";
import * as $index from "./routes/index.tsx";
import * as $isPartial_middleware from "./routes/isPartial/_middleware.ts";
import * as $isPartial_async from "./routes/isPartial/async.tsx";
@@ -185,6 +186,7 @@ const manifest = {
"./routes/head_merge/index.tsx": $head_merge_index,
"./routes/head_merge/injected.tsx": $head_merge_injected,
"./routes/head_merge/update.tsx": $head_merge_update,
+ "./routes/head_merge/without_title.tsx": $head_merge_without_title,
"./routes/index.tsx": $index,
"./routes/isPartial/_middleware.ts": $isPartial_middleware,
"./routes/isPartial/async.tsx": $isPartial_async,
diff --git a/tests/fixture_partials/routes/head_merge/index.tsx b/tests/fixture_partials/routes/head_merge/index.tsx
index 9261a37d..cc59d3b8 100644
--- a/tests/fixture_partials/routes/head_merge/index.tsx
+++ b/tests/fixture_partials/routes/head_merge/index.tsx
@@ -33,6 +33,15 @@ export default function SlotDemo() {
duplicate
+
+
+ without title
+
+
);
}
diff --git a/tests/fixture_partials/routes/head_merge/without_title.tsx b/tests/fixture_partials/routes/head_merge/without_title.tsx
new file mode 100644
index 00000000..15d0d559
--- /dev/null
+++ b/tests/fixture_partials/routes/head_merge/without_title.tsx
@@ -0,0 +1,24 @@
+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 (
+ <>
+
+
+
+
+
+ page without title
+
+
+ >
+ );
+});
diff --git a/tests/partials_test.ts b/tests/partials_test.ts
index 67f058f4..8d6f03f0 100644
--- a/tests/partials_test.ts
+++ b/tests/partials_test.ts
@@ -1538,3 +1538,34 @@ Deno.test("render 404 partial", async () => {
},
);
});
+
+Deno.test("render partial with title", 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(".duplicate-link");
+ await page.waitForSelector(".status-duplicated");
+
+ const doc = parseHtml(await page.content());
+ assertEquals(doc.title, "Head merge duplicated");
+ },
+ );
+});
+
+Deno.test("render partial without title", async () => {
+ await withPageName(
+ "./tests/fixture_partials/main.ts",
+ async (page, address) => {
+ await page.goto(`${address}/head_merge`);
+ await page.click(".without-title");
+
+ await page.waitForSelector(".page-without-title");
+
+ const doc = parseHtml(await page.content());
+ assertEquals(doc.title, "Head merge");
+ },
+ );
+});