温馨提示:本文翻译自stackoverflow.com,查看原文请点击:express - Heroku hosted angularjs app, i cant make the browser cache s3 images even with cache-control
amazon-s3 angularjs cache-control caching express

express - Heroku托管的angularjs应用程序,即使使用缓存控制,我也无法使浏览器缓存s3图像

发布于 2020-08-17 03:02:42

我在尝试启用浏览器缓存s3图像时遇到问题。我在Heorku上部署了angularjs应用。我的应用使用存储在s3存储桶中的图像。因为它有很多图像,所以我希望浏览器请求文件,将其存储在缓存中,然后使用缓存,但是无论我做什么,我都无法使浏览器从缓存中请求文件。我看到它正在请求文件,然后重新加载页面,并且它再次获得了相同的文件。

我尝试在index.html中设置以下标题:

  <meta http-equiv="Cache-Control" content="public, max-age=604800" />
  <meta http-equiv="Pragma" content="public, max-age=604800" />

但是图像仍未缓存。

This is what i see in network panel in chrome:

**General**
RequestURL: https://localhost-mercurio.s3.amazonaws.com/1584160111310-m3r-piriapolis-letras1.jpeg
Request Method: GET
Status Code: 200 OK
Remote Address: 52.217.41.172:443
Referrer Policy: no-referrer-when-downgrade

**Response headers**
Accept-Ranges: bytes
Cache-Control: public, max-age=31536000
Content-Length: 80901
Content-Type: application/octet-stream
Date: Sat, 14 Mar 2020 04:46:38 GMT
ETag: "c5f451ebcd689f3aacee4144e80b54bf"
Last-Modified: Sat, 14 Mar 2020 04:28:34 GMT
Server: AmazonS3
x-amz-id-2: h+iya1nhpaqv/YMuqmpmVWse1JTt6HqdFzYul7SzR9BQ5wN/EyrE70VqT19j1EnmCsJKpYUllvk=
x-amz-request-id: CCEC70B9DD70C757

**Request headers**
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,es;q=0.8,it;q=0.7
Cache-Control: no-cache
Connection: keep-alive
Host: localhost-mercurio.s3.amazonaws.com
Pragma: no-cache
Referer: http://localhost:3000/
Sec-Fetch-Dest: image
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36

As you can see the response headers have Cache-Control: public, max-age=31536000, this is being set at the moment the image is uploaded in s3, so i imagine that by this s3 serves the file with the correct header.

The request header is where i see Cache-Control: no-cache, i think this may be reason why these images are not being cached, but im not sure and dont know how to change it, after all the docs i read i really thought the cache control had to be set on the s3 object and that would be it, is there anything im missing to make this work?

My server is a simple expressjs serving a static dist folder, the rest happens inside the angularJS app.

Any help with this would be greatly appreciated! this is something thats beyond me, too much server stuff going on that i find difficult to understand!

In case anyone asks, its angularJS and not Angular because its something i started when Angular was in beta, and once i had it working and some clients all this happened and i never got to migrate, but i plan to do it soon, i just need this to work a little longer!

Update: I tried with different ways to set the headers in express, but no matter what i do i always see the same header in the browser. I also tried to set the headers in the HTML code, thinking maaaaybe they are being read, and the nocache is making the images cache not work, but no luck there also.

可能是什么原因?这是s3问题,服务器问题,angularjs问题吗?我也没有找到方法来配置如何在angularjs中请求图像,我唯一的缓存控制是对发送到服务器的请求的控制,我用尽了所有尝试的选项,因为我不想我认为我需要花费更多的钱对于诸如cloudflare之类的东西,如果可以以我们所有人都已经想到的方式完成,那么任何帮助将非常感激!!!

查看更多

提问者
Toddy
被浏览
48
Toddy 2020-05-05 02:33

这可能也会对某人有所帮助,但是我所做的所有工作都可以正常进行,默认情况下是打开chrome dev工具时,选中“禁用缓存”选项,因此在测试以查看是否正在缓存内容时,会出现缓存头好的,但是该对象未缓存。您必须取消选中该选项,重新加载页面,然后才可以看到对象被缓存。