Skip to content
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
5 changes: 4 additions & 1 deletion src/ssr/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ export const createHeadManager = (): HeadManager => {
let html = '';
if (state.title !== null) {
const formatted = state.titleTemplate
? state.titleTemplate.replace(/%s/g, state.title)
? // Use a replacer function so the title is inserted literally — a bare
// replacement string would interpret `$&`/`$1`/`` $` ``/`$'` in the
// title as special replacement patterns and mangle the output.
state.titleTemplate.replace(/%s/g, () => state.title as string)
: state.title;
html += `<title>${escapeText(formatted)}</title>`;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ssr-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,15 @@ describe('head manager', () => {
head.add({ title: 'Home', titleTemplate: '%s | Acme' });
expect(head.render()).toContain('<title>Home | Acme</title>');
});

it('inserts a title with special replacement patterns literally (#177)', () => {
const head = createHeadManager();
head.add({ title: 'Q&A $& more', titleTemplate: '%s | Acme' });
// `$&` in the title must not be interpreted as a replacement pattern.
// escapeText encodes the ampersands, so assert the literal `$&` survives.
expect(head.render()).toContain('$&amp; more');
expect(head.render()).toContain('| Acme');
});
});

describe('asset manager', () => {
Expand Down