MCPcopy
hub / github.com/nuxt/nuxt / navigateTo

Function navigateTo

packages/nuxt/src/app/composables/router.ts:152–267  ·  view source on GitHub ↗
(to: RouteLocationRaw | undefined | null, options?: NavigateToOptions)

Source from the content-addressed store, hash-verified

150 * @since 3.0.0
151 */
152export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: NavigateToOptions): Promise<void | NavigationFailure | false> | false | void | RouteLocationRaw => {
153 to ||= '/'
154
155 const toPath = typeof to === 'string' ? to : 'path' in to ? resolveRouteObject(to) : useRouter().resolve(to).href
156
157 // Early open handler
158 if (import.meta.client && options?.open) {
159 const { protocol } = new URL(toPath, window.location.href)
160 if (protocol && isScriptProtocol(protocol)) {
161 throw new Error(`Cannot navigate to a URL with '${protocol}' protocol.`)
162 }
163
164 const { target = '_blank', windowFeatures = {} } = options.open
165
166 const features: string[] = []
167 for (const [feature, value] of Object.entries(windowFeatures)) {
168 if (value !== undefined) {
169 features.push(`${feature.toLowerCase()}=${value}`)
170 }
171 }
172
173 open(toPath, target, features.join(', '))
174 return Promise.resolve()
175 }
176
177 const isExternalHost = hasProtocol(toPath, { acceptRelative: true })
178 const isExternal = options?.external || isExternalHost
179 if (isExternal) {
180 if (!options?.external) {
181 throw new Error('Navigating to an external URL is not allowed by default. Use `navigateTo(url, { external: true })`.')
182 }
183 const { protocol } = new URL(toPath, import.meta.client ? window.location.href : 'http://localhost')
184 if (protocol && isScriptProtocol(protocol)) {
185 throw new Error(`Cannot navigate to a URL with '${protocol}' protocol.`)
186 }
187 }
188
189 const inMiddleware = isProcessingMiddleware()
190
191 // Early redirect on client-side
192 if (import.meta.client && !isExternal && inMiddleware) {
193 if (options?.replace) {
194 if (typeof to === 'string') {
195 const { pathname, search, hash } = parseURL(to)
196 return {
197 path: pathname,
198 ...(search && { query: parseQuery(search) }),
199 ...(hash && { hash }),
200 replace: true,
201 }
202 }
203 return { ...to, replace: true }
204 }
205 return to
206 }
207
208 const router = useRouter()
209

Callers 15

setupFunction · 0.90
navigateFunction · 0.90
setupFunction · 0.90
nuxt-page.test.tsFile · 0.85
app-types.tsFile · 0.85

Calls 8

useNuxtAppFunction · 0.90
useRuntimeConfigFunction · 0.90
useRouterFunction · 0.85
isProcessingMiddlewareFunction · 0.85
redirectFunction · 0.85
encodeRoutePathFunction · 0.85
resolveMethod · 0.80
resolveRouteObjectFunction · 0.70

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…