Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

ruby-on-rails - Rails ActiveStorage:如何避免每个映像进行一次重定向?(Rails ActiveStorage: how to avoid one redirect for each image?)

If you use ActiveStorage and you have a page with N images you get N additional requests to your Rails app (ie N redirects).

(如果您使用ActiveStorage,并且您的页面中包含N张图片,那么您会向Rails应用程序收到N个其他请求(即N个重定向)。)

That means wasting a lot of server resources if you have tens of images on a page.

(如果页面上有几十个图像,这意味着浪费大量服务器资源。)

I know that the redirect is useful for signed URLs.

(我知道重定向对于签名URL很有用。)

However I wonder why Rails does not precompute the final signed URL and embed that into the HTML page... In this way we could keep the advantages of signed URLs / protected files, without making N additional calls to the Rails server.

(但是,我想知道为什么Rails不会预先计算最终的签名URL并将其嵌入到HTML页面中...这样,我们可以保留签名URL /受保护文件的优点,而无需对Rails服务器进行N次额外的调用。)

Is it possible to include the final URL / pre-signed URL of image variants directly in the HTML (thus avoiding the redirect)?

(是否可以在HTML中直接包含图像变体的最终URL /预签名URL(从而避免重定向)?)

Otherwise, why is that impossible?

(否则,那为什么不可能呢?)

  ask by collimarco translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the service_url to create direct links to your resources.

(您可以使用service_url创建指向资源的直接链接。)

We don't use Rails views in our project so my knowledge about the view layer is rusty.

(我们不在项目中使用Rails视图,因此我对视图层的了解是生锈的。)

I think you could put it in a dedicated helper and then use it from your views.

(我认为您可以将其放在专用的帮助器中,然后从您的角度使用它。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...