angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

No hash in name in production build

Open fredrik-holmqvist opened this issue 10 years ago • 31 comments

Hi

I am wondering if there is a setting to disable the hash in the name for main bundle and style bundle for production builds?

/Fredrik

fredrik-holmqvist avatar Oct 19 '16 09:10 fredrik-holmqvist

I need filenames that don't change but also would like to have a hash to invalidate cache. Is there a reason the hash can't be on the query string?

For example, this gets injected into my index.html:

<script type="text/javascript" src="inline.908517239e551bfc4e9b.bundle.js"></script>
<script type="text/javascript" src="polyfills.d2c49be8f78b2753060a.bundle.js"></script>
<script type="text/javascript" src="vendor.9f2dc5768d3730cb8f9d.bundle.js"></script>
<script type="text/javascript" src="main.6fa8c063126f9ead769a.bundle.js"></script>

How about:

<script type="text/javascript" src="inline.bundle.js?908517239e551bfc4e9b"></script>
<script type="text/javascript" src="polyfills.bundle.js?d2c49be8f78b2753060a"></script>
<script type="text/javascript" src="vendor.bundle.js?9f2dc5768d3730cb8f9d"></script>
<script type="text/javascript" src="main.bundle.js?6fa8c063126f9ead769a"></script>

kevindqc avatar Apr 25 '17 21:04 kevindqc

@kevindqc With query string there's no guarantee that the files will be cached.

Squid for example used to have (I think it has changed) a default configuration that didn't cache dynamic content and query strings were considered to be dynamic content.

grizzm0 avatar Apr 26 '17 07:04 grizzm0

Temporary solution...

In your package.json > scripts: "build": "ng build --prod && mv dist/main.*.bundle.js dist/main.js && mv dist/vendor.*.bundle.js dist/vendor.js && mv dist/inline.*.bundle.js dist/inline.js && mv dist/polyfills.*.bundle.js dist/polyfills.js && cp -f src/index.dist-default.html dist/index.html"

The last operation of the build command above is to overwrite the index.html with something like "index.dist-default.html" in your src and set all script sources without the hash like this...

Example of scripts In your index replacement file (index.dist-default.html or whatever): <script type="text/javascript" src="inline.js"></script> <script type="text/javascript" src="polyfills.js"></script> <script type="text/javascript" src="vendor.js"></script> <script type="text/javascript" src="main.js"></script>

timperez-weather avatar Apr 27 '17 17:04 timperez-weather

if you want build file like

first set build line like ng build --prod --output-hashing none

second update webpack config,the file at node_module/@angular/cli/models/webpack-configs/brower.js update HtmlWebpackPlugin config, new HtmlWebpackPlugin({ ... hash: true // add }),

this is just i find...hopeful official solution

lihuabest avatar Jul 05 '17 08:07 lihuabest

I tested the above solution and it works well, resulting in the following.

index.html
<script type="text/javascript" src="inline.bundle.js?b82bfbf37295902d0092"></script>
<script type="text/javascript" src="polyfills.bundle.js?b82bfbf37295902d0092"></script>
<script type="text/javascript" src="scripts.bundle.js?b82bfbf37295902d0092"></script>
<script type="text/javascript" src="styles.bundle.js?b82bfbf37295902d0092"></script>
<script type="text/javascript" src="vendor.bundle.js?b82bfbf37295902d0092"></script>
<script type="text/javascript" src="main.bundle.js?b82bfbf37295902d0092"></script>

IMHO, this solution provides the best of both worlds and end solution for many posters, as follows: (1) ng build --prod --output-hashing none - deploy files with same name on each and every build which is desirable by many developers for SCM purposes (tracking, comparison, history etc.) and (2) update webpack config to add hash query string in index.html - attaches a unique hash as a query string which, in most cases, will result in uniquely versioning your file in the browser cache so the browser reads the new version of the artifacts on each new deployment rather than reading it from cache.

However, it requires forking Angular CLI at the webpack config,the file at node_module/@angular/cli/models/webpack-configs/brower.js to add one configuration line to the new HtmlWebpackPlugin section (... ,hash: true // add }). To avoid a fork to Angular CLI, I would recommend a new option, to the effect of:

ng build --prod --output-hashing partial

where "partial" adds a hash query string but does not add a hash to files. The name of the option can be decided; having a configurable feature that blends both features above would avoid a fork, preserve SCM tracking of files, while also eliminate most caching issues, all of which are useful and seem to meet a requirement of a large population. Thank you.

dgecawich avatar Aug 01 '17 10:08 dgecawich

Any chance to reopen this issue for a solution like @dgecawich suggested? (move hash to query string) Or should we open a new issue?

I have a usecase that would need the same solution. A Piwik plugin tries to load the styles of our site at a later point but without a fixed filename it will already be gone. However, disabling the hashing completely will lead to trouble with the cache. Hash in the query string seems to have the best of both worlds.

magnattic avatar Feb 08 '18 07:02 magnattic

--output-hashing none works a treat. Nice that everyone on this post was notified and that it is now included in the docs. Oh wait, they weren't and it's not. I know - we'll add a great feature in that everyone was requesting but won't tell anyone or put it in the docs. Now that sounds like what devs would do... 👍

chrisj-skinner avatar Apr 30 '18 14:04 chrisj-skinner

Thanks! It's a really useful feature

yurart avatar May 24 '18 13:05 yurart

@lihuabest Is there a way to replace the hash number with a specified version number? Something like this: <script type="text/javascript" src="main.bundle.js?version=1.2.0"></script>

abbymrs avatar Jun 28 '18 07:06 abbymrs

in angular.json

"configurations": {
            "production": {
                           ...
                           "outputHashing": "none",
                           ...
             }
}

Vitzkrieg avatar May 31 '19 15:05 Vitzkrieg

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.