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

Components' Script Element Re-Excuted Unexpectedly When Using View Transitions #12830

Closed
1 task
SamuNatsu opened this issue Dec 25, 2024 · 1 comment
Closed
1 task
Labels
needs triage Issue needs to be triaged

Comments

@SamuNatsu
Copy link

Astro Info

Astro                    v5.1.1
Node                     v20.18.1
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             astro-d2-integration
                         html-minify
                         astro-icon
                         @astrojs/mdx
                         pagefind
                         @astrojs/sitemap
                         @astrojs/tailwind

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

No response

Describe the Bug

I wrote some web components with custom elements, but it seems that the scripts for defining the custom elements executed multiple times:

image

What's the expected result?

The scripts should not be re-excuted.

Only occurs after SSG, and no error when in development mode.

In the StackBlitz example, you can preview the build output, in browser DevTools it turns out:

image

Do I use the web components in a wrong way? Or it is a bug?

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-hm8w1cn2?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 25, 2024
@martrapp
Copy link
Member

martrapp commented Dec 26, 2024

Hi @SamuNatsu 👋🏼, I'm sorry that you ran into this issue.

The cause of the issue is that Astro re-executes inline scripts if they are found on the new page and haven't been on the old page. Astro normally turns scripts into external module scripts. External module scripts are only be executed once. Since Astro 5, an optimization automatically turns small scripts into inline script even if they do not have an is:inline direcitve. Thus navigating between page2 and the other pages executes the registration code on all visits of page2. And <ClientRouter/> effectively turned your app into a single page application. Thus the redefine errors. For more details see #12804.

The simplest hot fix right now seems to be adding an import of "astro:transitions/client" at the beginning of your script.

<script>
  import "astro:transitions/client";
  class Test extends HTMLDivElement {
  ...

Closed as a duplicate to #12804

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

No branches or pull requests

2 participants