Fingerprint attributes in Maven / JSP / RequireJS Webapp?

I have a simple Maven Webapp that uses one JSP and RequireJS to work with a single-page Javascript-heavy application. I was looking for something that I can use for fingerprints during the build process (.js, .css, etc.), but I did not find anything that would solve this problem.

I would like the asset file names to change whenever the contents change, so I can tell the browser to cache them for a very long time, but still download the latter when they change. I will also need references to those assets that will be updated whenever they change. Whatever I use will have to work with RequireJS.

Any suggestions?

+5
source share
2 answers

I solved this problem recently using the RequireJS urlArgsconfig parameter . I doubt that renaming files containing a timestamp would be feasible, it would complicate the assembly and RequireJS configuration, and most likely would require hacking for development or production. So, in a logical order:

  • in pom.xml:

    <properties>
        (...)
        <build.version>${maven.build.timestamp}</build.version>
    </properties>
    
  • in the main JSP file:

    <script type="text/javascript">
        var require = {
            (...)
            urlArgs: 'v=${build.version}',
        };
    </script>
    
    <link rel="stylesheet" type="text/css" href="style.css?v=${build.version}"></link>    
    <script data-main="app" src="libs/require.js?v=${build.version}"></script>
    

    It is important to define an object require before import require.js in order to make urlArgs work

  • pom.xml, again:

    <resources>
      (...)
      <resource>
        <targetPath>${project.build.directory}/filteredWebapp</targetPath>
        <directory>src/main/webapp</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    
  • r.js buildconfig (btw. I use requirejs-maven-plugin to connect Maven and r.js):

    ({
        appDir: '${project.build.directory}/filteredWebapp',
        dir: '${project.build.directory}/${project.build.finalName}',
        (...)
    })
    

    src/main/webapp (.. filteredWebapp), , r.js , . r.js buildconfig src/main/webapp; , maven (.. , r.js, ${build.timestamp})

. , ( , ). "v = ${build.timestamp}" .

, , , . , !

+2

"-", , . : , cdn . . . . - ( , ), , . , - OLD , .

+1

All Articles