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

Style and scripts moved into body tag in certain cases #316

Closed
1 task
ChrisSki opened this issue Jan 20, 2023 · 19 comments
Closed
1 task

Style and scripts moved into body tag in certain cases #316

ChrisSki opened this issue Jan 20, 2023 · 19 comments
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) ecosystem: compiler Issue is caused by a bug in the Astro compiler

Comments

@ChrisSki
Copy link

What version of astro are you using?

1.9.2

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

npm

What operating system are you using?

Mac

Describe the Bug

Per the docs for importing CSS:

Tip: A common pattern in Astro is to import global CSS inside a Layout component. Be sure to import the Layout component before other imports so that it has the lowest precedence.

I tried using a <style lang="scss"> outside of <html> like so:

src/layouts/BlogPostLayout.astro

Screenshot 2023-01-20 at 11 41 33 AM

But was left with this:

Screenshot 2023-01-20 at 11 41 51 AM

@aFuzzyBear suggested I log this in hope that the Astro Prettier plugin could allow for this and not confuse anyone else 🙂

Link to Minimal Reproducible Example

na

Participation

  • I am willing to submit a pull request for this issue.
@ChrisSki ChrisSki changed the title Astro Prettier plugin needs heavier hands in .astro files Astro Prettier plugin needs heavier hands in .astro layout files Jan 20, 2023
@Princesseuh Princesseuh transferred this issue from withastro/astro Jan 20, 2023
@Princesseuh
Copy link
Member

Hello, thank you for reporting this issue! I'm pretty sure we had this fixed already in the past, I'll do a proper investigation on Monday, but it is possible that you're using an outdated version of the plugin?

@ChrisSki
Copy link
Author

I'm on 0.7.2 which I think is the latest?

@Princesseuh
Copy link
Member

Hello! After further investigation, this is caused by an issue in the compiler. The previous fix fixed this in most situations, but it seems like there's still an issue when the content of head is a component. I'm sorry for the inconvenience!

@ChrisSki
Copy link
Author

All good! I just wanted to bring it up since the Astro docs suggest importing global css into a layout, and done like this it's just a formatting issue. The workaround was simply to build a GlobalStyles component and import that into the Head in my layout, which the docs also suggest. So nothing really wrong here :)

@HiDeoo
Copy link
Member

HiDeoo commented Mar 31, 2023

there's still an issue when the content of head is a component

Another workaround is using this syntax:

<head>
  {/* https://github.com/withastro/prettier-plugin-astro/issues/316#issuecomment-1400808708 */}
  {() => <Head />}
</head>

@mks-h
Copy link

mks-h commented Jun 29, 2023

Formatting the file twice puts styles inside the body element. Trying to prettier-ignore the styles triggers #338, which eats the styles (except for the lang=scss attribute, weirdly enough).

@Princesseuh
Copy link
Member

Both issues are unfortunately still blocked upstream. You can keep an eye out on this issue for the style tag moving: withastro/compiler#712

@shardbearer-shri
Copy link

Still facing this issue and i was wondering why my styles intellisense was not working

@Princesseuh Princesseuh removed the bug label Nov 28, 2023
@Princesseuh Princesseuh changed the title Astro Prettier plugin needs heavier hands in .astro layout files Style and scripts moved into body tag in certain cases Nov 29, 2023
@Princesseuh Princesseuh added the - P3: minor bug An edge case that only affects very specific usage (priority) label Nov 29, 2023
@jmegs
Copy link

jmegs commented Jan 19, 2024

just ran across this same issue in 2.6.2 of the extension, just to provide more evidence it's still happening. I did notice it after putting <ViewTransitions /> into the <head> section of this layout, if that helps.

CleanShot 2024-01-19 at 15 29 05

@firxworx
Copy link

I'm running latest everything and can confirm this issue remains: astro-prettier-plugin v5.1.3 + astro 4.2.4.

I note the comment above that things are currently blocked upstream per withastro/compiler#712.

@jmegs note that this happens with any component in the head not just <ViewTransition> (in case that helps you).

I can also confirm that this plugin appears to ignore prettier-ignore directives as documented in https://prettier.io/docs/en/ignore.html.

No combinations of <!-- prettier-ignore --> or <!-- prettier-ignore-start --> + <!-- prettier-ignore-end --> that I tried could get prettier to leave this alone.

I also tried moving my <style>...</style> to its own Astro component and the issue still appears, so whatever underlying pattern/regex is not specifically limited to <style> and <script>` tags.

I'm surprised this isn't tripping people up all over the place!!

@raae
Copy link

raae commented Mar 4, 2024

I also see this when adding to the head.

Astro extension v2.7.5

@cprcrack
Copy link

cprcrack commented Mar 7, 2024

Can I suggest this bug to be considered as higher priority? P3 / minor bug / edge case doesn't seem to make it justice.

@Princesseuh
Copy link
Member

There's already a PR in flight to fix this issue, bumping the priority at this point wouldn't change anything.

@MiguelJAS
Copy link

Can we bump the priority for this? Thanks!

@Princesseuh
Copy link
Member

Can we bump the priority for this? Thanks!

Please read the comment right above yours.

@rbsummers
Copy link

rbsummers commented May 16, 2024

Hi, I think I may have found a workaround for the time being, at least with that JSX block with just the comment now both vscode plugin stopped complaining and the cli linter passes without warnings:

<html>
  <head>
    // ...head, title, & other stuff...
  </head>

  <body>
    // ...layout code...
    {
      /*
       * For some reason,without this comment here prettier forces incorrect formatting.
       * See https://github.com/withastro/prettier-plugin-astro/issues/316
       */
    }
  </body>
</html>

<style lang="scss">
  // ...styles...
</style>

Can anyone double check?

@MoltenCoffee
Copy link

The workaround by @rbsummers seems to work for me as well!

@thedanbaird
Copy link

The workaround from @rbsummers works for me too - would be great to bump the priority of this up though.

@Princesseuh
Copy link
Member

This was fixed in the last version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) ecosystem: compiler Issue is caused by a bug in the Astro compiler
Projects
None yet
Development

No branches or pull requests