Skip to content

Commit

Permalink
feat(document-driven): introduce start and finish hooks (#1744)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Dec 12, 2022
1 parent 7ff1018 commit d2e59c2
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/runtime/plugins/documentDriven.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router'
// @ts-ignore
import { useRuntimeConfig, addRouteMiddleware, callWithNuxt, navigateTo } from '#app'
import type { NuxtApp } from 'nuxt/app'
import { withoutTrailingSlash, hasProtocol } from 'ufo'
import { NavItem, ParsedContent } from '../types'
// @ts-ignore
import { defineNuxtPlugin, queryContent, useContentHelpers, useContentState, fetchContentNavigation, useRoute } from '#imports'
// @ts-ignore
import layouts from '#build/layouts'

export default defineNuxtPlugin((nuxt) => {
export default defineNuxtPlugin((nuxt: NuxtApp) => {
const { documentDriven: moduleOptions, clientDB } = useRuntimeConfig()?.public?.content

/**
Expand Down Expand Up @@ -43,17 +44,17 @@ export default defineNuxtPlugin((nuxt) => {
return 'default'
}

const refresh = async (to: RouteLocationNormalized | RouteLocationNormalizedLoaded, force = false) => {
const refresh = async (to: RouteLocationNormalized | RouteLocationNormalizedLoaded, dedup = false) => {
// Call hook before fetching content
// @ts-ignore
nuxt.callHook('content:document-driven:start', { route: to, dedup })

const routeConfig = (to.meta.documentDriven || {}) as any
// Disabled document driven mode on next route
if (to.meta.documentDriven === false) {
return
}

// Expose hook to be used for loading indicators
// @ts-ignore
!force && nuxt.callHook('content:middleware:start')

const { navigation, pages, globals, surrounds } = useContentState()

// Normalize route path
Expand All @@ -70,7 +71,7 @@ export default defineNuxtPlugin((nuxt) => {
const navigationQuery = () => {
const { navigation } = useContentState()

if (navigation.value && !force) { return navigation.value }
if (navigation.value && !dedup) { return navigation.value }

return fetchContentNavigation()
.then((_navigation) => {
Expand Down Expand Up @@ -104,7 +105,7 @@ export default defineNuxtPlugin((nuxt) => {
Object.entries(moduleOptions.globals).map(
([key, query]: [string, any]) => {
// Avoid fetching same file twice
if (!force && globals.value[key]) { return globals.value[key] }
if (!dedup && globals.value[key]) { return globals.value[key] }

// Supports `find` if passed as `query: 'find'` in the query definition.
let type = 'findOne'
Expand Down Expand Up @@ -147,7 +148,7 @@ export default defineNuxtPlugin((nuxt) => {
const { pages } = useContentState()

// Return same page as page is already loaded
if (!force && pages.value[_path] && pages.value[_path]._path === _path) {
if (!dedup && pages.value[_path] && pages.value[_path]._path === _path) {
return pages.value[_path]
}

Expand Down Expand Up @@ -177,7 +178,7 @@ export default defineNuxtPlugin((nuxt) => {
const { surrounds } = useContentState()

// Return same surround as page is already loaded
if (!force && surrounds.value[_path]) {
if (!dedup && surrounds.value[_path]) {
return surrounds.value[_path]
}

Expand Down Expand Up @@ -236,6 +237,10 @@ export default defineNuxtPlugin((nuxt) => {
if (_surround) {
surrounds.value[_path] = _surround
}

// Call hook after content is fetched
// @ts-ignore
await nuxt.callHook('content:document-driven:finish', { route: to, dedup, page: _page, navigation: _navigation, globals: _globals, surround: _surround })
})
}

Expand Down

0 comments on commit d2e59c2

Please sign in to comment.