compiler icon indicating copy to clipboard operation
compiler copied to clipboard

Scripts fail to run when they are the only tag on page

Open Serenacula opened this issue 2 years ago • 5 comments

Astro Info

Astro                    v3.2.0
Node                     v18.18.0
System                   macOS (arm64)
Package Manager          yarn
Output                   static
Adapter                  none
Integrations             @astrojs/prefetch

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

No response

Describe the Bug

Given the following code:

<script>
    console.log("hi!")
</script>

nothing happens. If you add a div:

<script>
    console.log("hi!")
</script>
<div />

then it suddenly runs in console.

What's the expected result?

The script should run even without other tags present.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-kwyexm?file=src%2Fpages%2Findex.astro,src%2Fpages%2FwithoutDiv.astro,src%2Fpages%2FwithDiv.astro

Participation

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

Serenacula avatar Oct 04 '23 20:10 Serenacula

The script tag runs without any elements. But we have to use is:inline in script tag

<script is:inline> console.log("hi!") </script>

this works and got "hi!" in console

DevRohit06 avatar Oct 17 '23 09:10 DevRohit06

Looks like in the compiler we need to emit ${$$maybeRenderHead($$result)} so the render would attach the script here. The additional <div/> in the repo triggered adding ${$$maybeRenderHead($$result)} which made it work.

bluwy avatar Oct 17 '23 13:10 bluwy

@bluwy do you plan on working on this?

lilnasy avatar Oct 17 '23 20:10 lilnasy

Not yet. Feel free to take it!

bluwy avatar Oct 18 '23 04:10 bluwy

One of the tests I broke pointed me to this PR.

  • https://github.com/withastro/compiler/pull/710

It seems like this behavior was introduced intentionally.

lilnasy avatar Oct 18 '23 13:10 lilnasy