Skip to content

Commit

Permalink
feat(document-driven): support navigation.redirect from _dir files (
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Oct 19, 2022
1 parent 164f63e commit 186e463
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
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 }

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)) {
return 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)) {
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
7 changes: 6 additions & 1 deletion test/document-driven.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fileURLToPath } from 'url'
import { test, describe, expect } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils'
import { setup, $fetch, url } from '@nuxt/test-utils'

describe('fixtures:document-driven', async () => {
await setup({
Expand Down Expand Up @@ -69,4 +69,9 @@ describe('fixtures:document-driven', async () => {
expect(e.response.statusText).toBe('Not Found')
}
})

test('redirect in `_dir.yml`', async () => {
const response = await fetch(url('/redirect'))
expect(response.url).toBe('https://nuxtjs.org/')
})
})
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

0 comments on commit 186e463

Please sign in to comment.