From cc5e01b35a0a5647bde457dc6a4ab86dd627fb04 Mon Sep 17 00:00:00 2001 From: harlan Date: Mon, 16 Sep 2024 14:40:17 +1000 Subject: [PATCH] fix: cache bundled scripts relative to nuxt root --- src/assets.ts | 16 ++++++++++------ src/plugins/transform.ts | 16 +++++++++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/assets.ts b/src/assets.ts index d96a3887..64b67dc4 100644 --- a/src/assets.ts +++ b/src/assets.ts @@ -24,16 +24,20 @@ const renderedScript = new Map { + const nuxt = useNuxt() + return createStorage({ + driver: fsDriver({ + base: resolve(nuxt.options.rootDir, 'node_modules/.cache/nuxt/scripts'), + }), + }) +} // TODO: replace this with nuxt/assets when it is released export function setupPublicAssetStrategy(options: ModuleOptions['assets'] = {}) { const assetsBaseURL = options.prefix || '/_scripts' const nuxt = useNuxt() + const storage = bundleStorage() // Register font proxy URL for development addDevServerHandler({ @@ -46,7 +50,7 @@ export function setupPublicAssetStrategy(options: ModuleOptions['assets'] = {}) if (!scriptDescriptor || scriptDescriptor instanceof Error) throw createError({ statusCode: 404 }) - const key = `data:scripts:${filename}` + const key = `bundle:${filename}` // Use storage to cache the font data between requests let res = await storage.getItemRaw(key) if (!res) { diff --git a/src/plugins/transform.ts b/src/plugins/transform.ts index a1661d54..902da22e 100644 --- a/src/plugins/transform.ts +++ b/src/plugins/transform.ts @@ -12,7 +12,7 @@ import { join } from 'pathe' import { colors } from 'consola/utils' import { tryUseNuxt, useNuxt } from '@nuxt/kit' import { logger } from '../logger' -import { storage } from '../assets' +import { bundleStorage } from '../assets' import { isJS, isVue } from './util' import type { RegistryScript } from '#nuxt-scripts' @@ -55,12 +55,13 @@ async function downloadScript(opts: { if (src === url || !filename) { return } + const storage = bundleStorage() const scriptContent = renderedScript.get(src) let res: Buffer | undefined = scriptContent instanceof Error ? undefined : scriptContent?.content if (!res) { // Use storage to cache the font data between builds - if (await storage.hasItem(`data:scripts:${filename}`)) { - const res = await storage.getItemRaw(`data:scripts:${filename}`) + if (await storage.hasItem(`bundle:${filename}`)) { + const res = await storage.getItemRaw(`bundle:${filename}`) renderedScript.set(url, { content: res!, size: res!.length / 1024, @@ -80,11 +81,12 @@ async function downloadScript(opts: { encoding = r.headers.get('content-encoding') const contentLength = r.headers.get('content-length') size = contentLength ? Number(contentLength) / 1024 : 0 - return r.arrayBuffer() }).then(r => Buffer.from(r)) - storage.setItemRaw(`data:scripts:${filename}`, res) + await storage.setItemRaw(`bundle:${filename}`, res) + size = size || res!.length / 1024 + logger.info(`Downloading script ${colors.gray(`${src} → ${filename} (${size.toFixed(2)} kB ${encoding})`)}`) renderedScript.set(url, { content: res!, size, @@ -110,7 +112,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti logger.debug('[bundle-script-transformer] No scripts to bundle...') return } - logger.info('[bundle-script-transformer] Bundling scripts...') + logger.debug('[bundle-script-transformer] Bundling scripts...') // less aggressive cache clearing in dev if (!nuxt.options.dev) { await fsp.rm(cacheDir, { recursive: true, force: true }) @@ -121,7 +123,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti if (content instanceof Error || !content.filename) return await fsp.writeFile(join(nuxt.options.buildDir, 'cache', 'scripts', content.filename), content.content) - logger.info(colors.gray(` ├─ ${url} → ${joinURL(content.src)} (${content.size.toFixed(2)} kB ${content.encoding})`)) + logger.debug(colors.gray(` ├─ ${url} → ${joinURL(content.src)} (${content.size.toFixed(2)} kB ${content.encoding})`)) })) })