Skip to content

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

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

Open
1 task
Serenacula opened this issue Oct 4, 2023 · 5 comments
Open
1 task

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

Serenacula opened this issue Oct 4, 2023 · 5 comments
Labels
- P2-to-be-discussed Needs discussion

Comments

@Serenacula
Copy link

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.
@DevRohit06
Copy link

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

@bluwy
Copy link
Member

bluwy commented Oct 17, 2023

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 bluwy transferred this issue from withastro/astro Oct 17, 2023
@bluwy bluwy added the - P3: minor bug An edge case that only affects very specific usage (priority) label Oct 17, 2023
@lilnasy
Copy link
Contributor

lilnasy commented Oct 17, 2023

@bluwy do you plan on working on this?

@bluwy
Copy link
Member

bluwy commented Oct 18, 2023

Not yet. Feel free to take it!

@lilnasy lilnasy self-assigned this Oct 18, 2023
@lilnasy
Copy link
Contributor

lilnasy commented Oct 18, 2023

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

It seems like this behavior was introduced intentionally.

@lilnasy lilnasy added - P2-to-be-discussed Needs discussion and removed - P3: minor bug An edge case that only affects very specific usage (priority) labels Oct 18, 2023
@lilnasy lilnasy removed their assignment Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2-to-be-discussed Needs discussion
Projects
None yet
Development

No branches or pull requests

4 participants