Caching of static elements within the portal

Hi There,

I’m trying to get some browser caching headers emitted for the static items within my portal application.

I’ve been attempting this by changing my portal-dispatcher-spring.xml file so that it contains the following lines:

    <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
    <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
    <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>

However, this does not seem to be having any effect on the elements when they’re served to my browser.

Can you advise on what I might be doing wrong?

Many thanks


It seems that default CUBA portal template has enabled Spring Security for /resources url and Spring Security prevents resources from caching by sending “Pragma: no-cache” header.

For now you can simply disable Spring security for static resources. In your portal-security-spring.xml add http tag for /resources path with security=“none” and remove intercept-url with pattern="/resources":

<http pattern="/api/**" security="none"></http>
<http pattern="/resources/**" security="none"></http>

<http auto-config="true">
    <!-- login & registration -->
    <intercept-url pattern="/login" access="permitAll"></intercept>

    <!-- index page -->
    <intercept-url pattern="/" access="permitAll"></intercept>

    <intercept-url pattern="/**" access="hasRole('ROLE_USER')"></intercept>

    <form-login login-page='/login'

    <!--logout, do not invalidate session, it makes cuba_PortalLogoutHandler-->
    <logout invalidate-session="false"

And then you can use cache-period attribute for paths in portal-dispatcher-spring.xml:

<mvc:resources mapping="/resources/**" location="/resources/" cache-period="360000"></mvc:resources>

If caching options works you will see 304 HTTP status for consequent requests of the same resource in a browser development console.

And thank you for your question, we will fix template for the next release of Studio.

Thanks very much for your response Yuri.

By following the instructions above I now have caching in place for my static resources.



In Studio from 6.3.0, when the portal module is created, security for static resources is disabled by default.