You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Hippo CMS中实现浏览器级图片缓存及本地配置方法

Great question! Browser caching is a critical optimization for speeding up load times—especially for image-heavy sites built with Hippo CMS. Let’s walk through exactly how to set this up, including step-by-step actions for your local development environment.

How to Configure Browser-Level Caching (Focused on Images) in Hippo CMS

1. Understand Hippo’s Resource Flow First

Hippo serves images from two main locations by default:

  • /content/gallery/: For images stored in Hippo’s built-in document gallery (including auto-generated variants like thumbnails or resized versions)
  • /site/resources/: For static images bundled in your project’s site module

By default, Hippo doesn’t set aggressive caching headers for these resources, so we’ll need to add filters and tweak configurations to fix that.

2. Local Environment Implementation Steps (Tomcat-Based)

Most local Hippo environments use an embedded Tomcat server, so we’ll leverage Tomcat’s built-in tools and Hippo’s configuration files:

2.1 Add a Cache Control Filter to web.xml

Open your project’s src/main/webapp/WEB-INF/web.xml file and add a filter to set expiration headers for images. We’ll use Tomcat’s ExpiresFilter (no extra dependencies needed for Tomcat 7+):

<filter>
    <filter-name>StaticResourceCacheFilter</filter-name>
    <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
    <!-- Set 30-day cache for common image types -->
    <init-param>
        <param-name>ExpiresByType image</param-name>
        <param-value>access plus 30 days</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType image/png</param-name>
        <param-value>access plus 30 days</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType image/jpeg</param-name>
        <param-value>access plus 30 days</param-value>
    </init-param>
    <!-- Optional: Add caching for other static assets -->
    <init-param>
        <param-name>ExpiresByType text/css</param-name>
        <param-value>access plus 7 days</param-value>
    </init-param>
</filter>
<!-- Map the filter to Hippo's image paths -->
<filter-mapping>
    <filter-name>StaticResourceCacheFilter</filter-name>
    <url-pattern>/content/gallery/*</url-pattern>
    <url-pattern>/site/resources/*</url-pattern>
</filter-mapping>

This filter will inject Expires and Cache-Control headers into all image responses from the specified paths.

2.2 Configure Caching for Hippo Image Variants

Hippo automatically generates resized image variants (e.g., thumbnail, large) for gallery images. To ensure these variants get proper caching headers, edit repository-data/application/configuration/modules/imaging/imaging-module.xml:

<module>
    <parameters>
        <!-- Set cache max age to 30 days (in seconds) -->
        <parameter name="cache.maxAge">2592000</parameter>
        <!-- Allow public caching (browsers, CDNs) -->
        <parameter name="cache.public">true</parameter>
    </parameters>
</module>

This tells Hippo to include cache control headers directly when generating image variants.

2.3 Verify the Cache is Working

Start your local Hippo instance, then:

  1. Open your browser’s developer tools (F12)
  2. Navigate to a page with images
  3. Check the Network tab, select an image, and look at the Response Headers:
    • You should see Cache-Control: max-age=2592000 (30 days in seconds) and an Expires date 30 days in the future
    • Refresh the page—if caching works, the image will return a 304 Not Modified status instead of re-downloading

3. Local Development Tips

  • Disable caching temporarily for debugging: If you’re actively editing images, either set the ExpiresByType value to access plus 0 seconds in the filter, or check the "Disable cache" box in your browser’s developer tools.
  • Enable ETags for smarter caching: Tomcat’s DefaultServlet can generate ETags (unique identifiers for resources) to help browsers detect updates. Add this to your web.xml to enable it:
    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>etag</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    This ensures browsers only re-download images when the actual file content changes.

内容的提问来源于stack exchange,提问作者Paulomi Chakraborty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:16:07