@@ -20,13 +20,13 @@ import type { RegistryScript } from '#nuxt-scripts/types'
2020
2121const SEVEN_DAYS_IN_MS = 7 * 24 * 60 * 60 * 1000
2222
23- async function isCacheExpired ( storage : any , filename : string ) : Promise < boolean > {
23+ async function isCacheExpired ( storage : any , filename : string , cacheMaxAge : number = SEVEN_DAYS_IN_MS ) : Promise < boolean > {
2424 const metaKey = `bundle-meta:${ filename } `
2525 const meta = await storage . getItem ( metaKey )
2626 if ( ! meta || ! meta . timestamp ) {
2727 return true // No metadata means expired/invalid cache
2828 }
29- return Date . now ( ) - meta . timestamp > SEVEN_DAYS_IN_MS
29+ return Date . now ( ) - meta . timestamp > cacheMaxAge
3030}
3131
3232export interface AssetBundlerTransformerOptions {
@@ -36,6 +36,7 @@ export interface AssetBundlerTransformerOptions {
3636 scripts ?: Required < RegistryScript > [ ]
3737 fallbackOnSrcOnBundleFail ?: boolean
3838 fetchOptions ?: FetchOptions
39+ cacheMaxAge ?: number
3940 renderedScript ?: Map < string , {
4041 content : Buffer
4142 /**
@@ -68,7 +69,7 @@ async function downloadScript(opts: {
6869 url : string
6970 filename ?: string
7071 forceDownload ?: boolean
71- } , renderedScript : NonNullable < AssetBundlerTransformerOptions [ 'renderedScript' ] > , fetchOptions ?: FetchOptions ) {
72+ } , renderedScript : NonNullable < AssetBundlerTransformerOptions [ 'renderedScript' ] > , fetchOptions ?: FetchOptions , cacheMaxAge ?: number ) {
7273 const { src, url, filename, forceDownload } = opts
7374 if ( src === url || ! filename ) {
7475 return
@@ -79,7 +80,7 @@ async function downloadScript(opts: {
7980 if ( ! res ) {
8081 // Use storage to cache the font data between builds
8182 const cacheKey = `bundle:${ filename } `
82- const shouldUseCache = ! forceDownload && await storage . hasItem ( cacheKey ) && ! ( await isCacheExpired ( storage , filename ) )
83+ const shouldUseCache = ! forceDownload && await storage . hasItem ( cacheKey ) && ! ( await isCacheExpired ( storage , filename , cacheMaxAge ) )
8384
8485 if ( shouldUseCache ) {
8586 const res = await storage . getItemRaw < Buffer > ( cacheKey )
@@ -312,7 +313,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
312313 const { url : _url , filename } = normalizeScriptData ( src , options . assetsBaseURL )
313314 let url = _url
314315 try {
315- await downloadScript ( { src, url, filename, forceDownload } , renderedScript , options . fetchOptions )
316+ await downloadScript ( { src, url, filename, forceDownload } , renderedScript , options . fetchOptions , options . cacheMaxAge )
316317 }
317318 catch ( e ) {
318319 if ( options . fallbackOnSrcOnBundleFail ) {
0 commit comments