Skip to content

Commit cc5e01b

Browse files
committed
fix: cache bundled scripts relative to nuxt root
1 parent 582d360 commit cc5e01b

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/assets.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ const renderedScript = new Map<string, {
2424
const ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365
2525

2626
// TODO: refactor to use nitro storage when it can be cached between builds
27-
export const storage = createStorage({
28-
driver: fsDriver({
29-
base: 'node_modules/.cache/nuxt/scripts',
30-
}),
31-
})
27+
export const bundleStorage = () => {
28+
const nuxt = useNuxt()
29+
return createStorage({
30+
driver: fsDriver({
31+
base: resolve(nuxt.options.rootDir, 'node_modules/.cache/nuxt/scripts'),
32+
}),
33+
})
34+
}
3235

3336
// TODO: replace this with nuxt/assets when it is released
3437
export function setupPublicAssetStrategy(options: ModuleOptions['assets'] = {}) {
3538
const assetsBaseURL = options.prefix || '/_scripts'
3639
const nuxt = useNuxt()
40+
const storage = bundleStorage()
3741

3842
// Register font proxy URL for development
3943
addDevServerHandler({
@@ -46,7 +50,7 @@ export function setupPublicAssetStrategy(options: ModuleOptions['assets'] = {})
4650
if (!scriptDescriptor || scriptDescriptor instanceof Error)
4751
throw createError({ statusCode: 404 })
4852

49-
const key = `data:scripts:${filename}`
53+
const key = `bundle:${filename}`
5054
// Use storage to cache the font data between requests
5155
let res = await storage.getItemRaw(key)
5256
if (!res) {

src/plugins/transform.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { join } from 'pathe'
1212
import { colors } from 'consola/utils'
1313
import { tryUseNuxt, useNuxt } from '@nuxt/kit'
1414
import { logger } from '../logger'
15-
import { storage } from '../assets'
15+
import { bundleStorage } from '../assets'
1616
import { isJS, isVue } from './util'
1717
import type { RegistryScript } from '#nuxt-scripts'
1818

@@ -55,12 +55,13 @@ async function downloadScript(opts: {
5555
if (src === url || !filename) {
5656
return
5757
}
58+
const storage = bundleStorage()
5859
const scriptContent = renderedScript.get(src)
5960
let res: Buffer | undefined = scriptContent instanceof Error ? undefined : scriptContent?.content
6061
if (!res) {
6162
// Use storage to cache the font data between builds
62-
if (await storage.hasItem(`data:scripts:${filename}`)) {
63-
const res = await storage.getItemRaw<Buffer>(`data:scripts:${filename}`)
63+
if (await storage.hasItem(`bundle:${filename}`)) {
64+
const res = await storage.getItemRaw<Buffer>(`bundle:${filename}`)
6465
renderedScript.set(url, {
6566
content: res!,
6667
size: res!.length / 1024,
@@ -80,11 +81,12 @@ async function downloadScript(opts: {
8081
encoding = r.headers.get('content-encoding')
8182
const contentLength = r.headers.get('content-length')
8283
size = contentLength ? Number(contentLength) / 1024 : 0
83-
8484
return r.arrayBuffer()
8585
}).then(r => Buffer.from(r))
8686

87-
storage.setItemRaw(`data:scripts:${filename}`, res)
87+
await storage.setItemRaw(`bundle:${filename}`, res)
88+
size = size || res!.length / 1024
89+
logger.info(`Downloading script ${colors.gray(`${src}${filename} (${size.toFixed(2)} kB ${encoding})`)}`)
8890
renderedScript.set(url, {
8991
content: res!,
9092
size,
@@ -110,7 +112,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
110112
logger.debug('[bundle-script-transformer] No scripts to bundle...')
111113
return
112114
}
113-
logger.info('[bundle-script-transformer] Bundling scripts...')
115+
logger.debug('[bundle-script-transformer] Bundling scripts...')
114116
// less aggressive cache clearing in dev
115117
if (!nuxt.options.dev) {
116118
await fsp.rm(cacheDir, { recursive: true, force: true })
@@ -121,7 +123,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
121123
if (content instanceof Error || !content.filename)
122124
return
123125
await fsp.writeFile(join(nuxt.options.buildDir, 'cache', 'scripts', content.filename), content.content)
124-
logger.info(colors.gray(` ├─ ${url}${joinURL(content.src)} (${content.size.toFixed(2)} kB ${content.encoding})`))
126+
logger.debug(colors.gray(` ├─ ${url}${joinURL(content.src)} (${content.size.toFixed(2)} kB ${content.encoding})`))
125127
}))
126128
})
127129

0 commit comments

Comments
 (0)