Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): support redirect within page metadata #7746

Merged
merged 2 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/nuxt/src/pages/runtime/composables.ts
@@ -1,8 +1,20 @@
import { KeepAliveProps, TransitionProps, UnwrapRef } from 'vue'
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import type { RouteLocationNormalizedLoaded, RouteRecordRedirectOption } from 'vue-router'

export interface PageMeta {
[key: string]: any
/**
* Where to redirect if the route is directly matched. The redirection happens
* before any navigation guard and triggers a new navigation with the new
* target location.
*/
redirect?: RouteRecordRedirectOption
/**
* Aliases for the record. Allows defining extra paths that will behave like a
* copy of the record. Allows having paths shorthands like `/users/:id` and
* `/u/:id`. All `alias` and `path` values must share the same params.
*/
alias?: string | string[]
pageTransition?: boolean | TransitionProps
layoutTransition?: boolean | TransitionProps
key?: false | string | ((route: RouteLocationNormalizedLoaded) => string)
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/pages/utils.ts
Expand Up @@ -243,6 +243,7 @@ export function normalizeRoutes (routes: NuxtPage[], metaImports: Set<string> =
children: route.children ? normalizeRoutes(route.children, metaImports).routes : [],
meta: route.meta ? `{...(${metaImportName} || {}), ...${JSON.stringify(route.meta)}}` : metaImportName,
alias: aliasCode,
redirect: route.redirect ? JSON.stringify(route.redirect) : `${metaImportName}?.redirect || undefined`,
component: genDynamicImport(file, { interopDefault: true })
}
}))
Expand Down
3 changes: 2 additions & 1 deletion packages/schema/src/types/hooks.ts
Expand Up @@ -43,7 +43,8 @@ export type NuxtPage = {
path: string
file: string
meta?: Record<string, any>
alias?: string[]
alias?: string[] | string
redirect?: string
children?: NuxtPage[]
}

Expand Down
10 changes: 10 additions & 0 deletions test/basic.test.ts
Expand Up @@ -55,6 +55,16 @@ describe('pages', () => {
await expectNoClientErrors('/')
})

it('respects aliases in page metadata', async () => {
const html = await $fetch('/some-alias')
expect(html).toContain('Hello Nuxt 3!')
})

it('respects redirects in page metadata', async () => {
const { headers } = await fetch('/redirect', { redirect: 'manual' })
expect(headers.get('location')).toEqual('/')
})

it('render 404', async () => {
const html = await $fetch('/not-found')

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/basic/pages/index.vue
Expand Up @@ -28,6 +28,10 @@ setupDevtoolsPlugin({}, () => {})

const config = useRuntimeConfig()

definePageMeta({
alias: '/some-alias'
})

// reset title template example
useHead({
titleTemplate: ''
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/basic/pages/redirect.vue
@@ -0,0 +1,11 @@
<script setup>
definePageMeta({
redirect: () => '/'
})
</script>

<template>
<div>
<div>redirect.vue</div>
</div>
</template>