Skip to content
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

fix: only create document.title effect if value is dynamic #12698

Merged
merged 7 commits into from
Aug 2, 2024
Merged
Changes from 1 commit
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
Next Next commit
fix: dont create an effect for static title
  • Loading branch information
Rich-Harris committed Aug 2, 2024
commit b0002021b84fb4f5d14cb0ee8b38f05ded92b83f
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 8,21 @@ import { build_template_literal } from './shared/utils.js';
* @param {ComponentContext} context
*/
export function TitleElement(node, context) {
// TODO throw validation error when attributes present / when children something else than text/expression tags
// TODO only create update when expression is dynamic
let has_state = node.fragment.nodes.some(
(node) => node.type === 'ExpressionTag' && node.metadata.expression.has_state
);

if (node.fragment.nodes.length === 1 && node.fragment.nodes[0].type === 'Text') {
context.state.init.push(
b.stmt(
b.assignment(
'=',
b.member(b.id('$.document'), b.id('title')),
b.literal(/** @type {Text} */ (node.fragment.nodes[0]).data)
)
)
);
const value = build_template_literal(
/** @type {any} */ (node.fragment.nodes),
context.visit,
context.state
)[1];

const statement = b.stmt(b.assignment('=', b.member(b.id('$.document'), b.id('title')), value));

if (has_state) {
context.state.update.push(statement);
} else {
context.state.update.push(
b.stmt(
b.assignment(
'=',
b.member(b.id('$.document'), b.id('title')),
build_template_literal(
/** @type {any} */ (node.fragment.nodes),
context.visit,
context.state
)[1]
)
)
);
context.state.init.push(statement);
}
}