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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/content/3.guide/1.writing/7.document-driven.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,38 @@ const { theme } = useContent().globals

Any changes to these files will be automatically reflected in the application during development.

## Disable or control the page data

Using special `documentDriven` meta in your pages, you can disable this feature for specific route or control it's behavior.

### Disable document driven

Setting `documentDriven` to `false` will disable document driven. This means that exposed refs from `useContent()` will be `undefined`.

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

### Control data

To control document driven data you can pass an object to `documentDriven` meta key and enable/disable specific parts of it.

```html
<script setup lang="ts">
definePageMeta({
documentDriven: {
page: true, // Keep page fetching enabled
surround: false // Disable surround fetching
}
})
</script>
```


## Example

Jump into the [Document Driven Example](/examples/essentials/document-driven) to see a working example.
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
// Disabled document driven mode on next route
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: 0 additions & 1 deletion test/features/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const testNavigation = () => {
_params: jsonStringify(query)
}
})
console.log(JSON.stringify(list, null, 2))

expect(list.find(item => item._path === '/')).toBeTruthy()
expect(list.find(item => item._path === '/').children).toBeUndefined()
Expand Down
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>