-
-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(navigation): prevent client-db conflict
- Loading branch information
Showing
2 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |