Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Grails 4 configuration class for specifying static files that should not be cached
package com.hypercision.attproto.assets
import groovy.transform.CompileStatic
import org.springframework.stereotype.Component
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@CompileStatic
@Component
class StaticAssetsResolverConfig implements WebMvcConfigurer { // WebMvcConfigurerAdapter for Grails 3.x
// Copied from the GrailsWebMvcConfigurer
// - https://github.com/grails/grails-core/blob/28556c0a1a01d2958d6d3aed251dcacc2a6e38da/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy#L179-L193
private static final String[] RESOURCE_LOCATIONS = [
"/", "classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/"
]
@Override
void addResourceHandlers(ResourceHandlerRegistry registry) {
// Want to match all of our application JavaScript and XML i.e.
// /src/main/resources/public/ui5/com/hypercision/attproto/Device/crud/list/List.controller.js
// When I use these patterns, the matched files are still being cached:
// '/static/ui5/com/**/*.xml', '/static/ui5/com/**/*.js'
// When I use this pattern, then files named List.controller.js are not cached:
// '/static/**/List.controller.js'
// When I use this pattern, the matched files are returning 404 status from the server:
//'/static/ui5/com/**'
registry.addResourceHandler('/static/ui5/com/**/*.xml', '/static/ui5/com/**/*.js') // ant matcher pattern
.addResourceLocations(RESOURCE_LOCATIONS)
.setCachePeriod(0) // <-- no-cache
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment