diff --git a/docs/content/4.api/3.configuration.md b/docs/content/4.api/3.configuration.md index 930af0e53..295f13646 100644 --- a/docs/content/4.api/3.configuration.md +++ b/docs/content/4.api/3.configuration.md @@ -411,6 +411,13 @@ Toggles the document-driven mode. | `injectPage` | `boolean` | Inject `[...slug].vue` pre-configured page | | `trailingSlash` | `boolean` | Add a slash at the end of `canonical` and `og:url` | +## `useCache` + +- Type: `boolean` +- Default: `true` + +When `true`, the production server (`nuxt start`) will use cached version of the content (generated after running `nuxt build`) instead of parsing files. This improves app startup time, but makes app unaware of any content changes. + ## `respectPathCase` - Type: `boolean`{lang=ts} diff --git a/src/module.ts b/src/module.ts index 9cbcb9d08..cde969647 100644 --- a/src/module.ts +++ b/src/module.ts @@ -219,6 +219,11 @@ export interface ModuleOptions { injectPage?: boolean trailingSlash?: boolean }, + /** + * Disable to dynamically render content in production. + * @default true + */ + buildCache: boolean, /** * Enable to keep uppercase characters in the generated routes. * @@ -286,6 +291,7 @@ export default defineNuxtModule({ fields: [] }, documentDriven: false, + buildCache: true, respectPathCase: false, experimental: { clientDB: false, @@ -298,8 +304,9 @@ export default defineNuxtModule({ // Ensure default locale alway is the first item of locales options.locales = Array.from(new Set([options.defaultLocale, ...options.locales].filter(Boolean))) as string[] - // Disable cache in dev mode - const buildIntegrity = nuxt.options.dev ? undefined : Date.now() + // Cache content in production, when 'buildCache' is enabled. + const buildCache = Boolean(!nuxt.options.dev && options.buildCache) + const buildIntegrity = buildCache ? Date.now() : undefined if (options.base) { logger.warn('content.base is deprecated. Use content.api.baseURL instead.') @@ -323,11 +330,9 @@ export default defineNuxtModule({ nuxt.hook('nitro:config', (nitroConfig) => { // Init Nitro context - nitroConfig.prerender = nitroConfig.prerender || {} - nitroConfig.prerender.routes = nitroConfig.prerender.routes || [] - nitroConfig.handlers = nitroConfig.handlers || [] // Add server handlers + nitroConfig.handlers = nitroConfig.handlers || [] nitroConfig.handlers.push( { method: 'get', @@ -346,25 +351,21 @@ export default defineNuxtModule({ }, { method: 'get', - route: nuxt.options.dev - ? `${options.api.baseURL}/cache.json` - : `${options.api.baseURL}/cache.${buildIntegrity}.json`, + route: buildCache + ? `${options.api.baseURL}/cache.${buildIntegrity}.json` + : `${options.api.baseURL}/cache.json`, handler: resolveRuntimeModule('./server/api/cache') } ) - if (!nuxt.options.dev) { + if (buildCache) { + nitroConfig.prerender = nitroConfig.prerender || {} + nitroConfig.prerender.routes = nitroConfig.prerender.routes || [] nitroConfig.prerender.routes.unshift(`${options.api.baseURL}/cache.${buildIntegrity}.json`) } // Register source storages const sources = useContentMounts(nuxt, contentContext.sources) - nitroConfig.devStorage = Object.assign(nitroConfig.devStorage || {}, sources) - nitroConfig.devStorage['cache:content'] = { - driver: 'fs', - base: resolve(nuxt.options.buildDir, 'content-cache') - } - // Tell Nuxt to ignore content dir for app build for (const source of Object.values(sources)) { // Only targets directories inside the srcDir @@ -377,8 +378,21 @@ export default defineNuxtModule({ ) } } - nitroConfig.bundledStorage = nitroConfig.bundledStorage || [] - nitroConfig.bundledStorage.push('/cache/content') + + if (buildCache) { + nitroConfig.devStorage = Object.assign(nitroConfig.devStorage || {}, sources) + nitroConfig.devStorage['cache:content'] = { + driver: 'fs', + base: resolve(nuxt.options.buildDir, 'content-cache') + } + nitroConfig.bundledStorage = nitroConfig.bundledStorage || [] + nitroConfig.bundledStorage.push('/cache/content') + } else { + nitroConfig.storage = Object.assign(nitroConfig.storage || {}, sources) + nitroConfig.storage['cache:content'] = { + driver: 'memory' + } + } // @ts-ignore nitroConfig.externals = defu(typeof nitroConfig.externals === 'object' ? nitroConfig.externals : {}, { @@ -652,8 +666,8 @@ export default defineNuxtModule({ // ignore files const isIgnored = makeIgnored(contentContext.ignores) - // Setup content dev module - if (!nuxt.options.dev) { + // Prepare cache for build + if (buildCache) { nuxt.hook('build:before', async () => { const storage = createStorage() const sources = useContentMounts(nuxt, contentContext.sources)