Skip to content

SSR should only render one <title> #4250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Expose compiler version in dev events ([#4047](https://github.com/sveltejs/svelte/issues/4047))
* Fix reactive assignments with destructuring and stores where the destructured value should be undefined ([#4170](https://github.com/sveltejs/svelte/issues/4170))
* Do not automatically declare variables in reactive declarations when assigning to a member expression ([#4212](https://github.com/sveltejs/svelte/issues/4212))
* Only render one `<title>` in SSR mode when multiple components provide one ([#4250](https://github.com/sveltejs/svelte/pull/4250))

## 3.16.7

Expand Down
6 changes: 6 additions & 0 deletions src/compiler/compile/render_ssr/handlers/Title.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import Renderer, { RenderOptions } from '../Renderer';
import Title from '../../nodes/Title';
import { x } from 'code-red';

export default function(node: Title, renderer: Renderer, options: RenderOptions) {
renderer.push();

renderer.add_string(`<title>`);

renderer.render(node.children, options);

renderer.add_string(`</title>`);
const result = renderer.pop();

renderer.add_expression(x`($$result.title = ${result}, "")`);
}
5 changes: 3 additions & 2 deletions src/runtime/internal/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ export function create_ssr_component(fn) {
on_destroy = [];

const result: {
title: string;
head: string;
css: Set<{
map: null;
code: string;
}>;
} = { head: '', css: new Set() };
} = { title: '', head: '', css: new Set() };

const html = $$render(result, props, {}, options);

Expand All @@ -120,7 +121,7 @@ export function create_ssr_component(fn) {
code: Array.from(result.css).map(css => css.code).join('\n'),
map: null // TODO
},
head: result.head
head: result.title + result.head
};
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svelte:head>
<title>A</title>
</svelte:head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svelte:head>
<title>B</title>
</svelte:head>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<title>B</title>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"adjective": "custom"
}
10 changes: 10 additions & 0 deletions test/server-side-rendering/samples/head-multiple-title/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import A from './A.svelte';
import B from './B.svelte';
</script>

<svelte:head>
<title>Main</title>
</svelte:head>
<A />
<B />