From 82e69d47bef9da5a56e4af063f4d5d540d1a02ed Mon Sep 17 00:00:00 2001 From: Reinier Kaper Date: Sat, 24 Sep 2022 13:38:50 -0400 Subject: [PATCH 1/3] feat: Add option to configure intialCache --- .../nuxt/src/app/composables/asyncData.ts | 8 ++-- packages/schema/src/config/_app.ts | 38 +++++++++++++++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 51a338619e3..172e00d05a6 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -90,17 +90,17 @@ export function useAsyncData< options.server = options.server ?? true options.default = options.default ?? getDefault + // Setup nuxt instance payload + const nuxt = useNuxtApp() + // TODO: remove support for `defer` in Nuxt 3 RC if ((options as any).defer) { console.warn('[useAsyncData] `defer` has been renamed to `lazy`. Support for `defer` will be removed in RC.') } options.lazy = options.lazy ?? (options as any).defer ?? false - options.initialCache = options.initialCache ?? true + options.initialCache = options.initialCache ?? nuxt.ssrContext?.runtimeConfig.app.initialCache options.immediate = options.immediate ?? true - // Setup nuxt instance payload - const nuxt = useNuxtApp() - const useInitialCache = () => (nuxt.isHydrating || options.initialCache) && nuxt.payload.data[key] !== undefined // Create or use a shared asyncData entity diff --git a/packages/schema/src/config/_app.ts b/packages/schema/src/config/_app.ts index a11bdd5e087..b5d10f8f52a 100644 --- a/packages/schema/src/config/_app.ts +++ b/packages/schema/src/config/_app.ts @@ -1,5 +1,5 @@ -import { resolve, join } from 'pathe' import { existsSync, readdirSync } from 'node:fs' +import { resolve, join } from 'pathe' import defu from 'defu' import { defineUntypedSchema } from 'untyped' @@ -8,6 +8,7 @@ import { MetaObject } from '../types/meta' export default defineUntypedSchema({ /** * Vue.js config + * * @version 2 * @version 3 */ @@ -25,10 +26,11 @@ export default defineUntypedSchema({ }, performance: { $resolve: async (val, get) => val ?? await get('dev') - }, + } }, /** * Options for the Vue compiler that will be passed at build time. + * * @see [documentation](https://vuejs.org/api/application.html#app-config-compileroptions) * @type {typeof import('@vue/compiler-core').CompilerOptions} * @version 3 @@ -38,6 +40,7 @@ export default defineUntypedSchema({ /** * Nuxt App configuration. + * * @version 2 * @version 3 */ @@ -46,6 +49,7 @@ export default defineUntypedSchema({ * The base path of your Nuxt application. * * This can be set at runtime by setting the NUXT_APP_BASE_URL environment variable. + * * @example * ```bash * NUXT_APP_BASE_URL=/prefix/ node .output/server/index.mjs @@ -58,6 +62,7 @@ export default defineUntypedSchema({ /** * The folder name for the built site assets, relative to `baseURL` (or `cdnURL` if set). + * * @deprecated - use `buildAssetsDir` instead * @version 2 */ @@ -68,6 +73,7 @@ export default defineUntypedSchema({ * An absolute URL to serve the public folder from (production-only). * * This can be set to a different value at runtime by setting the `NUXT_APP_CDN_URL` environment variable. + * * @example * ```bash * NUXT_APP_CDN_URL=https://mycdn.org/ node .output/server/index.mjs @@ -161,6 +167,24 @@ export default defineUntypedSchema({ * @type {typeof import('../src/types/config').NuxtAppConfig['keepalive']} */ keepalive: false, + /** + * Default value for `initialCache` configuration when fetching data. + * + * This can be overridden with `{ initialCache: true|false }` on a per-case + * basis. + * + * @type {typeof import('../src/types/config').NuxtAppConfig['initialCache']} + * @version 3 + * @example + * ```js + * export default { + * app: { + * initialCache: false + * } + * } + * ``` + */ + initialCache: true }, /** * The path to an HTML template file for rendering Nuxt responses. @@ -196,6 +220,7 @@ export default defineUntypedSchema({ * Enable or disable Vuex store. * * By default, it is enabled if there is a `store/` directory. + * * @version 2 */ store: { @@ -246,6 +271,7 @@ export default defineUntypedSchema({ /** * Configuration for the Nuxt `fetch()` hook. + * * @version 2 */ fetch: { @@ -284,6 +310,7 @@ export default defineUntypedSchema({ * You may want to extend plugins or change their order. For this, you can pass * a function using `extendPlugins`. It accepts an array of plugin objects and * should return an array of plugin objects. + * * @type {(plugins: Array<{ src: string, mode?: 'client' | 'server' }>) => Array<{ src: string, mode?: 'client' | 'server' }>} * @version 2 */ @@ -320,6 +347,7 @@ export default defineUntypedSchema({ * An object where each key name maps to a path to a layout .vue file. * * Normally, there is no need to configure this directly. + * * @type {Record} * @version 2 */ @@ -329,6 +357,7 @@ export default defineUntypedSchema({ * Set a custom error page layout. * * Normally, there is no need to configure this directly. + * * @type {string} * @version 2 */ @@ -338,6 +367,7 @@ export default defineUntypedSchema({ * Configure the Nuxt loading progress bar component that's shown between * routes. Set to `false` to disable. You can also customize it or create * your own component. + * * @version 2 */ loading: { @@ -375,6 +405,7 @@ export default defineUntypedSchema({ * configuration. The name can refer to an indicator from [SpinKit](https://tobiasahlin.com/spinkit/) * or a path to an HTML template of the indicator source code (in this case, all the * other options will be passed to the template). + * * @version 2 */ loadingIndicator: { @@ -426,7 +457,7 @@ export default defineUntypedSchema({ * @version 2 */ layoutTransition: { - $resolve: val => { + $resolve: (val) => { val = typeof val === 'string' ? { name: val } : val return defu(val, { name: 'layout', @@ -437,6 +468,7 @@ export default defineUntypedSchema({ /** * You can disable specific Nuxt features that you do not want. + * * @version 2 */ features: { From 885f0bcdce398b8b330794fd3da79b927a33ae7b Mon Sep 17 00:00:00 2001 From: Reinier Kaper Date: Sat, 24 Sep 2022 15:25:14 -0400 Subject: [PATCH 2/3] refactor: Use the proper runtimeConfig --- packages/nuxt/src/app/composables/asyncData.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index 172e00d05a6..b74e1e59108 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -1,6 +1,6 @@ import { onBeforeMount, onServerPrefetch, onUnmounted, ref, getCurrentInstance, watch, unref } from 'vue' import type { Ref, WatchSource } from 'vue' -import { NuxtApp, useNuxtApp } from '../nuxt' +import { NuxtApp, useNuxtApp, useRuntimeConfig } from '../nuxt' export type _Transform = (input: Input) => Output @@ -90,17 +90,20 @@ export function useAsyncData< options.server = options.server ?? true options.default = options.default ?? getDefault - // Setup nuxt instance payload - const nuxt = useNuxtApp() + const config = useRuntimeConfig() + console.log(config) // TODO: remove support for `defer` in Nuxt 3 RC if ((options as any).defer) { console.warn('[useAsyncData] `defer` has been renamed to `lazy`. Support for `defer` will be removed in RC.') } options.lazy = options.lazy ?? (options as any).defer ?? false - options.initialCache = options.initialCache ?? nuxt.ssrContext?.runtimeConfig.app.initialCache + options.initialCache = options.initialCache ?? config.app.initialCache options.immediate = options.immediate ?? true + // Setup nuxt instance payload + const nuxt = useNuxtApp() + const useInitialCache = () => (nuxt.isHydrating || options.initialCache) && nuxt.payload.data[key] !== undefined // Create or use a shared asyncData entity From c9a22bbfb45e0d668fcb90e126a556f00f5a3ddd Mon Sep 17 00:00:00 2001 From: Reinier Kaper Date: Sat, 24 Sep 2022 16:07:49 -0400 Subject: [PATCH 3/3] chore: :fire: remove console.log --- packages/nuxt/src/app/composables/asyncData.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/nuxt/src/app/composables/asyncData.ts b/packages/nuxt/src/app/composables/asyncData.ts index b74e1e59108..1a60856504d 100644 --- a/packages/nuxt/src/app/composables/asyncData.ts +++ b/packages/nuxt/src/app/composables/asyncData.ts @@ -91,7 +91,6 @@ export function useAsyncData< options.default = options.default ?? getDefault const config = useRuntimeConfig() - console.log(config) // TODO: remove support for `defer` in Nuxt 3 RC if ((options as any).defer) {