简体   繁体   English

将 Angular 应用作为 Express Server 中的静态内容提供服务

[英]Serving Angular app as a static content in Express Server

I am serving Angular app as a static content in Express Server.我将 Angular 应用程序作为 Express Server 中的静态内容提供服务。 When serving static files with Express, Express by default adds ETag to the files.使用 Express 提供静态文件时,默认情况下 Express 会向文件中添加ETag So, each next request will first check if ETag is matched and if it is, it will not send files again.因此,每个下一个请求将首先检查ETag是否匹配,如果匹配,则不会再次发送文件。 I know that Service Worker works similar and it tries to match the hash .我知道 Service Worker 的工作方式类似,它尝试匹配hash Does anyone know what is the main difference between these two approaches (caching with ETag and caching with Service Workers), and when we should use one over the other?有谁知道这两种方法(使用ETag缓存和使用 Service Workers 缓存)之间的主要区别是什么,以及我们什么时候应该使用一种方法而不是另一种方法? What would be the most efficient when it comes to performance:就性能而言,什么是最有效的:

  1. Server side caching and serving Angular app static files服务器端缓存和提供 Angular 应用程序静态文件
  2. Implementing Angular Service Worker for caching实现用于缓存的 Angular Service Worker
  3. Do both 1 and 2做 1 和 2

To give a better perspective, I'll address a third cache option as well, to clarify the differences.为了提供更好的视角,我还将介绍第三个缓存选项,以澄清差异。

Types of caching缓存类型

Basically we have 3 possible layers of caching, based on the priority they are checked from the client:基本上我们有 3 个可能的缓存层,基于从客户端检查它们的优先级:

  1. Service Worker cache (client-side) Service Worker 缓存(客户端)
  2. Browser Cache, also known as HTTP cache (client-side)浏览器缓存,也称为 HTTP 缓存(客户端)
  3. Server side cache (CDN)服务器端缓存 (CDN)

PS: Some browser like Chrome have an extra memory cache layer in front of the service worker cache. PS:某些浏览器(如 Chrome)在 Service Worker 缓存前面有一个额外的内存缓存层。

Characteristics / differences特点/差异

The service worker is the most reliable from the client-side ones, since it defines its own rules over how to manage the caching, and provide extra capabilities and fine-grained control over exactly what is cached and how caching is done. Service Worker 是客户端中最可靠的,因为它定义了自己的规则来管理缓存,并提供额外的功能和对缓存内容和缓存如何完成的细粒度控制。

The Browser caching is defined based on some HTTP headers from the assets response ( Cache-Control and Expires ), but the main issue is that there are many conditions in which those are ignored.浏览器缓存是基于资产响应( Cache-ControlExpires )中的一些 HTTP 标头定义的,但主要问题是有许多条件会忽略这些标头。 For instance, I've heard that for files bigger than 25Mb, normally they are not cached, specially on mobile, where the memory is limited (I believe it's getting even more strict lately, due to the increase in mobile usage).例如,我听说对于大于 25Mb 的文件,通常它们不会被缓存,特别是在内存有限的移动设备上(我相信最近变得更加严格,由于移动设备使用量的增加)。

So between those 2 options, I'd always chose the Service Worker cache for more reliability.所以在这两个选项之间,我总是选择 Service Worker 缓存以获得更高的可靠性。

Now, talking to the 3rd option, the CDN checks the HTTP headers to look for ETag for busting the cache.现在,谈到第三个选项,CDN 检查 HTTP 标头以查找用于破坏缓存的 ETag。 The idea of the Server-side caching is to only call the origin server in case the asset is not found on the CDN.服务器端缓存的想法是仅在 CDN 上找不到资产的情况下才调用源服务器。

Now, between 1st and 3rd, the main difference is that Service Workers works best for Slow / failing network connections and offline, since the cache is done client-side, so if the network is off, then the service worker retrieves the last cached information, allowing for a smooth user experience.现在,在第 1 个和第 3 个之间,主要区别在于 Service Workers 最适合慢速/失败的网络连接和离线,因为缓存是在客户端完成的,所以如果网络关闭,那么 Service Workers 会检索最后缓存的信息,提供流畅的用户体验。 Server-side, on the other hand, only works when we are able to reach the server, but at the same time, the caching happens out of user's device, saving local space, and reducing the application memory consumption.另一方面,服务器端仅在我们能够访问服务器时才工作,但同时,缓存发生在用户设备之外,节省了本地空间,并减少了应用程序内存消耗。

So as you see, there's no right / wrong answers, just what works best for your use case.如您所见,没有正确/错误的答案,只有最适合您的用例的答案。

Some Sources一些来源

Let's answer your questions:让我们回答您的问题:

what is the main difference between these two approaches (caching with ETag and caching with Service Workers)这两种方法的主要区别是什么(使用 ETag 缓存和使用 Service Workers 缓存)

Both solutions cache files, the main difference is the need to reach the server or stay locally:两种解决方案都缓存文件,主要区别在于需要到达服务器还是留在本地:

  • For the ETag, the browser hits the server asking for a file with a hash (the etag), depending on the file stored in the server, the server will answer with a " the file was not modified, use your local copy " with a 300 HTTP response or " here is a new version of that file " with a 200 HTTP response and a new file.对于 ETag,浏览器向服务器请求一个带有哈希值的文件(etag),根据存储在服务器中的文件,服务器将回答“文件未被修改,使用您的本地副本” 300 HTTP 响应或“这是该文件的新版本”,带有 200 HTTP 响应和一个新文件。 In both cases the server always decides.在这两种情况下,服务器总是决定。 and the user will wait for a round trip.并且用户将等待往返。
  • With the Service worker approach you can decide locally what to do.使用 Service Worker 方法,您可以在本地决定要做什么。 You can write some logic to control what/when to use a local copy (cached) or when go to the server.您可以编写一些逻辑来控制什么/何时使用本地副本(缓存)或何时转到服务器。 This is very useful for offline capabilities since the logic is happening in the client , and there is no need to hit the server.这对于离线功能非常有用,因为逻辑发生在客户端,不需要访问服务器。

when we should use one over the other?我们什么时候应该使用一个?

You can use both together.您可以同时使用两者。 You can define some logic in the service worker, if there is no connection return the local copies, otherwise go to the server.您可以在 service worker 中定义一些逻辑,如果没有连接返回本地副本,否则转到服务器。

What would be the most efficient when it comes to performance:就性能而言,什么是最有效的:

  • Server side caching and serving Angular app static files服务器端缓存和提供 Angular 应用程序静态文件
  • Implementing Angular Service Worker for caching实现用于缓存的 Angular Service Worker
  • Do both 1 and 2做 1 和 2

My recommended approach is use both approaches.我推荐的方法是同时使用这两种方法。 Although treat your files differently, the ' index.html ' file can change, in this case use the service worker (in case there is no internet access) and if there is internet access let the web server answer with the etag.尽管以不同的方式对待您的文件,“ index.html ”文件可以更改,在这种情况下,请使用服务工作者(如果没有互联网访问),如果有互联网访问,让网络服务器用 etag 回答。 All the other static files (CSS and JS) should be immutable files, this is you can be sure the local copy is valid, in this case add a hash to the files' name (so they are always unique files) and cache them.所有其他静态文件(CSS 和 JS)都应该是不可变文件,这是您可以确定本地副本是有效的,在这种情况下,向文件名添加哈希(因此它们始终是唯一文件)并缓存它们。 When you have a new version of your app, you will modify the ' index.html ' pointing to new immutable files.当您有新版本的应用程序时,您将修改指向新的不可变文件的“ index.html ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM