feat(nuxt): allow getRouteFromPath to use objects (#5900)

This commit is contained in:
TheColaber 2022-08-22 12:08:43 -04:00 committed by GitHub
parent 39a6054a44
commit 9f1ecd14c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
import { reactive, h } from 'vue' import { reactive, h } from 'vue'
import { parseURL, parseQuery, withoutBase, isEqual, joinURL } from 'ufo' import { parseURL, stringifyParsedURL, parseQuery, stringifyQuery, withoutBase, isEqual, joinURL } from 'ufo'
import { createError } from 'h3' import { createError } from 'h3'
import { defineNuxtPlugin, clearError, navigateTo, showError, useRuntimeConfig } from '..' import { defineNuxtPlugin, clearError, navigateTo, showError, useRuntimeConfig } from '..'
import { callWithNuxt } from '../nuxt' import { callWithNuxt } from '../nuxt'
@ -28,9 +28,13 @@ interface Route {
meta: Record<string, any>; meta: Record<string, any>;
} }
function getRouteFromPath (fullPath: string | Record<string, unknown>) { function getRouteFromPath (fullPath: string | Partial<Route>) {
if (typeof fullPath === 'object') { if (typeof fullPath === 'object') {
throw new TypeError('[nuxt] Route location object cannot be resolved when vue-router is disabled (no pages).') fullPath = stringifyParsedURL({
pathname: fullPath.path || '',
search: stringifyQuery(fullPath.query || {}),
hash: fullPath.hash || ''
})
} }
const url = parseURL(fullPath.toString()) const url = parseURL(fullPath.toString())
@ -79,7 +83,7 @@ interface Router {
afterEach: (guard: RouterHooks['navigate:after']) => () => void afterEach: (guard: RouterHooks['navigate:after']) => () => void
onError: (handler: RouterHooks['error']) => () => void onError: (handler: RouterHooks['error']) => () => void
// Routes // Routes
resolve: (url: string | Record<string, unknown>) => Route resolve: (url: string | Partial<Route>) => Route
addRoute: (parentName: string, route: Route) => void addRoute: (parentName: string, route: Route) => void
getRoutes: () => any[] getRoutes: () => any[]
hasRoute: (name: string) => boolean hasRoute: (name: string) => boolean
@ -107,7 +111,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
const baseURL = useRuntimeConfig().app.baseURL const baseURL = useRuntimeConfig().app.baseURL
const route: Route = reactive(getRouteFromPath(initialURL)) const route: Route = reactive(getRouteFromPath(initialURL))
async function handleNavigation (url: string, replace?: boolean): Promise<void> { async function handleNavigation (url: string | Partial<Route>, replace?: boolean): Promise<void> {
try { try {
// Resolve route // Resolve route
const to = getRouteFromPath(url) const to = getRouteFromPath(url)
@ -127,7 +131,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
// Perform navigation // Perform navigation
Object.assign(route, to) Object.assign(route, to)
if (process.client) { if (process.client) {
window.history[replace ? 'replaceState' : 'pushState']({}, '', joinURL(baseURL, url)) window.history[replace ? 'replaceState' : 'pushState']({}, '', joinURL(baseURL, to.fullPath))
if (!nuxtApp.isHydrating) { if (!nuxtApp.isHydrating) {
// Clear any existing errors // Clear any existing errors
await callWithNuxt(nuxtApp, clearError) await callWithNuxt(nuxtApp, clearError)