This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(nuxt): navigateTo
supports external redirects
#5022
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a2bca32
wip
TheAlexLichter 9bb7c33
wip
TheAlexLichter 37d150d
feat: improve external url detection
TheAlexLichter fb67669
fix: handle external link when processing middleware
TheAlexLichter c69ddd0
fix: return fallback '/' for getPath
TheAlexLichter ca0db61
refactor: code style
TheAlexLichter 4e3207f
refactor: use ufo for protocol check
TheAlexLichter 59a3549
refactor: use `allowExternal` and check for scripts
TheAlexLichter 633e6e3
fix: test
TheAlexLichter 343c570
Merge branch 'main' into pr/manniL/5022
pi0 f750239
Merge branch 'main' into feat-redirect-external
TheAlexLichter dea1f12
fix: always return promise.
pi0 89c6772
refactor: update error message and always throw error
pi0 68b7f27
refactor: inline toPath computation and avoid extra url parsing
pi0 aeeecdc
small change
pi0 6ddf63b
update fixture
pi0 d97e68a
t y p o
pi0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { getCurrentInstance, inject } from 'vue' | ||
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, NavigationFailure } from 'vue-router' | ||
import type { Router, RouteLocationNormalizedLoaded, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, NavigationFailure, RouteLocationPathRaw } from 'vue-router' | ||
import { sendRedirect } from 'h3' | ||
import { joinURL } from 'ufo' | ||
import { hasProtocol, joinURL, parseURL } from 'ufo' | ||
import { useNuxtApp, useRuntimeConfig } from '#app' | ||
|
||
export const useRouter = () => { | ||
|
@@ -58,26 +58,49 @@ const isProcessingMiddleware = () => { | |
|
||
export interface NavigateToOptions { | ||
replace?: boolean | ||
redirectCode?: number | ||
redirectCode?: number, | ||
external?: boolean | ||
} | ||
|
||
export const navigateTo = (to: RouteLocationRaw | undefined | null, options: NavigateToOptions = {}): Promise<void | NavigationFailure> | RouteLocationRaw => { | ||
if (!to) { | ||
to = '/' | ||
} | ||
// Early redirect on client-side since only possible option is redirectCode and not applied | ||
if (process.client && isProcessingMiddleware()) { | ||
|
||
const toPath = typeof to === 'string' ? to : ((to as RouteLocationPathRaw).path || '/') | ||
const isExternal = hasProtocol(toPath, true) | ||
if (isExternal && !options.external) { | ||
throw new Error('Navigating to external URL is not allowed by default. Use `nagivateTo (url, { external: true })`.') | ||
} | ||
if (isExternal && parseURL(toPath).protocol === 'script:') { | ||
throw new Error('Cannot navigate to an URL with script protocol.') | ||
} | ||
|
||
// Early redirect on client-side | ||
if (!isExternal && isProcessingMiddleware()) { | ||
return to | ||
} | ||
|
||
const router = useRouter() | ||
|
||
if (process.server) { | ||
const nuxtApp = useNuxtApp() | ||
if (nuxtApp.ssrContext && nuxtApp.ssrContext.event) { | ||
const redirectLocation = joinURL(useRuntimeConfig().app.baseURL, router.resolve(to).fullPath || '/') | ||
return nuxtApp.callHook('app:redirected').then(() => sendRedirect(nuxtApp.ssrContext!.event, redirectLocation, options.redirectCode || 302)) | ||
const redirectLocation = isExternal ? toPath : joinURL(useRuntimeConfig().app.baseURL, router.resolve(to).fullPath || '/') | ||
return nuxtApp.callHook('app:redirected').then(() => sendRedirect(nuxtApp.ssrContext!.event, redirectLocation, options.redirectCode || 301)) | ||
} | ||
} | ||
|
||
// Client-side redirection using vue-router | ||
if (isExternal) { | ||
if (options.replace) { | ||
location.replace(toPath) | ||
} else { | ||
location.href = toPath | ||
} | ||
return Promise.resolve() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One last thing to ensure: In nuxt 2, we had this problem with external navigations that during navigation, rest of logic goes on. And solution was a custom made error because it was sync. I think we could do something like a never resolving promise that avoids this. (will make a reproduction and implement in later PR btw need more testing) => https://github.com/nuxt/framework/issues/6906 |
||
} | ||
|
||
return options.replace ? router.replace(to) : router.push(to) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<template> | ||
<div>You should not see me</div> | ||
</template> | ||
|
||
<script setup> | ||
await navigateTo('https://example.com/', { external: true, replace: true }) | ||
</script> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why you check protocol for external? may be I want to redirect within current domain, i.e. respond with headers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please open an issue with a sandbox to track? ππΌ