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

🐛 BUG: Page blank when <Head> is outsourced to component (Astro 0.21.4) #2022

Closed
stephanbogner opened this issue Nov 25, 2021 · 9 comments
Milestone

Comments

@stephanbogner
Copy link
Contributor

stephanbogner commented Nov 25, 2021

What version of astro are you using?

0.21.4

What package manager are you using?

npm

What operating system are you using?

Mac (Catalina 10.15.7)

Describe the Bug

Woah ... that took me some hours to reproduce because the behaviour is so strange 😅

Description

Page doesn't render (completely white, but no errors), when <head>...</head> is outsourced to another component (which worked perfectly fine before 0.21.X).
However, this doesn't always happen – see "Various cases" for more details.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-5aervp?file=src/pages/index.astro

Various cases for index.astro:

I only provided the failing case 2 in StackBlitz, but there is more to it:

✅ Case 1 – Works (no <MetaHead/> or <head></head>)

---
import MetaHead from '../components/MetaHead.astro';
import Tour from '../components/Tour.astro';
---
<html lang="en">
    <body>
        Test
    </body>
</html>

❌ Case 2 – Doesn't work (<MetaHead/> added)

---
import MetaHead from '../components/MetaHead.astro';
import Tour from '../components/Tour.astro';
---
<html lang="en">
    <MetaHead/>
    <body>
        Test
    </body>
</html>

✅ Case 3 – Works 😮 (Test replaced with <Tour/>)

---
import MetaHead from '../components/MetaHead.astro';
import Tour from '../components/Tour.astro';
---
<html lang="en">
    <MetaHead/>
    <body>
        <Tour/>
    </body>
</html>

✅ Case 4 – Works again (Empty <head></head> added):

---
import MetaHead from '../components/MetaHead.astro';
import Tour from '../components/Tour.astro';
---
<html lang="en">
    <head></head>
    <MetaHead/>
    <body>
        Test
    </body>
</html>

✅ Case 5 – Also works (readded <Tour/>)

---
import MetaHead from '../components/MetaHead.astro';
import Tour from '../components/Tour.astro';
---
<html lang="en">
    <head></head>
    <MetaHead/>
    <body>
        Test
        <Tour/>
    </body>
</html>

MetaHead.astro

This file is also in StackBlitz, but for completeness, I added it here too:

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>Head Component Test</title>
    <link rel="icon" type="image/x-icon" href="/favicon.ico" />
    <link rel="stylesheet" href={Astro.resolve('../styles/global.scss')}>
    <link rel="stylesheet" href={Astro.resolve('../styles/home.scss')}>

    <style>
        header {
            display: flex;
            flex-direction: column;
            gap: 1em;
            max-width: min(100%, 68ch);
        }
    </style>
</head>
@Chrissdroid
Copy link
Contributor

Can confirm, it's happening also for me. (even regressing to previous astro version 0.21.x breaks also although it worked back then)

@peterlerche
Copy link

I have the same experience

@drwpow
Copy link
Member

drwpow commented Nov 29, 2021

We’ve been noticing some weirdness when <head> or <body> are included in components. @natemoo-re is adding fixes in the compiler to prevent errors. I think we are still deciding on how best to handle this, but current workaround is to limit <head> and <body> tags to pages, and try to make a habit to always include them.

This may even become best practice, because if <head> can be specified within components, we’d likely need some sort of API or behavior to merge & combine those tags, which we don’t have currently.

@stephanbogner
Copy link
Contributor Author

@drwpow My 5cts:

Limitting <head> and <body> to pages should be fine (if mentioned in the documentation) 👍 .

Solutions for the "<head> inside components"-problem (e.g. for React with React-Helmet) feel like a hack anyways, when you are used to writing "normal" HTML-files, where you have the <head> once.

FYI: So far, I personally structured my projects like this:
→ Basically outsourcing the whole head into its own component (just to keep everything neat and tidy and reusable across projects).

<html lang="en">
<MetaHead title="Interface Index | Collection of interface elements" ogTitle="Interface Index"/>
<body>
    <main>
    ...
    </main>
</body>
</html>

But I don't see a problem doing this as well:

<html lang="en">
<head>
    <MetaHeadContent title="Interface Index | Collection of interface elements" ogTitle="Interface Index"/>
</head>
<body>
    <main>
    ...
    </main>
</body>
</html>

@drwpow
Copy link
Member

drwpow commented Nov 30, 2021

Yea that all sounds reasonable!

Limitting and to pages should be fine (if mentioned in the documentation) 👍 .

Yes this needs to he explicit, you’re right! But before we commit to this, let me check with the other core maintainers to get their opinions. Because what this ticket is about is lack of clarity on whether a limitation is intentional or not. And we’ll try our best to make it intentional (in service of easier-to-manage code), and clear (docs, warnings, etc).

@jonathantneal
Copy link
Contributor

Confirmed this is still happening in Astro v0.21.12.

https://stackblitz.com/edit/github-5aervp-kt2jvq?file=astro.config.mjs

@tony-sull
Copy link
Contributor

An RFC was accepted not too long ago to finalize <head> and <body> behavior 👍

@natemoo-re
Copy link
Member

This is on track to be fixed in withastro/compiler#267 and the subsequent 0.24.x release of astro.

@natemoo-re natemoo-re added this to the 0.24 milestone Mar 9, 2022
@natemoo-re
Copy link
Member

This should be fixed in the newly released [email protected]! See working reproduction.

Please give feel free to reopen this issue if you're still having trouble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants