prettier-plugin-astro icon indicating copy to clipboard operation
prettier-plugin-astro copied to clipboard

Style and scripts moved into body tag in certain cases

Open ChrisSki opened this issue 2 years ago • 17 comments

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 avatar Jan 20 '23 16:01 ChrisSki

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?

Princesseuh avatar Jan 20 '23 23:01 Princesseuh

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

ChrisSki avatar Jan 21 '23 01:01 ChrisSki

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!

Princesseuh avatar Jan 23 '23 18:01 Princesseuh

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 :)

ChrisSki avatar Jan 23 '23 18:01 ChrisSki

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>

HiDeoo avatar Mar 31 '23 04:03 HiDeoo

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).

mks-h avatar Jun 29 '23 12:06 mks-h

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

Princesseuh avatar Jun 29 '23 13:06 Princesseuh

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

shardbearer-shri avatar Jul 02 '23 05:07 shardbearer-shri

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

jmegs avatar Jan 19 '24 20:01 jmegs

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 https://github.com/withastro/compiler/issues/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

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

firxworx avatar Jan 24 '24 19:01 firxworx

I also see this when adding <ViewTransitions /> to the head.

Astro extension v2.7.5

raae avatar Mar 04 '24 21:03 raae

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

cprcrack avatar Mar 07 '24 19:03 cprcrack

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

Princesseuh avatar Mar 07 '24 19:03 Princesseuh

Can we bump the priority for this? Thanks!

MiguelJAS avatar Apr 18 '24 20:04 MiguelJAS

Can we bump the priority for this? Thanks!

Please read the comment right above yours.

Princesseuh avatar Apr 19 '24 04:04 Princesseuh

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?

rbsummers avatar May 16 '24 09:05 rbsummers

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

MoltenCoffee avatar Jun 02 '24 19:06 MoltenCoffee

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

thedanbaird avatar Jul 12 '24 02:07 thedanbaird

This was fixed in the last version!

Princesseuh avatar Jul 18 '24 21:07 Princesseuh