Skip to content

Commit

Permalink
fix(navigation): prevent client-db conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Aug 30, 2023
1 parent b680b47 commit fd8e3b8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/module.ts
Expand Up @@ -520,7 +520,7 @@ export default defineNuxtModule<ModuleOptions>({

// Register navigation
if (options.navigation) {
addImports({ name: 'fetchContentNavigation', as: 'fetchContentNavigation', from: resolveRuntimeModule('./composables/navigation') })
addImports({ name: 'fetchContentNavigation', as: 'fetchContentNavigation', from: resolveRuntimeModule(`./${options.experimental.advanceQuery ? '' : 'legacy/'}composables/navigation`) })

nuxt.hook('nitro:config', (nitroConfig) => {
nitroConfig.handlers = nitroConfig.handlers || []
Expand Down
54 changes: 54 additions & 0 deletions src/runtime/legacy/composables/navigation.ts
@@ -0,0 +1,54 @@
import { hash } from 'ohash'
import type { NavItem, QueryBuilder, QueryBuilderParams } from '../../types'
import { encodeQueryParams } from '../../utils/query'
import { jsonStringify } from '../../utils/json'
import { ContentQueryBuilder } from '../../types/query'
import { addPrerenderPath, shouldUseClientDB, withContentBase } from '../../composables/utils'
import { useContentPreview } from '../../composables/preview'
import { queryContent } from './query'
import { useRuntimeConfig } from '#app'

export const fetchContentNavigation = async (queryBuilder?: QueryBuilder | QueryBuilderParams | ContentQueryBuilder): Promise<Array<NavItem>> => {
const { content } = useRuntimeConfig().public

// Ensure that queryBuilder is an instance of QueryBuilder
if (typeof queryBuilder?.params !== 'function') {
queryBuilder = queryContent(queryBuilder as any)
}

// Get query params from queryBuilder instance to ensure default values are applied
const params = queryBuilder.params()

const apiPath = content.experimental.stripQueryParameters
? withContentBase(`/navigation/${process.dev ? '_' : `${hash(params)}.${content.integrity}`}/${encodeQueryParams(params)}.json`)
: withContentBase(process.dev ? `/navigation/${hash(params)}` : `/navigation/${hash(params)}.${content.integrity}.json`)

// Add `prefetch` to `<head>` in production
if (!process.dev && process.server) {
addPrerenderPath(apiPath)
}

if (shouldUseClientDB()) {
const generateNavigation = await import('./client-db').then(m => m.generateNavigation)
return generateNavigation(params)
}

const data = await $fetch(apiPath as any, {
method: 'GET',
responseType: 'json',
params: content.experimental.stripQueryParameters
? undefined
: {
_params: jsonStringify(params),
previewToken: useContentPreview().getPreviewToken()
}
})

// On SSG, all url are redirected to `404.html` when not found, so we need to check the content type
// to know if the response is a valid JSON or not
if (typeof data === 'string' && (data as string).startsWith('<!DOCTYPE html>')) {
throw new Error('Not found')
}

return data
}

0 comments on commit fd8e3b8

Please sign in to comment.