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

Document caveats with using ViewTransitions component #3810

Merged
merged 9 commits into from
Jul 25, 2023
31 changes: 31 additions & 0 deletions src/content/docs/en/guides/view-transitions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,34 @@ import { ViewTransitions } from 'astro:transitions';
:::note[known limitations]
The `morph` animation cannot be simulated in traditional CSS. So any element using this animation will not be animated.
:::

## Script behavior during page navigation

When navigating between pages with the `<ViewTransitions />` component, scripts are run in sequential order to match browser behavior.

If you have code that sets up global state, this state will need to take into account that the script might execute more than once. Check for the global state in your `<script>` tag, and conditionally execute your code where possible:

```astro
<script is:inline>
if(!window.SomeGlobal) {
window.SomeGlobal = {} // ....
}
</script>
```

Module scripts are only ever executed once because the browser keeps track of which modules are already loaded. For these scripts, you do not need to worry about re-execution.

#### astro:load

An event that fires on page navigation. You can listen to this event on the `document`. The `<ViewTransitions />` component fires this event both on initial page navigation (MPA) and any subsequent navigation, either forwards or backwards.

You can use this event to run code on every page navigation, or only once ever:

```astro
<script>
document.addEventListener('astro:load', () => {
// This only runs once.
setupStuff();
}, { once: true });
</script>
```