#client script not running
4 messages · Page 1 of 1 (latest)
Hi @sweet fiber. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
There could be several reasons why a client loading script is not running in Astro. Here are some potential issues and their solutions based on the information provided:
-
Accessing
documentorwindowon the server: Astro components run on the server, so you can’t access these browser-specific objects within the frontmatter. If you aren't usingdocumentorwindowdirectly and still getting this error, check to see if any packages you’re importing are meant to run on the client. If the code is in an Astro component, move it to a<script>tag outside of the frontmatter. This tells Astro to run this code on the client, wheredocumentandwindoware available. If the code is in a framework component, try to access these objects after rendering using lifecycle methods. You can also prevent the component from rendering on the server at all by adding theclient:onlydirective. source (https://docs.astro.build/en/guides/troubleshooting/) -
Import statement outside a module: In Astro components,
<script>tags are hoisted and loaded as JS modules by default. If you have included theis:inlinedirective or any other attribute in your tag, this default behavior is removed. If you have added any attributes to your<script>tag, you must also add thetype="module"attribute to be able to use import statements. source (https://docs.astro.build/en/guides/troubleshooting/)
-
Component is not interactive: If your component is rendering but is not responding to user interaction, then you may be missing a
client:*directive to hydrate your component. By default, a UI Framework component is not hydrated in the client. If noclient:*directive is provided, its HTML is rendered onto the page without JavaScript. You can use a<script>tag in your Astro component template to send JavaScript to the browser that executes in the global scope. source (https://docs.astro.build/en/guides/troubleshooting/) -
Content Security Policy (CSP) violation: If your site’s CSP disallows running inline
<script>tags, which Astro outputs by default, you may see an error logged in the browser console. Update your CSP to includescript-src: 'unsafe-inline'to allow inline scripts to run. source (https://docs.astro.build/en/guides/troubleshooting/)