@@ -183,17 +183,25 @@ export function usePackage(
183183) {
184184 const cachedFetch = useCachedFetch ( )
185185
186- return useLazyAsyncData (
186+ const asyncData = useLazyAsyncData (
187187 ( ) => `package:${ toValue ( name ) } :${ toValue ( requestedVersion ) ?? '' } ` ,
188188 async ( ) => {
189189 const encodedName = encodePackageName ( toValue ( name ) )
190- const r = await cachedFetch < Packument > ( `${ NPM_REGISTRY } /${ encodedName } ` )
190+ const { data : r , isStale } = await cachedFetch < Packument > ( `${ NPM_REGISTRY } /${ encodedName } ` )
191191 const reqVer = toValue ( requestedVersion )
192192 const pkg = transformPackument ( r , reqVer )
193193 const resolvedVersion = getResolvedVersion ( pkg , reqVer )
194- return { ...pkg , resolvedVersion }
194+ return { ...pkg , resolvedVersion, isStale }
195195 } ,
196196 )
197+
198+ if ( import . meta. client && asyncData . data . value ?. isStale ) {
199+ onMounted ( ( ) => {
200+ asyncData . refresh ( )
201+ } )
202+ }
203+
204+ return asyncData
197205}
198206
199207function getResolvedVersion ( pkg : SlimPackument , reqVer ?: string | null ) : string | null {
@@ -223,15 +231,24 @@ export function usePackageDownloads(
223231) {
224232 const cachedFetch = useCachedFetch ( )
225233
226- return useLazyAsyncData (
234+ const asyncData = useLazyAsyncData (
227235 ( ) => `downloads:${ toValue ( name ) } :${ toValue ( period ) } ` ,
228236 async ( ) => {
229237 const encodedName = encodePackageName ( toValue ( name ) )
230- return await cachedFetch < NpmDownloadCount > (
238+ const { data , isStale } = await cachedFetch < NpmDownloadCount > (
231239 `${ NPM_API } /downloads/point/${ toValue ( period ) } /${ encodedName } ` ,
232240 )
241+ return { ...data , isStale }
233242 } ,
234243 )
244+
245+ if ( import . meta. client && asyncData . data . value ?. isStale ) {
246+ onMounted ( ( ) => {
247+ asyncData . refresh ( )
248+ } )
249+ }
250+
251+ return asyncData
235252}
236253
237254type NpmDownloadsRangeResponse = {
@@ -260,6 +277,7 @@ export async function fetchNpmDownloadsRange(
260277const emptySearchResponse = {
261278 objects : [ ] ,
262279 total : 0 ,
280+ isStale : false ,
263281 time : new Date ( ) . toISOString ( ) ,
264282} satisfies NpmSearchResponse
265283
@@ -305,7 +323,7 @@ export function useNpmSearch(
305323 // Use requested size for initial fetch
306324 params . set ( 'size' , String ( opts . size ?? 25 ) )
307325
308- const response = await cachedFetch < NpmSearchResponse > (
326+ const { data : response , isStale } = await cachedFetch < NpmSearchResponse > (
309327 `${ NPM_REGISTRY } /-/v1/search?${ params . toString ( ) } ` ,
310328 { } ,
311329 60 ,
@@ -317,7 +335,7 @@ export function useNpmSearch(
317335 total : response . total ,
318336 }
319337
320- return response
338+ return { ... response , isStale }
321339 } ,
322340 { default : ( ) => lastSearch || emptySearchResponse } ,
323341 )
@@ -357,7 +375,7 @@ export function useNpmSearch(
357375 params . set ( 'size' , String ( size ) )
358376 params . set ( 'from' , String ( from ) )
359377
360- const response = await cachedFetch < NpmSearchResponse > (
378+ const { data : response } = await cachedFetch < NpmSearchResponse > (
361379 `${ NPM_REGISTRY } /-/v1/search?${ params . toString ( ) } ` ,
362380 { } ,
363381 60 ,
@@ -405,6 +423,7 @@ export function useNpmSearch(
405423 const data = computed < NpmSearchResponse | null > ( ( ) => {
406424 if ( cache . value ) {
407425 return {
426+ isStale : false ,
408427 objects : cache . value . objects ,
409428 total : cache . value . total ,
410429 time : new Date ( ) . toISOString ( ) ,
@@ -413,6 +432,12 @@ export function useNpmSearch(
413432 return asyncData . data . value
414433 } )
415434
435+ if ( import . meta. client && asyncData . data . value ?. isStale ) {
436+ onMounted ( ( ) => {
437+ asyncData . refresh ( )
438+ } )
439+ }
440+
416441 // Whether there are more results available on the server (incremental mode only)
417442 const hasMore = computed ( ( ) => {
418443 if ( ! cache . value ) return true
@@ -482,7 +507,7 @@ function packumentToSearchResult(pkg: MinimalPackument, weeklyDownloads?: number
482507export function useOrgPackages ( orgName : MaybeRefOrGetter < string > ) {
483508 const cachedFetch = useCachedFetch ( )
484509
485- return useLazyAsyncData (
510+ const asyncData = useLazyAsyncData (
486511 ( ) => `org-packages:${ toValue ( orgName ) } ` ,
487512 async ( ) => {
488513 const org = toValue ( orgName )
@@ -493,7 +518,7 @@ export function useOrgPackages(orgName: MaybeRefOrGetter<string>) {
493518 // Get all package names in the org
494519 let packageNames : string [ ]
495520 try {
496- const data = await cachedFetch < Record < string , string > > (
521+ const { data } = await cachedFetch < Record < string , string > > (
497522 `${ NPM_REGISTRY } /-/org/${ encodeURIComponent ( org ) } /package` ,
498523 )
499524 packageNames = Object . keys ( data )
@@ -526,7 +551,10 @@ export function useOrgPackages(orgName: MaybeRefOrGetter<string>) {
526551 batch . map ( async name => {
527552 try {
528553 const encoded = encodePackageName ( name )
529- return await cachedFetch < MinimalPackument > ( `${ NPM_REGISTRY } /${ encoded } ` )
554+ const { data : pkg } = await cachedFetch < MinimalPackument > (
555+ `${ NPM_REGISTRY } /${ encoded } ` ,
556+ )
557+ return pkg
530558 } catch {
531559 return null
532560 }
@@ -551,13 +579,16 @@ export function useOrgPackages(orgName: MaybeRefOrGetter<string>) {
551579 )
552580
553581 return {
582+ isStale : false ,
554583 objects : results ,
555584 total : results . length ,
556585 time : new Date ( ) . toISOString ( ) ,
557586 } satisfies NpmSearchResponse
558587 } ,
559588 { default : ( ) => emptySearchResponse } ,
560589 )
590+
591+ return asyncData
561592}
562593
563594// ============================================================================
@@ -665,9 +696,9 @@ async function checkDependencyOutdated(
665696 if ( cached ) {
666697 packument = await cached
667698 } else {
668- const promise = cachedFetch < Packument > (
669- ` ${ NPM_REGISTRY } / ${ encodePackageName ( packageName ) } ` ,
670- ) . catch ( ( ) => null )
699+ const promise = cachedFetch < Packument > ( ` ${ NPM_REGISTRY } / ${ encodePackageName ( packageName ) } ` )
700+ . then ( ( { data } ) => data )
701+ . catch ( ( ) => null )
671702 packumentCache . set ( packageName , promise )
672703 packument = await promise
673704 }
0 commit comments