2022-10-06 09:15:30 +00:00
---
title: "navigateTo"
description: navigateTo is a helper function that programmatically navigates users.
2023-10-18 10:59:43 +00:00
links:
- label: Source
icon: i-simple-icons-github
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/router.ts
size: xs
2022-10-06 09:15:30 +00:00
---
2023-10-18 10:59:43 +00:00
## Usage
2024-01-31 09:46:21 +00:00
`navigateTo` is available on both server side and client side. It can be used within the [Nuxt context ](/docs/guide/going-further/nuxt-app#the-nuxt-context ), or directly, to perform page navigation.
2025-02-08 09:08:38 +00:00
::warning
Make sure to always use `await` or `return` on result of `navigateTo` when calling it.
::
::note
`navigateTo` cannot be used within Nitro routes. To perform a server-side redirect in Nitro routes, use [`sendRedirect` ](https://h3.unjs.io/utils/response#sendredirectevent-location-code ) instead.
2024-07-11 08:07:30 +00:00
::
2023-10-18 10:59:43 +00:00
### Within a Vue Component
```vue
< script setup lang = "ts" >
// passing 'to' as a string
await navigateTo('/search')
// ... or as a route object
await navigateTo({ path: '/search' })
// ... or as a route object with query parameters
await navigateTo({
path: '/search',
query: {
page: 1,
sort: 'asc'
}
})
< / script >
```
### Within Route Middleware
```ts
export default defineNuxtRouteMiddleware((to, from) => {
if (to.path !== '/search') {
// setting the redirect code to '301 Moved Permanently'
return navigateTo('/search', { redirectCode: 301 })
}
})
```
2025-02-08 09:08:38 +00:00
When using `navigateTo` within route middleware, you must **return its result** to ensure the middleware execution flow works correctly.
For example, the following implementation **will not work as expected** :
```ts
export default defineNuxtRouteMiddleware((to, from) => {
if (to.path !== '/search') {
// ❌ This will not work as expected
navigateTo('/search', { redirectCode: 301 })
return
}
})
```
In this case, `navigateTo` will be executed but not returned, which may lead to unexpected behavior.
2023-10-18 10:59:43 +00:00
:read-more{to="/docs/guide/directory-structure/middleware"}
2025-02-08 09:08:38 +00:00
### Navigating to an External URL
2023-10-18 10:59:43 +00:00
2024-06-07 08:05:08 +00:00
The `external` parameter in `navigateTo` influences how navigating to URLs is handled:
- **Without `external: true` **:
- Internal URLs navigate as expected.
- External URLs throw an error.
- **With `external: true` **:
- Internal URLs navigate with a full-page reload.
- External URLs navigate as expected.
#### Example
2023-10-18 10:59:43 +00:00
```vue
< script setup lang = "ts" >
// will throw an error;
// navigating to an external URL is not allowed by default
await navigateTo('https://nuxt.com')
2022-04-06 05:56:08 +00:00
2023-10-18 10:59:43 +00:00
// will redirect successfully with the 'external' parameter set to 'true'
await navigateTo('https://nuxt.com', {
external: true
})
< / script >
```
2022-07-07 18:48:10 +00:00
2025-02-08 09:08:38 +00:00
### Opening a Page in a New Tab
2023-10-18 10:59:43 +00:00
```vue
< script setup lang = "ts" >
// will open 'https://nuxt.com' in a new tab
2024-02-21 17:09:45 +00:00
await navigateTo('https://nuxt.com', {
2023-10-18 10:59:43 +00:00
open: {
target: '_blank',
windowFeatures: {
width: 500,
height: 500
}
}
})
< / script >
```
2022-07-07 18:48:10 +00:00
2022-09-03 12:30:41 +00:00
## Type
2022-07-07 18:48:10 +00:00
```ts
2025-02-08 09:08:38 +00:00
function navigateTo(
to: RouteLocationRaw | undefined | null,
options?: NavigateToOptions
) => Promise< void | NavigationFailure | false > | false | void | RouteLocationRaw
2022-07-07 18:48:10 +00:00
2022-09-03 12:30:41 +00:00
interface NavigateToOptions {
replace?: boolean
redirectCode?: number
external?: boolean
2023-06-07 19:27:00 +00:00
open?: OpenOptions
2022-09-03 12:30:41 +00:00
}
2022-07-07 18:48:10 +00:00
2025-02-08 09:08:38 +00:00
type OpenOptions = {
target: string
windowFeatures?: OpenWindowFeatures
}
type OpenWindowFeatures = {
popup?: boolean
noopener?: boolean
noreferrer?: boolean
} & XOR< { width?: number }, { innerWidth?: number }>
& XOR< { height?: number }, { innerHeight?: number }>
& XOR< { left?: number }, { screenX?: number }>
& XOR< { top?: number }, { screenY?: number }>
```
2022-04-06 05:56:08 +00:00
2022-09-03 12:30:41 +00:00
## Parameters
### `to`
2024-06-28 11:49:16 +00:00
**Type**: [`RouteLocationRaw` ](https://router.vuejs.org/api/interfaces/RouteLocationOptions.html#Interface-RouteLocationOptions ) | `undefined` | `null`
2022-09-03 12:30:41 +00:00
**Default**: `'/'`
`to` can be a plain string or a route object to redirect to. When passed as `undefined` or `null` , it will default to `'/'` .
2024-10-20 10:02:56 +00:00
#### Example
```ts
// Passing the URL directly will redirect to the '/blog' page
await navigateTo('/blog')
// Using the route object, will redirect to the route with the name 'blog'
await navigateTo({ name: 'blog' })
// Redirects to the 'product' route while passing a parameter (id = 1) using the route object.
await navigateTo({ name: 'product', params: { id: 1 } })
```
2022-09-03 12:30:41 +00:00
### `options` (optional)
**Type**: `NavigateToOptions`
An object accepting the following properties:
2025-02-08 09:08:38 +00:00
- `replace`
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
- **Type**: `boolean`
- **Default**: `false`
- By default, `navigateTo` pushes the given route into the Vue Router's instance on the client side.
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
This behavior can be changed by setting `replace` to `true` , to indicate that given route should be replaced.
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
- `redirectCode`
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
- **Type**: `number`
- **Default**: `302`
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
- `navigateTo` redirects to the given path and sets the redirect code to [`302 Found` ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302 ) by default when the redirection takes place on the server side.
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
This default behavior can be modified by providing different `redirectCode` . Commonly, [`301 Moved Permanently` ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301 ) can be used for permanent redirections.
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
- `external`
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
- **Type**: `boolean`
- **Default**: `false`
2022-09-03 12:30:41 +00:00
2025-02-08 09:08:38 +00:00
- Allows navigating to an external URL when set to `true` . Otherwise, `navigateTo` will throw an error, as external navigation is not allowed by default.
2023-06-07 19:27:00 +00:00
2025-02-08 09:08:38 +00:00
- `open`
2023-06-07 19:27:00 +00:00
2025-02-08 09:08:38 +00:00
- **Type**: `OpenOptions`
- Allows navigating to the URL using the [open() ](https://developer.mozilla.org/en-US/docs/Web/API/Window/open ) method of the window. This option is only applicable on the client side and will be ignored on the server side.
2023-06-07 19:27:00 +00:00
An object accepting the following properties:
2025-02-08 09:08:38 +00:00
- `target`
2023-06-07 19:27:00 +00:00
2025-02-08 09:08:38 +00:00
- **Type**: `string`
- **Default**: `'_blank'`
2023-06-07 19:27:00 +00:00
2025-02-08 09:08:38 +00:00
- A string, without whitespace, specifying the name of the browsing context the resource is being loaded into.
2024-02-21 17:09:45 +00:00
2025-02-08 09:08:38 +00:00
- `windowFeatures`
2023-06-07 19:27:00 +00:00
2025-02-08 09:08:38 +00:00
- **Type**: `OpenWindowFeatures`
2023-06-07 19:27:00 +00:00
2025-02-08 09:08:38 +00:00
- An object accepting the following properties:
2024-02-21 17:09:45 +00:00
2025-02-08 09:08:38 +00:00
| Property | Type | Description |
|----------|---------|--------------|
| `popup` | `boolean` | Requests a minimal popup window instead of a new tab, with UI features decided by the browser. |
| `width` or `innerWidth` | `number` | Specifies the content area's width (minimum 100 pixels), including scrollbars. |
| `height` or `innerHeight` | `number` | Specifies the content area's height (minimum 100 pixels), including scrollbars. |
| `left` or `screenX` | `number` | Sets the horizontal position of the new window relative to the left edge of the screen. |
| `top` or `screenY` | `number` | Sets the vertical position of the new window relative to the top edge of the screen. |
| `noopener` | `boolean` | Prevents the new window from accessing the originating window via `window.opener` . |
| `noreferrer` | `boolean` | Prevents the Referer header from being sent and implicitly enables `noopener` . |
2023-06-07 19:27:00 +00:00
2025-02-08 09:08:38 +00:00
Refer to the [documentation ](https://developer.mozilla.org/en-US/docs/Web/API/Window/open#windowfeatures ) for more detailed information on the **windowFeatures** properties.