Skip to content

Commit

Permalink
fix(meta): Remove duplicated title and meta attributes (possible edge…
Browse files Browse the repository at this point in the history
… cases) (#9113)

When rendering meta tags on Cell success, we now check if the new tags
being rendered are already in the `<head>` and remove them.

We do this to make sure the new title etc are displayed by the browser.
Having multiple `<title>` tags gave inconsistent results
  • Loading branch information
dac09 committed Sep 1, 2023
1 parent afda25d commit 8d0ab16
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/vite/src/streaming/streamHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,21 @@ function createServerInjectionStream({
outputStream.write(elementsAtTheEnd)

// This will find all the elements added by PortalHead during a server render, and move them into <head>
outputStream.write(
"<script>document.querySelectorAll('body [data-rwjs-head]').forEach((el)=>{el.removeAttribute('data-rwjs-head');document.head.appendChild(el);});</script>"
)
// @TODO remove the whitespace to save them bytes later
outputStream.write(`<script>document.querySelectorAll('body [data-rwjs-head]').forEach((el) => {
document.querySelectorAll('head ' + el.tagName).forEach((e) => {
if (
el.tagName === 'TITLE' ||
(el.tagName === 'META' &&
el.getAttribute('name') === e.getAttribute('name') &&
el.getAttribute('property') === e.getAttribute('property'))
) {
e.remove();
}
document.head.appendChild(el);
});
});
</script>`)

outputStream.end()
},
Expand Down

0 comments on commit 8d0ab16

Please sign in to comment.