Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/content/docs/en/guides/view-transitions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: View transitions
description: Enable seamless navigation between pages in Astro with view transitions.
i18nReady: true
---
import ReadMore from '~/components/ReadMore.astro'
import Since from '~/components/Since.astro'
import { Steps } from '@astrojs/starlight/components'

Expand Down Expand Up @@ -319,7 +320,7 @@ Links with the `data-astro-reload` attribute will be ignored by the router and a

### Trigger navigation

You can also trigger client-side navigation via events not normally listened to by the `<ClientRouter />` router using `navigate`. This function from the `astro:transitions/client` module can be used in scripts, and in framework components that are hydrated with a [client directive](/en/reference/directives-reference/#client-directives).
You can also trigger client-side navigation via events not normally listened to by the `<ClientRouter />` router using [`navigate()`](/en/reference/modules/astro-transitions/#navigate). This function from the `astro:transitions/client` module can be used in scripts, and in framework components that are hydrated with a [client directive](/en/reference/directives-reference/#client-directives).

The following example shows an Astro component that navigates a visitor to another page they select from a menu:

Expand Down Expand Up @@ -446,6 +447,32 @@ You can opt out of router transitions on any individual form using the `data-ast
<form action="/contact" data-astro-reload>
```

### Navigating with user input

The `navigate()` API does not perform sanitization on the URLs passed to it. If you are using user input to determine the URL to navigate to, you should validate the input before passing it to `navigate()`.

For example, a `?redirect` query parameter could be used to navigate away from your site (`?redirect=http://example.com`) or to execute arbitrary code (`?redirect=javascript:alert('Evil code')`) if the value is not sanitized before use.

One way to implement this safely is to ensure only a set of known paths can be redirected to:

```astro title="src/pages/index.astro"
<script>
import { navigate } from 'astro:transitions/client';

const params = new URLSearchParams(window.location.search);
const redirect = params.get('redirect');
const allowedPaths = ['/home', '/about', '/contact'];

if (allowedPaths.includes(redirect)) {
navigate(redirect);
}
</script>
```

The exact kind of sanitization you need will depend on your site and what you want to allow.

<ReadMore>Consider enabling Astro’s [experimental Content Security Policy feature](/en/reference/experimental-flags/csp/) to help protect against cross-site scripting (XSS) risks if using user input with the `navigate()` API.</ReadMore>

## Fallback control

The `<ClientRouter />` router works best in browsers that support View Transitions (i.e. Chromium browsers), but also includes default fallback support for other browsers. Even if the browser does not support the View Transitions API, Astro's client router can still provide in-browser navigation using one of the fallback options.
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/en/reference/modules/astro-transitions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ A function that executes a navigation to the given `href` using the View Transit

This function signature is based on the [`navigate` function from the browser Navigation API](https://developer.mozilla.org/en-US/docs/Web/API/Navigation/navigate). Although based on the Navigation API, this function is implemented on top of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to allow for navigation without reloading the page.

`navigate()` does not perform sanitization on the `href` parameter. [Sanitize user input](/en/guides/view-transitions/#navigating-with-user-input) if you use it to determine the URL to navigate to.

#### `history` option

<p>
Expand Down