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: disable document driven with route meta #1333

Merged
merged 5 commits into from
Jul 6, 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
35 changes: 21 additions & 14 deletions src/runtime/plugins/documentDriven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export default defineNuxtPlugin((nuxt) => {
}

const refresh = async (to: RouteLocationNormalized | RouteLocationNormalizedLoaded, force: boolean = false) => {
const routeConfig = (to.meta.documentDriven || {}) as any
// Document drive disabled on route
farnabaz marked this conversation as resolved.
Show resolved Hide resolved
if (to.meta.documentDriven === false) {
return
}

const { navigation, page, globals, surround } = useContentState()

// Normalize route path
Expand All @@ -57,7 +63,7 @@ export default defineNuxtPlugin((nuxt) => {
*
* `navigation`
*/
if (moduleOptions.navigation) {
if (moduleOptions.navigation && routeConfig.navigation !== false) {
const navigationQuery = () => {
const { navigation } = useContentState()

Expand Down Expand Up @@ -128,7 +134,7 @@ export default defineNuxtPlugin((nuxt) => {
/**
* `page`
*/
if (moduleOptions.page) {
if (moduleOptions.page && routeConfig.page !== false) {
const pageQuery = () => {
const { page } = useContentState()

Expand All @@ -155,7 +161,7 @@ export default defineNuxtPlugin((nuxt) => {
/**
* `surround`
*/
if (moduleOptions.surround) {
if (moduleOptions.surround && routeConfig.surround !== false) {
const surroundQuery = () => {
// Return same surround as page is already loaded
if (!force && page.value && page.value._path === _path) {
Expand Down Expand Up @@ -195,22 +201,23 @@ export default defineNuxtPlugin((nuxt) => {
if (_globals) {
globals.value = _globals
}
// Find used layout
const layoutName = findLayout(to, _page, _navigation, _globals)

// Prefetch layout component
const layout = layouts[layoutName]

if (layout && layout?.__asyncLoader && !layout.__asyncResolved) {
await layout.__asyncLoader()
}
// Apply layout
to.meta.layout = layoutName

// Use `redirect` key to redirect to another page
if (_page?.redirect) { return _page?.redirect }

if (_page) {
// Find used layout
const layoutName = findLayout(to, _page, _navigation, _globals)

// Prefetch layout component
const layout = layouts[layoutName]

if (layout && layout?.__asyncLoader && !layout.__asyncResolved) {
await layout.__asyncLoader()
}
// Apply layout
to.meta.layout = layoutName

// Update values
page.value = _page
process.client && pagesCache.set(_path, _page)
Expand Down
14 changes: 14 additions & 0 deletions test/document-driven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ describe('fixtures:document-driven', async () => {

expect(html).contains('Home | Document Driven Fixture')
})

test('disabled document driven', async () => {
const html = await $fetch('/disabled')

expect(html).contains('<div>surround: </div>')
expect(html).contains('<div>page: </div>')
})

test('disabled surround document driven', async () => {
const html = await $fetch('/no-surround')

expect(html).contains('<div>surround: </div>')
expect(html).contains('<div>page: {')
})
})
1 change: 1 addition & 0 deletions test/fixtures/document-driven/content/no-surround.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Surround disabled in document driven
15 changes: 15 additions & 0 deletions test/fixtures/document-driven/pages/disabled.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<div>
Document driven is disabled for this page
<div>page: {{ page }}</div>
<div>surround: {{ surround }}</div>
</div>
</template>

<script setup lang="ts">
definePageMeta({
documentDriven: false
})

const { page, surround } = useContent()
</script>
17 changes: 17 additions & 0 deletions test/fixtures/document-driven/pages/no-surround.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<div>
Document driven page is disabled for this route
<div>page: {{ page }}</div>
<div>surround: {{ surround }}</div>
</div>
</template>

<script setup lang="ts">
definePageMeta({
documentDriven: {
surround: false
}
})

const { page, surround } = useContent()
</script>