Skip to content

Commit b000202

Browse files
committed
fix: dont create an effect for static title
1 parent 81e0b5e commit b000202

File tree

1 file changed

+14
-25
lines changed
  • packages/svelte/src/compiler/phases/3-transform/client/visitors

1 file changed

+14
-25
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/TitleElement.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,21 @@ import { build_template_literal } from './shared/utils.js';
88
* @param {ComponentContext} context
99
*/
1010
export function TitleElement(node, context) {
11-
// TODO throw validation error when attributes present / when children something else than text/expression tags
12-
// TODO only create update when expression is dynamic
11+
let has_state = node.fragment.nodes.some(
12+
(node) => node.type === 'ExpressionTag' && node.metadata.expression.has_state
13+
);
1314

14-
if (node.fragment.nodes.length === 1 && node.fragment.nodes[0].type === 'Text') {
15-
context.state.init.push(
16-
b.stmt(
17-
b.assignment(
18-
'=',
19-
b.member(b.id('$.document'), b.id('title')),
20-
b.literal(/** @type {Text} */ (node.fragment.nodes[0]).data)
21-
)
22-
)
23-
);
15+
const value = build_template_literal(
16+
/** @type {any} */ (node.fragment.nodes),
17+
context.visit,
18+
context.state
19+
)[1];
20+
21+
const statement = b.stmt(b.assignment('=', b.member(b.id('$.document'), b.id('title')), value));
22+
23+
if (has_state) {
24+
context.state.update.push(statement);
2425
} else {
25-
context.state.update.push(
26-
b.stmt(
27-
b.assignment(
28-
'=',
29-
b.member(b.id('$.document'), b.id('title')),
30-
build_template_literal(
31-
/** @type {any} */ (node.fragment.nodes),
32-
context.visit,
33-
context.state
34-
)[1]
35-
)
36-
)
37-
);
26+
context.state.init.push(statement);
3827
}
3928
}

0 commit comments

Comments
 (0)