Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(document-driven): support navigation.redirect from _dir files #1545

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions src/runtime/plugins/documentDriven.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router'
// @ts-ignore
import { useRuntimeConfig, addRouteMiddleware } from '#app'
import { withoutTrailingSlash } from 'ufo'
import { useRuntimeConfig, addRouteMiddleware, callWithNuxt, navigateTo } from '#app'
import { withoutTrailingSlash, hasProtocol } from 'ufo'
import { NavItem, ParsedContent } from '../types'
// @ts-ignore
import { defineNuxtPlugin, queryContent, useContentHelpers, useContentState, fetchContentNavigation, useRoute } from '#imports'
Expand Down Expand Up @@ -224,6 +224,7 @@ export default defineNuxtPlugin((nuxt) => {

// Use `redirect` key to redirect to another page
if (_page?.redirect) { return _page?.redirect }
if (_page?._dir?.navigation?.redirect) { return _page?._dir?.navigation?.redirect }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leverage navKeyFromPath(_page._path, 'redirect', navigation) instead of fetching the _dir.yml


if (_page) {
// Find used layout
Expand Down Expand Up @@ -258,7 +259,13 @@ export default defineNuxtPlugin((nuxt) => {

const redirect = await refresh(to, false)

if (redirect) { return redirect }
if (redirect) {
if (hasProtocol(redirect)) {
callWithNuxt(nuxt, navigateTo, [redirect, { external: true }])
} else {
return redirect
}
}
})

if (process.server) {
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/server/api/query.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { createError, defineEventHandler } from 'h3'
import { join } from 'pathe'
import { serverQueryContent } from '../storage'
import { getContentQuery } from '../../utils/query'

export default defineEventHandler(async (event) => {
const query = getContentQuery(event)
const contents = await serverQueryContent(event, query).find()

if (query.first) {
const path = contents?._path || query.where.find(w => w._path)?._path
if (path) {
const _dir = await serverQueryContent(event).where({ _path: join(path, '_dir') }).without('_').findOne()
if (!Array.isArray(_dir)) {
Atinux marked this conversation as resolved.
Show resolved Hide resolved
return {
_path: path,
...contents,
_dir
}
}
}
}

// If no documents matchs and using findOne()
if (query.first && Array.isArray(contents) && contents.length === 0) {
throw createError({
Expand Down
9 changes: 9 additions & 0 deletions test/document-driven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ describe('fixtures:document-driven', async () => {
expect(e.response.statusText).toBe('Not Found')
}
})

test('redirect in `_dir.yml`', async () => {
await $fetch('/redirect', {
onResponse (ctx) {
expect(ctx.response.status).toBe(302)
return Promise.resolve()
}
})
})
})
1 change: 1 addition & 0 deletions test/fixtures/document-driven/content/redirect/_dir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
navigation.redirect: https://nuxtjs.org