Configure Tomcat to use browser caching?

I launched Google Page Speed in our web application to analyze and optimize our website.

One of the many points in β€œWeb Performance Efficiency,” as stated in β€œPage Speed,” says: β€œTo take full advantage of caching successively across all browsers, we recommend that you configure your web server to explicitly set caching headers and apply them to all cached static resources, not just a small subset (for example, images). Cache resources include JS and CSS files, image files and other binary files, object files (media files, PDF files, flash files, etc.). In general, HTML failed It is static and should not be considered cached.

How do I configure tomcat to achieve the same? I know that this can be done using filters by putting some HTTP headers, but can we do this without touching the code only with the configuration?

Edit: just for information, we use JSF 1.2, although I think it doesn't matter in the context of this question.

+6
source share
1 answer

If you use Tomcat7, there is a built-in filter for this. http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter

We use the wonderful URlRewriteFilter for this. No code changes, just setup on web.xml, that's it. Link and rule below.

http://tuckey.org/urlrewrite/

<rule> <from>^.*\.(js|css|gif)$</from> <set type="expires">6 hours</set> </rule> 
+9
source

Source: https://habr.com/ru/post/919025/


All Articles