Skip to content

Commit 4ec1621

Browse files
authored
fix(module): put query parameters removal under an experimental flag (#1757)
1 parent e7d11d7 commit 4ec1621

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

src/module.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export interface ModuleOptions {
215215
},
216216
experimental: {
217217
clientDB: boolean
218+
stripQueryParameters: boolean
218219
}
219220
}
220221

@@ -274,13 +275,13 @@ export default defineNuxtModule<ModuleOptions>({
274275
},
275276
documentDriven: false,
276277
experimental: {
277-
clientDB: false
278+
clientDB: false,
279+
stripQueryParameters: false
278280
}
279281
},
280282
async setup (options, nuxt) {
281283
const { resolve } = createResolver(import.meta.url)
282284
const resolveRuntimeModule = (path: string) => resolveModule(path, { paths: resolve('./runtime') })
283-
284285
// Ensure default locale alway is the first item of locales
285286
options.locales = Array.from(new Set([options.defaultLocale, ...options.locales].filter(Boolean))) as string[]
286287

@@ -581,8 +582,9 @@ export default defineNuxtModule<ModuleOptions>({
581582
locales: options.locales,
582583
defaultLocale: contentContext.defaultLocale,
583584
integrity: buildIntegrity,
584-
clientDB: {
585-
isSPA: options.experimental.clientDB && nuxt.options.ssr === false
585+
experimental: {
586+
stripQueryParameters: options.experimental.stripQueryParameters,
587+
clientDB: options.experimental.clientDB && nuxt.options.ssr === false
586588
},
587589
api: {
588590
baseURL: options.api.baseURL
@@ -687,12 +689,9 @@ export default defineNuxtModule<ModuleOptions>({
687689
})
688690

689691
interface ModulePublicRuntimeConfig {
690-
/**
691-
* @experimental
692-
*/
693-
clientDB: {
694-
isSPA: boolean
695-
integrity: number
692+
experimental: {
693+
stripQueryParameters: boolean
694+
clientDB: boolean
696695
}
697696

698697
defaultLocale: ModuleOptions['defaultLocale']

src/runtime/composables/navigation.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { hash } from 'ohash'
22
import { useRuntimeConfig } from '#app'
33
import type { NavItem, QueryBuilder, QueryBuilderParams } from '../types'
44
import { encodeQueryParams } from '../utils/query'
5+
import { jsonStringify } from '../utils/json'
56
import { addPrerenderPath, shouldUseClientDB, withContentBase } from './utils'
67

78
export const fetchContentNavigation = async (queryBuilder?: QueryBuilder | QueryBuilderParams): Promise<Array<NavItem>> => {
@@ -21,7 +22,9 @@ export const fetchContentNavigation = async (queryBuilder?: QueryBuilder | Query
2122
}
2223
}
2324

24-
const apiPath = withContentBase(`/navigation/${process.dev ? '_' : `${hash(params)}.${content.integrity}`}/${encodeQueryParams(params)}.json`)
25+
const apiPath = content.experimental.stripQueryParameters
26+
? withContentBase(`/navigation/${process.dev ? '_' : `${hash(params)}.${content.integrity}`}/${encodeQueryParams(params)}.json`)
27+
: withContentBase(process.dev ? `/navigation/${hash(params)}` : `/navigation/${hash(params)}.${content.integrity}.json`)
2528

2629
// Add `prefetch` to `<head>` in production
2730
if (!process.dev && process.server) {
@@ -33,7 +36,16 @@ export const fetchContentNavigation = async (queryBuilder?: QueryBuilder | Query
3336
return generateNavigation(params)
3437
}
3538

36-
const data = await $fetch<NavItem[]>(apiPath, { method: 'GET', responseType: 'json' })
39+
const data = await $fetch<NavItem[]>(apiPath as any, {
40+
method: 'GET',
41+
responseType: 'json',
42+
params: content.experimental.stripQueryParameters
43+
? undefined
44+
: {
45+
_params: jsonStringify(params),
46+
previewToken: useCookie('previewToken').value
47+
}
48+
})
3749

3850
// On SSG, all url are redirected to `404.html` when not found, so we need to check the content type
3951
// to know if the response is a valid JSON or not

src/runtime/composables/query.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useRuntimeConfig } from '#app'
44
import { createQuery } from '../query/query'
55
import type { ParsedContent, QueryBuilder, QueryBuilderParams } from '../types'
66
import { encodeQueryParams } from '../utils/query'
7+
import { jsonStringify } from '../utils/json'
78
import { addPrerenderPath, shouldUseClientDB, withContentBase } from './utils'
89

910
/**
@@ -37,7 +38,9 @@ export const createQueryFetch = <T = ParsedContent>(path?: string) => async (que
3738

3839
const params = query.params()
3940

40-
const apiPath = withContentBase(`/query/${process.dev ? '_' : `${hash(params)}.${content.integrity}`}/${encodeQueryParams(params)}.json`)
41+
const apiPath = content.experimental.stripQueryParameters
42+
? withContentBase(`/query/${process.dev ? '_' : `${hash(params)}.${content.integrity}`}/${encodeQueryParams(params)}.json`)
43+
: withContentBase(process.dev ? '/query' : `/query/${hash(params)}.${content.integrity}.json`)
4144

4245
// Prefetch the query
4346
if (!process.dev && process.server) {
@@ -49,7 +52,16 @@ export const createQueryFetch = <T = ParsedContent>(path?: string) => async (que
4952
return db.fetch(query as QueryBuilder<ParsedContent>)
5053
}
5154

52-
const data = await $fetch(apiPath as any, { method: 'GET', responseType: 'json' })
55+
const data = await $fetch(apiPath as any, {
56+
method: 'GET',
57+
responseType: 'json',
58+
params: content.experimental.stripQueryParameters
59+
? undefined
60+
: {
61+
_params: jsonStringify(params),
62+
previewToken: useCookie('previewToken').value
63+
}
64+
})
5365

5466
// On SSG, all url are redirected to `404.html` when not found, so we need to check the content type
5567
// to know if the response is a valid JSON or not

src/runtime/composables/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export const addPrerenderPath = (path: string) => {
4343
}
4444

4545
export const shouldUseClientDB = () => {
46-
const { clientDB } = useRuntimeConfig().content
46+
const { experimental } = useRuntimeConfig().content
4747
if (!process.client) { return false }
48-
if (clientDB?.isSPA) { return true }
48+
if (experimental.clientDB) { return true }
4949

5050
const query = useRoute().query
5151
// Disable clientDB when `?preview` is set in query, and it has falsy value

src/runtime/plugins/documentDriven.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { defineNuxtPlugin, queryContent, useContentHelpers, useContentState, fet
1010
import layouts from '#build/layouts'
1111

1212
export default defineNuxtPlugin((nuxt: NuxtApp) => {
13-
const { documentDriven: moduleOptions, clientDB } = useRuntimeConfig()?.public?.content
13+
const { documentDriven: moduleOptions, experimental } = useRuntimeConfig()?.public?.content
1414

1515
/**
1616
* Finds a layout value from a cascade of objects.
@@ -249,7 +249,7 @@ export default defineNuxtPlugin((nuxt: NuxtApp) => {
249249
// TODO: Remove this (https://github.com/nuxt/framework/pull/5274)
250250
if (to.path.includes('favicon.ico')) { return }
251251
// Avoid calling on hash change
252-
if (process.client && !clientDB.isSPA && to.path === from.path) { return }
252+
if (process.client && !experimental.clientDB && to.path === from.path) { return }
253253

254254
const redirect = await refresh(to, false)
255255

0 commit comments

Comments
 (0)