Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support useCache option. #2212

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/content/4.api/3.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
52 changes: 33 additions & 19 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -286,6 +291,7 @@ export default defineNuxtModule<ModuleOptions>({
fields: []
},
documentDriven: false,
buildCache: true,
respectPathCase: false,
experimental: {
clientDB: false,
Expand All @@ -298,8 +304,9 @@ export default defineNuxtModule<ModuleOptions>({
// 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.')
Expand All @@ -323,11 +330,9 @@ export default defineNuxtModule<ModuleOptions>({

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',
Expand All @@ -346,25 +351,21 @@ export default defineNuxtModule<ModuleOptions>({
},
{
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
Expand All @@ -377,8 +378,21 @@ export default defineNuxtModule<ModuleOptions>({
)
}
}
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 : {}, {
Expand Down Expand Up @@ -652,8 +666,8 @@ export default defineNuxtModule<ModuleOptions>({
// 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)
Expand Down