(to: RouteLocationRaw | undefined | null, options?: NavigateToOptions)
| 150 | * @since 3.0.0 |
| 151 | */ |
| 152 | export 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 |
searching dependent graphs…