Understanding Routes.static_path on javascript files

I have a line like this in my phoenix project :

<script type="text/javascript" 
        src="<%= Routes.static_path(@conn, "/js/myScript.js") %>"></script> 

When I run the project with MIX_ENV=“prod”, the generated html looks like this

<script type="text/javascript" 
        src="/js/myScript-e96060ad8bff62ca8d0fbb42c87d0cb4.js?vsn=d"></script>

I assume Phoenix copys the file /js/myScript.js to /js/myScript-e96060ad8bff62ca8d0fbb42c87d0cb4.js. What is the reason of that ?

Also my issue is that when I commit a new version of myScript.js, it is not taken into account : the same old file myScript-e96060ad8bff62ca8d0fbb42c87d0cb4.js is used instead of being updated. What is the best way to handle that ?

1 Like

This is done by mix phx.digest. It takes all static files and copies them to an additional file with the checksum as part of the filename. It also creates a json manifest file in the root, which has a list of mappings of paths -> files with checksum.

This is done to make things like multiple versions on CDN easier – a.k.a. pages already loaded can still access the old script even after a deploy – as well as have proper cache busting if you deploy a new version, so the new html includes a new filename, which prevents the browser from falling back to cached resources no longer current.

4 Likes

Thanks.
When I run mix phx.digest, it freezes for a few seconds, write the message “killed” and stops. Any idea?

Well, I have a lot of dependencies, which I think was too much to handle. When I remove them from the directory the command succeeds