Skip to content

Commit fd8e3b8

Browse files
committed
fix(navigation): prevent client-db conflict
1 parent b680b47 commit fd8e3b8

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ export default defineNuxtModule<ModuleOptions>({
520520

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

525525
nuxt.hook('nitro:config', (nitroConfig) => {
526526
nitroConfig.handlers = nitroConfig.handlers || []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { hash } from 'ohash'
2+
import type { NavItem, QueryBuilder, QueryBuilderParams } from '../../types'
3+
import { encodeQueryParams } from '../../utils/query'
4+
import { jsonStringify } from '../../utils/json'
5+
import { ContentQueryBuilder } from '../../types/query'
6+
import { addPrerenderPath, shouldUseClientDB, withContentBase } from '../../composables/utils'
7+
import { useContentPreview } from '../../composables/preview'
8+
import { queryContent } from './query'
9+
import { useRuntimeConfig } from '#app'
10+
11+
export const fetchContentNavigation = async (queryBuilder?: QueryBuilder | QueryBuilderParams | ContentQueryBuilder): Promise<Array<NavItem>> => {
12+
const { content } = useRuntimeConfig().public
13+
14+
// Ensure that queryBuilder is an instance of QueryBuilder
15+
if (typeof queryBuilder?.params !== 'function') {
16+
queryBuilder = queryContent(queryBuilder as any)
17+
}
18+
19+
// Get query params from queryBuilder instance to ensure default values are applied
20+
const params = queryBuilder.params()
21+
22+
const apiPath = content.experimental.stripQueryParameters
23+
? withContentBase(`/navigation/${process.dev ? '_' : `${hash(params)}.${content.integrity}`}/${encodeQueryParams(params)}.json`)
24+
: withContentBase(process.dev ? `/navigation/${hash(params)}` : `/navigation/${hash(params)}.${content.integrity}.json`)
25+
26+
// Add `prefetch` to `<head>` in production
27+
if (!process.dev && process.server) {
28+
addPrerenderPath(apiPath)
29+
}
30+
31+
if (shouldUseClientDB()) {
32+
const generateNavigation = await import('./client-db').then(m => m.generateNavigation)
33+
return generateNavigation(params)
34+
}
35+
36+
const data = await $fetch(apiPath as any, {
37+
method: 'GET',
38+
responseType: 'json',
39+
params: content.experimental.stripQueryParameters
40+
? undefined
41+
: {
42+
_params: jsonStringify(params),
43+
previewToken: useContentPreview().getPreviewToken()
44+
}
45+
})
46+
47+
// On SSG, all url are redirected to `404.html` when not found, so we need to check the content type
48+
// to know if the response is a valid JSON or not
49+
if (typeof data === 'string' && (data as string).startsWith('<!DOCTYPE html>')) {
50+
throw new Error('Not found')
51+
}
52+
53+
return data
54+
}

0 commit comments

Comments
 (0)