bod*_*ruk 13 javascript json caching firebase firebase-storage
我在Firebase上运行了一个PWA.我的图片文件托管在Firebase存储上.我注意到我的浏览器没有为从存储系统加载的文件保存缓存.浏览器为每个页面刷新请求文件.它会导致不必要的延迟和流量.
我的JS脚本从Firebase存储的下载链接加载文件,例如:https://firebasestorage.googleapis.com/v0/b/discipulado-7b14b.appspot.com/o/book3.png?val = media&token = 65b2cde7-c8a4 -45da-a743-401759663c17.
我可以缓存这些请求吗?
UPDATE
根据这些答案,我不应该使用Firebase存储来托管我的网站上的文件.只是为了管理用户的下载和上传.它是否正确?
cacheControl存储:https://firebase.google.com/docs/reference/js/firebase.storage.SettableMetadata#cacheControl
您可以更好地使用托管服务,使用firebase CLI进行部署非常简单.我认为默认情况下,Hosting中的图像上的Cache-Control是2小时,您可以使用.json全局增加它.
https://firebase.google.com/docs/hosting/full-config#headers
托管可以扩展您的网站并将其移动到更靠近需求的不同边缘节点.存储仅限于存储桶,但您可以为欧洲指定一个存储桶,为中国指定一个存储桶,为北美指定存储桶等.
存储对于用户文件上传更好,而托管用于静态内容(尽管他们正在推出具有云功能的动态托管我认为)
将这些部分放在一起以备将来参考
第一的,
// Create file metadata to update
var newMetadata = {
cacheControl: 'public,max-age=4000',
}
Run Code Online (Sandbox Code Playgroud)
然后,
storageRef.put(file, newMetadata)
Run Code Online (Sandbox Code Playgroud)
storageRef.updateMetadata(yourMetadata)可能是您需要的解决方案。
https://firebase.google.com/docs/storage/web/file-metadata#update_file_metadata
或者
storageRef.put(file, yourMetadata)
https://firebase.google.com/docs/storage/web/upload-files#upload_files