#ViewTransisions with external scripts src
3 messages · Page 1 of 1 (latest)
Hi @dusky basalt. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Based on the information from the provided extracts, it seems that scripts are not automatically re-executed during view transitions in Astro. This means that if you navigate to a new page, the script won't reinitialize.
However, there are a few potential solutions to this issue:
- You can add an event listener for the
astro:after-swapevent, which is called after every transition. This can be used to reinitialize the script once the new page is loaded. Here's an example of how you might do this:
<script>
function loadScript() {
var script = document.createElement('script');
script.src = '<https://cdn.snipcart.com/themes/v3.3.3/default/snipcart.js';>
script.async = true;
document.head.appendChild(script);
}
document.addEventListener('astro:after-swap', loadScript);
loadScript();
</script>
- Another approach discussed in the extracts is to use the
transition:persistattribute on a component that includes the script. This attribute keeps the component active during navigation events, which might help with scripts that need to maintain their state across page transitions.
Remember to test these solutions thoroughly, as the behavior can depend on the specifics of the third-party script you're using.
Sources:
- Astro Documentation: View Transitions (https://docs.astro.build/en/guides/view-transitions#adding-view-transitions-to-a-page)
- Discord Support Thread: View transition and 3rd party scripts (https://discord.com/channels/830184174198718474/1141283784738750464)