Skip to content

Commit 4814ac1

Browse files
farnabazatinuxTahul
committed
feat: disable document driven with route meta (#1333)
Co-authored-by: Sébastien Chopin <[email protected]> Co-authored-by: Yaël Guilloux <[email protected]>
1 parent 1ef469c commit 4814ac1

File tree

7 files changed

+100
-15
lines changed

7 files changed

+100
-15
lines changed

docs/content/3.guide/1.writing/7.document-driven.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,38 @@ const { theme } = useContent().globals
164164

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

167+
## Disable or control the page data
168+
169+
Using special `documentDriven` meta in your pages, you can disable this feature for specific route or control it's behavior.
170+
171+
### Disable document driven
172+
173+
Setting `documentDriven` to `false` will disable document driven. This means that exposed refs from `useContent()` will be `undefined`.
174+
175+
```html
176+
<script setup lang="ts">
177+
definePageMeta({
178+
documentDriven: false
179+
})
180+
</script>
181+
```
182+
183+
### Control data
184+
185+
To control document driven data you can pass an object to `documentDriven` meta key and enable/disable specific parts of it.
186+
187+
```html
188+
<script setup lang="ts">
189+
definePageMeta({
190+
documentDriven: {
191+
page: true, // Keep page fetching enabled
192+
surround: false // Disable surround fetching
193+
}
194+
})
195+
</script>
196+
```
197+
198+
167199
## Example
168200

169201
Jump into the [Document Driven Example](/examples/essentials/document-driven) to see a working example.

src/runtime/plugins/documentDriven.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ export default defineNuxtPlugin((nuxt) => {
4646
}
4747

4848
const refresh = async (to: RouteLocationNormalized | RouteLocationNormalizedLoaded, force: boolean = false) => {
49+
const routeConfig = (to.meta.documentDriven || {}) as any
50+
// Disabled document driven mode on next route
51+
if (to.meta.documentDriven === false) {
52+
return
53+
}
54+
4955
const { navigation, page, globals, surround } = useContentState()
5056

5157
// Normalize route path
@@ -57,7 +63,7 @@ export default defineNuxtPlugin((nuxt) => {
5763
*
5864
* `navigation`
5965
*/
60-
if (moduleOptions.navigation) {
66+
if (moduleOptions.navigation && routeConfig.navigation !== false) {
6167
const navigationQuery = () => {
6268
const { navigation } = useContentState()
6369

@@ -128,7 +134,7 @@ export default defineNuxtPlugin((nuxt) => {
128134
/**
129135
* `page`
130136
*/
131-
if (moduleOptions.page) {
137+
if (moduleOptions.page && routeConfig.page !== false) {
132138
const pageQuery = () => {
133139
const { page } = useContentState()
134140

@@ -155,7 +161,7 @@ export default defineNuxtPlugin((nuxt) => {
155161
/**
156162
* `surround`
157163
*/
158-
if (moduleOptions.surround) {
164+
if (moduleOptions.surround && routeConfig.surround !== false) {
159165
const surroundQuery = () => {
160166
// Return same surround as page is already loaded
161167
if (!force && page.value && page.value._path === _path) {
@@ -195,22 +201,23 @@ export default defineNuxtPlugin((nuxt) => {
195201
if (_globals) {
196202
globals.value = _globals
197203
}
198-
// Find used layout
199-
const layoutName = findLayout(to, _page, _navigation, _globals)
200-
201-
// Prefetch layout component
202-
const layout = layouts[layoutName]
203-
204-
if (layout && layout?.__asyncLoader && !layout.__asyncResolved) {
205-
await layout.__asyncLoader()
206-
}
207-
// Apply layout
208-
to.meta.layout = layoutName
209204

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

213208
if (_page) {
209+
// Find used layout
210+
const layoutName = findLayout(to, _page, _navigation, _globals)
211+
212+
// Prefetch layout component
213+
const layout = layouts[layoutName]
214+
215+
if (layout && layout?.__asyncLoader && !layout.__asyncResolved) {
216+
await layout.__asyncLoader()
217+
}
218+
// Apply layout
219+
to.meta.layout = layoutName
220+
214221
// Update values
215222
page.value = _page
216223
process.client && pagesCache.set(_path, _page)

test/document-driven.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,18 @@ describe('fixtures:document-driven', async () => {
1313

1414
expect(html).contains('Home | Document Driven Fixture')
1515
})
16+
17+
test('disabled document driven', async () => {
18+
const html = await $fetch('/disabled')
19+
20+
expect(html).contains('<div>surround: </div>')
21+
expect(html).contains('<div>page: </div>')
22+
})
23+
24+
test('disabled surround document driven', async () => {
25+
const html = await $fetch('/no-surround')
26+
27+
expect(html).contains('<div>surround: </div>')
28+
expect(html).contains('<div>page: {')
29+
})
1630
})

test/features/navigation.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const testNavigation = () => {
1212
_params: jsonStringify(query)
1313
}
1414
})
15-
console.log(JSON.stringify(list, null, 2))
1615

1716
expect(list.find(item => item._path === '/')).toBeTruthy()
1817
expect(list.find(item => item._path === '/').children).toBeUndefined()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Surround disabled in document driven
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<div>
3+
Document driven is disabled for this page
4+
<div>page: {{ page }}</div>
5+
<div>surround: {{ surround }}</div>
6+
</div>
7+
</template>
8+
9+
<script setup lang="ts">
10+
definePageMeta({
11+
documentDriven: false
12+
})
13+
14+
const { page, surround } = useContent()
15+
</script>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div>
3+
Document driven page is disabled for this route
4+
<div>page: {{ page }}</div>
5+
<div>surround: {{ surround }}</div>
6+
</div>
7+
</template>
8+
9+
<script setup lang="ts">
10+
definePageMeta({
11+
documentDriven: {
12+
surround: false
13+
}
14+
})
15+
16+
const { page, surround } = useContent()
17+
</script>

0 commit comments

Comments
 (0)