Open Closed

PWA cache #9072


User avatar
0
maria_ruiz created
  • ABP Framework version: v9.0.4

  • UI Type: Blazor WASM

  • Database System: EF Core (SQL Server)

  • Tiered (for MVC) or Auth Server Separated (for Angular): no

  • Exception message and full stack trace:

  • Steps to reproduce the issue:

Hi,
We have recently implemented the PWA and I think we have a bit of a problem.

I have been reading the documentation:
https://abp.io/docs/latest/framework/ui/blazor/pwa-configuration

We use the service-workers that come by default in the ABP template,

We have updated a few versions of nugets in the application
and accessed the published application,
The problem is that when we accessed the web through the Chrome browser, the changes of the new versions were not applied,
it seemed that they were using a previous version because it did not work as expected, correcting an error,

This is stated in your documentation:

"The service-worker.published.js file, which is used after the app is published. Caches certain file extensions and supports offline scenarios by default (uses a cache-first strategy). A user must first visit the app while they're online. The browser automatically downloads and caches all of the resources required to operate offline and then when the network connection is disconnected, it can be used like before."

In the Chrome browser I had to clear the cache so that it would detect the new changes,
Can it be related to having the PWA?


1 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi,

    Whenever the file name is exactly same, the service-worker and browser do not download it again. In this scenario, people uses v parameter in the querystring with the file hash or actual version like main.js?v=1f129b3...daab834 or main.js?v=1.0.2 etc.

    Whenever the file path is changed even with querystring, it's considered as a new file and re-downloaded.

    ABP startup templates already has this unique file hash as a version in the templates:
    image.png
    image.png

    When you execute abp bundle command to update bundles, it'll update the hashes in the file links too.


    If you face a problem with *.dll files, here you can follow this workaround.

    1. Add id="global-script" to your <script> tag in App.razor file.

    <script id="global-script" src="global.js?_v=638524142774706627"></script>
    
    1. Go to service-worker.published.js file and create a function to store this version and check it:

    
    function isVersionMismatch() {
        const globalScript = document.getElementById('global-script');
        const version = globalScript.getAttribute('src').split('?')[1];
        const cacheVersion = localStorage.getItem('abp.assets.version');
    
        // Update the cache version
        localStorage.setItem('abp.assets.version', version);
        return version !== cacheVersion;
    }
    
    1. Find onFetch function in the same file and append the check right at the begginning of the function to check version mismatch:

    async function onFetch(event) {
        if (isVersionMismatch()) {
            return fetch(event.request);
        }
    
        // ... rest of the codes
    }
    
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.3.0-preview. Updated on April 08, 2025, 09:13