Gathering detailed insights and metrics for @sitelintcode/angular-static-assets-hash
Gathering detailed insights and metrics for @sitelintcode/angular-static-assets-hash
Gathering detailed insights and metrics for @sitelintcode/angular-static-assets-hash
Gathering detailed insights and metrics for @sitelintcode/angular-static-assets-hash
Create a list of Angular static assets and a hash for each file.
npm install @sitelintcode/angular-static-assets-hash
Typescript
Module System
Node Version
NPM Version
JavaScript (80.98%)
TypeScript (19.02%)
Total Downloads
10,327
Last Day
13
Last Week
143
Last Month
384
Last Year
4,973
MPL-2.0 License
3 Stars
54 Commits
1 Watchers
1 Branches
1 Contributors
Updated on May 03, 2025
Minified
Minified + Gzipped
Latest Version
0.0.27
Package Id
@sitelintcode/angular-static-assets-hash@0.0.27
Unpacked Size
43.74 kB
Size
13.91 kB
File Count
19
NPM Version
11.3.0
Node Version
23.11.0
Published on
May 01, 2025
Cumulative downloads
Total Downloads
Last Day
44.4%
13
Compared to previous day
Last Week
95.9%
143
Compared to previous week
Last Month
-0.8%
384
Compared to previous month
Last Year
-7.1%
4,973
Compared to previous year
Cache busting static files (images, pdfs, pngs, etc.) for the Angular application.
When building, Angular will add a hash to JavaScript, CSS, and assets referenced in your CSS files. However, what about images and other assets you have in your assets folder that you reference in templates?
Those assets are not hashed.
The package looks for static assets, creates a list of them with their hashes, and saves it as a JSON file. This is useful when you want to change the same static asset without changing the name and ensure that the browser will always fetch the latest version.
Note that the hash is created the same way as for Subresource Integrity (SRI).
1npm install @sitelintcode/angular-static-assets-hash --save--dev
You can use directly in package.json
script
or through npx createAngularStaticHashes
[params].
--staticAssetsPath=[path to static assets]
. Default: [angular root folder]/assets/images
--globPattern=[glob pattern]
. Default: /**/*
1import { createHashes } from '@sitelintcode/angular-static-assets-hash'; 2 3const angularStaticAssetsHash = new AngularStaticAssetsHash(); 4 5angularStaticAssetsHash.createHashes();
1{ 2 "assets/images/example.png": "iY5RY0G8wePLPRkZSTgW2XYZFZ7kQOqXoJTFpQFG5nI", 3 "assets/images/avatar.svg": "7A7qFs_iOghsdUtLG-7y-5cxT3FC8S_BRXl5ldsNY7Y", 4 "assets/images/body-background.svg": "K2FTBtDsxgKLQFr4BUW1ptnLWqPCKPyGypHCBTfcctQ", 5 "assets/images/icons.svg": "Ka-ngr7Fht6ucmN03kzJeMN7f2iOtnkD-D63QJ01jhM" 6}
Once the list of static assets is created, we need to have a way to use it with Angular. For that purpose, we can use an Angular Pipe.
staticAssetsList
private property. It points to the location of assets.json
file is created.[angular root path]/assets/images
.1import { Pipe, PipeTransform } from '@angular/core'; 2 3import staticAssetsList from '../../assets.json'; 4 5@Pipe({ 6 name: 'fileHash' 7}) 8export class FileHashPipe implements PipeTransform { 9 10 private staticAssets: { [key: string]: string }; 11 12 constructor() { 13 this.staticAssets = staticAssetsList; 14 } 15 16 private addParamsToUrl(givenUrl: string, urlParameters: string): string { 17 18 if (typeof urlParameters !== 'string' || urlParameters.length === 0) { 19 return givenUrl; 20 } 21 22 const urlSplitByHash: string[] = givenUrl.split('#'); 23 const hash: string = urlSplitByHash[1] || ''; 24 const params: string[] = urlParameters.split('&'); 25 let url: string = urlSplitByHash[0]; 26 27 if (url.indexOf('?') === -1) { 28 url += '?'; 29 } else { 30 url += '&'; 31 } 32 33 url += params.map((paramItem: string): string => { 34 const p: string[] = paramItem.split('='); 35 36 return `${p[0]}=${window.encodeURIComponent(p[1])}`; 37 }) 38 .join('&'); 39 40 url = url.slice(0, -1); // remove last & 41 42 return hash ? `${url}#${hash}` : url; 43 } 44 45 private getHashForStaticAsset(assetPath: string): string { 46 const path: string = assetPath.split('#')[0]; 47 48 if (typeof this.staticAssets[path] === 'string') { 49 return this.addParamsToUrl(assetPath, `c=${this.staticAssets[path]}`); 50 } 51 52 return ''; 53 } 54 55 public transform(filePath: string): string { 56 const filePathWithCacheHash: string = this.getHashForStaticAsset(filePath); 57 58 return filePathWithCacheHash; 59 } 60}
Later in the code the pipe can be used in the following way.
1<img attr.src="{{ 'assets/images/example.png' | fileHash }}" alt="">
The hash is quite useful when we want to manage, e.g., one file with all <svg>
icons. While you could change your single file with all svg icons all the time, your code remains the same.
1<svg aria-hidden="true" focusable="false" viewBox="0 0 48 48" width="32" height="32"> 2 <use attr.href="{{ 'assets/images/icons.svg#image-logo' | fileHash }}"></use> 3</svg>
You can see the implementation by looking at the source code of the app based on Angular. The app refers to SiteLint Audit Platform.
Contributions are welcome, and greatly appreciated! Contributing doesn't just mean submitting pull requests. There are many different ways for you to get involved, including answering questions on the issues, reporting or triaging bugs, and participating in the features evolution process.
MOZILLA PUBLIC LICENSE, VERSION 2.0
No vulnerabilities found.
No security vulnerabilities found.