Skip to content

Commit

Permalink
perf(nuxt): use fallthrough cache for prerender (#26104)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 6, 2024
1 parent 2e8d47c commit bc44dfc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/nuxt/package.json
Expand Up @@ -109,6 +109,7 @@
"unimport": "^3.7.1",
"unplugin": "^1.8.2",
"unplugin-vue-router": "^0.7.0",
"unstorage": "^1.10.1",
"untyped": "^1.4.2",
"vue": "^3.4.21",
"vue-bundle-renderer": "^2.0.0",
Expand Down
14 changes: 8 additions & 6 deletions packages/nuxt/src/core/nitro.ts
@@ -1,3 +1,4 @@
import { pathToFileURL } from 'node:url'
import { existsSync, promises as fsp, readFileSync } from 'node:fs'
import { cpus } from 'node:os'
import { join, normalize, relative, resolve } from 'pathe'
Expand All @@ -6,7 +7,7 @@ import { randomUUID } from 'uncrypto'
import { joinURL, withTrailingSlash } from 'ufo'
import { build, copyPublicAssets, createDevServer, createNitro, prepare, prerender, scanHandlers, writeTypes } from 'nitropack'
import type { Nitro, NitroConfig } from 'nitropack'
import { findPath, logger, resolveIgnorePatterns, resolveNuxtModule } from '@nuxt/kit'
import { findPath, logger, resolveIgnorePatterns, resolveNuxtModule, resolvePath } from '@nuxt/kit'
import escapeRE from 'escape-string-regexp'
import { defu } from 'defu'
import fsExtra from 'fs-extra'
Expand Down Expand Up @@ -392,11 +393,12 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
}
})

// Set prerender-only options
nitro.options._config.storage ||= {}
nitro.options._config.storage['internal:nuxt:prerender'] = { driver: 'memory' }
nitro.options._config.storage['internal:nuxt:prerender:island'] = { driver: 'lruCache', max: 1000 }
nitro.options._config.storage['internal:nuxt:prerender:payload'] = { driver: 'lruCache', max: 1000 }
nitro.options._config.storage = defu(nitro.options._config.storage, {
'internal:nuxt:prerender': {
driver: pathToFileURL(await resolvePath(join(distDir, 'core/runtime/nitro/cache-driver'))).href,
base: resolve(nuxt.options.buildDir, 'cache/nitro/prerender')
}
})

// Expose nitro to modules and kit
nuxt._nitro = nitro
Expand Down
27 changes: 27 additions & 0 deletions packages/nuxt/src/core/runtime/nitro/cache-driver.ts
@@ -0,0 +1,27 @@
import { defineDriver } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs-lite'
import lruCache from 'unstorage/drivers/lru-cache'

// Ensure we don't try to write/read from directory index for `/` paths
const normalizeFsKey = (item: string) => item.indexOf(':') === -1 ? `${item}:index` : item

export default defineDriver((opts: { base: string }) => {
const fs = fsDriver({ base: opts.base })
const lru = lruCache({ max: 1000 })

return {
...fs, // fall back to file system - only the bottom three methods are used in renderer
async setItem (key, value, opts) {
await Promise.all([
fs.setItem(normalizeFsKey(key), value, opts),
lru.setItem(key, value, opts)
])
},
async hasItem (key, opts) {
return await lru.hasItem(key, opts) || await fs.hasItem(normalizeFsKey(key), opts)
},
async getItem (key, opts) {
return await lru.getItem(key, opts) || await fs.getItem(normalizeFsKey(key), opts)
},
}
})
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bc44dfc

Please sign in to comment.