@@ -11,6 +11,7 @@ import {
1111 UploadTaskSnapshot ,
1212 StorageError ,
1313 SettableMetadata ,
14+ FullMetadata ,
1415} from 'firebase/storage'
1516import {
1617 computed ,
@@ -26,6 +27,8 @@ import {
2627} from 'vue'
2728import { useFirebaseApp } from '../app'
2829import { noop , _MaybeRef , _Nullable } from '../shared'
30+ import { getInitialValue } from '../ssr/initialState'
31+ import { addPendingPromise } from '../ssr/plugin'
2932
3033/**
3134 * Retrieves the Storage instance.
@@ -45,8 +48,15 @@ export function useStorage(name?: string) {
4548export function useStorageUrl (
4649 storageRef : _MaybeRef < _Nullable < StorageReference > >
4750) {
51+ const initialSourceValue = unref ( storageRef )
4852 const url = ref < string | null > ( )
53+ url . value = getInitialValue (
54+ initialSourceValue ,
55+ undefined ,
56+ url . value
57+ ) as string
4958 const promise = ref < Promise < string | null > > ( Promise . resolve ( null ) )
59+ let removePendingPromise = noop
5060
5161 function refresh ( ) {
5262 const storageSource = unref ( storageRef )
@@ -65,9 +75,15 @@ export function useStorageUrl(
6575 watch ( storageRef , refresh )
6676 }
6777
78+ // SSR
79+ if ( initialSourceValue ) {
80+ removePendingPromise = addPendingPromise ( promise . value , initialSourceValue )
81+ }
82+
83+ if ( getCurrentScope ( ) ) {
84+ onScopeDispose ( removePendingPromise )
85+ }
6886 if ( getCurrentInstance ( ) ) {
69- // TODO: rework API to allow adding with a custom group key and key
70- // addPendingPromise(promise)
7187 onServerPrefetch ( ( ) => promise . value )
7288 }
7389
@@ -83,11 +99,20 @@ export function useStorageUrl(
8399export function useStorageMetadata (
84100 storageRef : _MaybeRef < _Nullable < StorageReference > >
85101) {
86- // TODO: retrieve global data from local store
87- const metadata = shallowRef < UploadMetadata | null > ( )
88- const promise = shallowRef < Promise < UploadMetadata | null > > (
102+ const initialSourceValue = unref ( storageRef )
103+ const metadata = shallowRef < FullMetadata | null > ( )
104+ if ( initialSourceValue ) {
105+ metadata . value = getInitialValue (
106+ initialSourceValue ,
107+ // 'm ' is a prefix to differentiate from urls since both are stored in the same object
108+ 'm ' + initialSourceValue . toString ( ) ,
109+ metadata . value
110+ ) as FullMetadata
111+ }
112+ const promise = shallowRef < Promise < FullMetadata | null > > (
89113 Promise . resolve ( null )
90114 )
115+ let removePendingPromise = noop
91116
92117 function refresh ( ) {
93118 const storageSource = unref ( storageRef )
@@ -120,9 +145,15 @@ export function useStorageMetadata(
120145 watch ( storageRef , refresh )
121146 }
122147
148+ // SSR
149+ if ( initialSourceValue ) {
150+ removePendingPromise = addPendingPromise ( promise . value , initialSourceValue )
151+ }
152+
153+ if ( getCurrentScope ( ) ) {
154+ onScopeDispose ( removePendingPromise )
155+ }
123156 if ( getCurrentInstance ( ) ) {
124- // TODO: rework API to allow adding with a custom group key and key
125- // addPendingPromise(promise)
126157 onServerPrefetch ( ( ) => promise . value )
127158 }
128159
0 commit comments