compiler icon indicating copy to clipboard operation
compiler copied to clipboard

Expressions in `<head>` causes elements to end up in `<body>`

Open donnikitos opened this issue 1 year ago • 4 comments
trafficstars

Astro Info

Astro                    v4.5.5
Node                     v21.6.2
System                   Linux (x64)
Package Manager          bun
Output                   static
Adapter                  none
Integrations             @astrojs/tailwind
                         @astrojs/preact
                         astro-reroute

If this issue only occurs in one browser, which browser is a problem?

Chrome, Chromium, Firefox

Describe the Bug

Updated Astro from 4.0.8 to latest 4.5.4 and suddenly browsers receive files with broken utf-8 characters: image image

<meta charset="UTF-8" /> is set. Input files are encoded in proper UTF-8 charset.

For now my only solution for deployment is to enforce UTF-8 via .htaccess headers. Dev server still displays broken characters.

What's the expected result?

During development and after build non-ASCI character should be properly encoded. For example:

  • ö should be ö
  • ü should be ü
  • ...

Link to Minimal Reproducible Example

https://github.com/donnikitos/astro-utf-bug

Participation

  • [ ] I am willing to submit a pull request for this issue.

donnikitos avatar Mar 15 '24 21:03 donnikitos

Hello @donnikitos. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

github-actions[bot] avatar Mar 16 '24 00:03 github-actions[bot]

While creating the reproduction repo, I noticed, that the conditional title in the head causes the encoding to break: image

donnikitos avatar Mar 16 '24 09:03 donnikitos

It seems that some of the tags are misplaced to <body> when this kind of expression is in <head>:

{<title>{...}</title>}

mingjunlu avatar Mar 18 '24 08:03 mingjunlu

@donnikitos After playing around with the reproduction repo, I think I might have found a workaround: wrapping <title> with <Fragment>. Could you try if it works for you?

<Fragment>{meta?.title && <title>{meta.title}</title>}</Fragment>

mingjunlu avatar Mar 18 '24 08:03 mingjunlu

@MoustaphaDev should merge https://github.com/withastro/compiler/issues/982 he thinks is related to this.

cdtut avatar Mar 27 '24 21:03 cdtut

@Princesseuh Can priority level be urgent? Can't upgrade after 4.2.1 because everything break.

cdtut avatar Mar 28 '24 19:03 cdtut

This simple example doesn't reproduce: https://stackblitz.com/edit/github-pfaayw?file=src%2Fpages%2Findex.astro

Can anyone come up with a minimal repro at the level with it?

matthewp avatar Mar 28 '24 19:03 matthewp

I add this inside <head> to see issue from astro build.

{[1, 2, 3].map(() => <>
    {<meta name="" content="" />}
</>)}

cdtut avatar Mar 28 '24 20:03 cdtut

I updated this demo to use that: https://stackblitz.com/edit/github-pfaayw?file=src%2Fpages%2Findex.astro,package.json

Still in the head. Can you adjust my demo to get it to break?

matthewp avatar Mar 29 '24 12:03 matthewp

I was able to reproduce it down with this: https://stackblitz.com/edit/github-pfaayw-axdvrr?file=src%2Fpages%2Findex.astro,package.json

matthewp avatar Mar 29 '24 12:03 matthewp

Unfortunately that PR breaks a bunch of tests in core. There might be a way to fix this issue differently, though.

matthewp avatar Mar 29 '24 14:03 matthewp

Pretty sure this is the culprit: https://github.com/withastro/compiler/commit/14ccba5d4dc0c962af247eff7cb0d7c199302538

matthewp avatar Mar 29 '24 15:03 matthewp

I need to test against core, but hopefully this does it: https://github.com/withastro/compiler/pull/990

matthewp avatar Mar 29 '24 15:03 matthewp

If test pass will PR be in next astro release?

cdtut avatar Mar 29 '24 18:03 cdtut

I'll create a preview release and have a few people test it. But yes, it will be soon (probably next week) if everything looks ok.

matthewp avatar Mar 29 '24 18:03 matthewp

@cdtut you can test by install astro@experimental--head-body-content.

I tested with this demo, which is fixed now: https://stackblitz.com/edit/github-pfaayw-axdvrr?file=src%2Fpages%2Findex.astro,package.json

matthewp avatar Mar 29 '24 19:03 matthewp

@matthewp's fix works for my case :smile:. Thank you!

deeprobin avatar Mar 29 '24 21:03 deeprobin