Integration with Frontend
Detail steps of how to integrate between frontend and backend.
Overview
When Frontend team release new FE package, BE team will have following items:
-
/PreciseAlloy.Patterns - folder
This folder contains HTML snapshots of the rendered frontend pages using the code from the time of package creation. Its purpose is to facilitate change tracking, enabling the Backend team to easily identify the areas that require reintegration.
Note that these files are not necessary for running your website, so there is no need to deploy them. -
/PreciseAlloy.Web/wwwroot/assets - folder
This folder contains compiled frontend assets, including CSS, JavaScript, and images required for integration and website functionality. -
/PreciseAlloy.Web/wwwroot/assets/hashes.json - file
This file provides a comprehensive list of all assets in the frontend bundle, along with their content hashing. It is essential to include this file during site deployment to ensure the cache buster asset path generation, thereby preventing any issues with CDN caching. Failure to include this file will render the cache buster mechanism ineffective.
The Frontend team will handle the preparation of these items, so the Backend team doesn't need to take any action to obtain them. However, if there is a need to manually build them, please refer to the Integration - Frontend guide for detailed instructions.
Caution
Please be aware that the following files/folders are automatically generated by the frontend integration pipeline. It is important not to make any manual modifications as they will be overwritten.
- /PreciseAlloy.Patterns/
- /PreciseAlloy.Web/wwwroot/assets/css/
- /PreciseAlloy.Web/wwwroot/assets/fonts/
- /PreciseAlloy.Web/wwwroot/assets/images/
- /PreciseAlloy.Web/wwwroot/assets/js/
- /PreciseAlloy.Web/wwwroot/assets/vendors/
- /PreciseAlloy.Web/wwwroot/assets/hashes.json
Making any changes to these files/folders manually will result in the loss of those modifications.
Integration
Suppose that you have a file named hashes.json located at /PreciseAlloy.Web/wwwroot/assets/hashes.json with the following content:
To simplify handling this information, we will create HTML extension methods. These methods will generate appropriate tags for CSS and JavaScript files, taking care of cache busting through the hash value appended to the file URLs. For the list of availabe methods, please check /PreciseAlloy.Utils/Extensions/HtmlExtensions.cs.
Including CSS files
If you want to include b-header.css into the <head>
element of your page, use the following method:
@{
Html..;
}
The output will be:
The ?v=t8cxlFd_dF
part represents the hash of the file's content, generated at the frontend's compile time. This approach ensures proper caching. If the file remains unchanged, the hash value remains the same, allowing browsers and/or CDN to cache it for extended periods. When the file content changes, the hash value changes as well, prompting browsers to retrieve the updated file to avoid any caching issues.
You can call the above method multiple times, but the rendered HTML will contain exactly one instance of the link to that CSS file.
Including JavaScript files
To include header.js at the footer of your page, use the following method:
@{
Html..;
}
The output will be:
Similar to the CSS method, the JavaScript method also appends the hash value to the file URL, ensuring cache busting.
Obtaining the path directly
If you only need the path of an asset without the HTML tag, you can use the method @Html.GetCacheBusterPath()
directly. For example, to include an image:
The output will be:
Special Case for React Components
There is a special case where the file react-loader.0x....js serves as the entry point for all React components using client-side rendering. You only need to include it once in the layout. For the <head>
element, use the following method:
The method will search for any file with a name containing /react-loader.0x and return that file.
The output will be:
By following these guidelines, you can effectively handle hashed assets, manage cache busting, and ensure efficient loading of resources in your web application.