├── .gitattributes ├── .gitignore ├── Blazor.PWA.MSBuild-NuGet-Icon.pdn ├── Blazor.PWA.MSBuild-NuGet-Icon.png ├── Blazor.PWA.MSBuild.Tasks ├── Blazor.PWA.MSBuild.Tasks.csproj ├── Directory.Build.props ├── Templates │ ├── Images │ │ ├── default-icon-192x192.png │ │ └── default-icon-512x512.png │ └── ServiceWorker │ │ ├── sw_fetch-none.template.js │ │ ├── sw_fetch-offline-first.template.js │ │ ├── sw_install.template.js │ │ ├── sw_register-beforeinstallprompt.template.js │ │ ├── sw_register-installable-banner.template.js │ │ ├── sw_register-installable-blazor.template.js │ │ ├── sw_register-update-alert.template.js │ │ └── sw_register.template.js └── build │ ├── BlazorPWA.MSBuild.Manifest.targets │ ├── BlazorPWA.MSBuild.ServiceWorker.targets │ ├── BlazorPWA.MSBuild.ServiceWorkerRegister.targets │ └── BlazorPWA.MSBuild.targets ├── Blazor.PWA.MSBuild.sln ├── CHANGELOG.md ├── LICENSE.txt ├── README - nuget.md └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild-NuGet-Icon.pdn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-MisterMagoo/Blazor.PWA.MSBuild/03c829a8b70b97beae861ef0c8918b7e2d161453/Blazor.PWA.MSBuild-NuGet-Icon.pdn -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild-NuGet-Icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-MisterMagoo/Blazor.PWA.MSBuild/03c829a8b70b97beae861ef0c8918b7e2d161453/Blazor.PWA.MSBuild-NuGet-Icon.png -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Blazor.PWA.MSBuild.Tasks.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard1.6 5 | 6 | true 7 | 8 | tasks 9 | true 10 | Mister Magoo 11 | 2019 SQL-MisterMagoo 12 | true 13 | The easiest way to turn your Blazor application into a PWA with offline capabilities. 14 | Add this package to your build process and it will generate the files you need to become PWA compatible. 15 | This does not make your application ready for distribution through "App Stores" - it just provides basic PWA functionality. 16 | BlazorPWA.MSBuild 17 | LICENSE.txt 18 | 19 | false 20 | https://github.com/SQL-MisterMagoo/Blazor.PWA.MSBuild 21 | Blazor,Build,MSBuild,PWA,Manifest,ServiceWorker,C#,DotNET,Web,Client 22 | BlazorPWA.MSBuild 23 | https://github.com/SQL-MisterMagoo/Blazor.PWA.MSBuild 24 | 25 | git 26 | This is a very basic PWA build target. 27 | Just by including this in the build, it can generate the basic requirements for an installable PWA for Blazor. 28 | The result is not an App Store package, it is simply the basic requirements for PWA. 29 | I will add more network caching strategies, but for now it has just one for CSB - cache all local assets. 30 | Blazor.PWA.MSBuild-NuGet-Icon.png 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | True 40 | 41 | 42 | 43 | True 44 | 45 | 46 | 47 | 48 | 49 | 50 | build\ 51 | True 52 | 53 | 54 | build\ 55 | True 56 | 57 | 58 | build\ 59 | True 60 | 61 | 62 | build\ 63 | True 64 | 65 | 66 | True 67 | Templates\ 68 | 69 | 70 | True 71 | Templates\ 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Directory.Build.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 1.0.2 4 | beta$([System.DateTime]::Now.ToString("yyyyMMdd-HHmmss")) 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/Images/default-icon-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-MisterMagoo/Blazor.PWA.MSBuild/03c829a8b70b97beae861ef0c8918b7e2d161453/Blazor.PWA.MSBuild.Tasks/Templates/Images/default-icon-192x192.png -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/Images/default-icon-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-MisterMagoo/Blazor.PWA.MSBuild/03c829a8b70b97beae861ef0c8918b7e2d161453/Blazor.PWA.MSBuild.Tasks/Templates/Images/default-icon-512x512.png -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_fetch-none.template.js: -------------------------------------------------------------------------------- 1 | self.addEventListener(networkFetchEvent, event => { 2 | return fetch(event.request); 3 | }); 4 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_fetch-offline-first.template.js: -------------------------------------------------------------------------------- 1 | self.addEventListener(networkFetchEvent, event => { 2 | const requestUrl = new URL(event.request.url); 3 | if (requestUrl.origin === location.origin) { 4 | if (requestUrl.pathname === baseURL) { 5 | event.respondWith(caches.match(indexURL)); 6 | return; 7 | } 8 | } 9 | event.respondWith( 10 | caches.match(event.request) 11 | .then(response => { 12 | if (response) { 13 | return response; 14 | } 15 | return fetch(event.request) 16 | .then(response => { 17 | if (response.ok) { 18 | if (requestUrl.origin === location.origin) { 19 | caches.open(staticCacheName).then(cache => { 20 | cache.put(event.request.url, response); 21 | }); 22 | } 23 | } 24 | return response.clone(); 25 | }); 26 | }).catch(error => { 27 | console.error(error); 28 | }) 29 | ); 30 | }); 31 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_install.template.js: -------------------------------------------------------------------------------- 1 | // * listen for the install event and pre-cache anything in filesToCache * // 2 | self.addEventListener(swInstallEvent, event => { 3 | self.skipWaiting(); 4 | event.waitUntil( 5 | caches.open(staticCacheName) 6 | .then(cache => { 7 | return cache.addAll(requiredFiles); 8 | }) 9 | ); 10 | }); 11 | self.addEventListener(swActivateEvent, function (event) { 12 | event.waitUntil( 13 | caches.keys().then(function (cacheNames) { 14 | return Promise.all( 15 | cacheNames.map(function (cacheName) { 16 | if (staticCacheName !== cacheName && cacheName.startsWith(staticCachePrefix)) { 17 | return caches.delete(cacheName); 18 | } 19 | }) 20 | ); 21 | }) 22 | ); 23 | }); -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_register-beforeinstallprompt.template.js: -------------------------------------------------------------------------------- 1 | window.addEventListener('beforeinstallprompt', function (e) { 2 | // Prevent Chrome 67 and earlier from automatically showing the prompt 3 | e.preventDefault(); 4 | // Stash the event so it can be triggered later. 5 | window.PWADeferredPrompt = e; 6 | 7 | showAddToHomeScreen(); 8 | 9 | }); 10 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_register-installable-banner.template.js: -------------------------------------------------------------------------------- 1 |  2 | function showAddToHomeScreen() { 3 | var pwaInstallPrompt = document.createElement('div'); 4 | var pwaInstallButton = document.createElement('button'); 5 | var pwaCancelButton = document.createElement('button'); 6 | 7 | pwaInstallPrompt.id = 'pwa-install-prompt'; 8 | pwaInstallPrompt.style.position = 'absolute'; 9 | pwaInstallPrompt.style.bottom = '0.1rem'; 10 | pwaInstallPrompt.style.left = '0.1rem'; 11 | pwaInstallPrompt.style.right = '0.1rem'; 12 | pwaInstallPrompt.style.padding = '0.5rem'; 13 | pwaInstallPrompt.style.display = 'flex'; 14 | pwaInstallPrompt.style.backgroundColor = 'lightslategray'; 15 | pwaInstallPrompt.style.color = 'white'; 16 | pwaInstallPrompt.style.fontFamily = 'sans-serif'; 17 | pwaInstallPrompt.style.fontSize = '1.3rem'; 18 | pwaInstallPrompt.style.borderRadius = '4px'; 19 | 20 | pwaInstallButton.style.marginLeft = 'auto'; 21 | pwaInstallButton.style.width = '4em'; 22 | pwaInstallButton.style.backgroundColor = '#00796B'; 23 | pwaInstallButton.style.color = 'white'; 24 | pwaInstallButton.style.border = 'none'; 25 | pwaInstallButton.style.borderRadius = '25px'; 26 | 27 | pwaCancelButton.style.marginLeft = '0.3rem'; 28 | pwaCancelButton.style.width = '4em'; 29 | pwaCancelButton.style.backgroundColor = '#9d0d0d'; 30 | pwaCancelButton.style.color = 'white'; 31 | pwaCancelButton.style.border = 'none'; 32 | pwaCancelButton.style.borderRadius = '25px'; 33 | 34 | pwaInstallPrompt.innerText = 'Add to your homescreen?'; 35 | pwaInstallButton.innerText = 'ok'; 36 | pwaCancelButton.innerText = 'no'; 37 | 38 | pwaInstallPrompt.appendChild(pwaInstallButton); 39 | pwaInstallPrompt.appendChild(pwaCancelButton); 40 | document.body.appendChild(pwaInstallPrompt); 41 | 42 | pwaInstallButton.addEventListener('click', addToHomeScreen); 43 | pwaCancelButton.addEventListener('click', hideAddToHomeScreen); 44 | setTimeout(hideAddToHomeScreen, 10000); 45 | } 46 | 47 | function hideAddToHomeScreen() { 48 | var pwa = document.getElementById('pwa-install-prompt'); 49 | if (pwa) document.body.removeChild(pwa); 50 | } 51 | 52 | function addToHomeScreen(s, e) { 53 | hideAddToHomeScreen(); 54 | if (window.PWADeferredPrompt) { 55 | window.PWADeferredPrompt.prompt(); 56 | window.PWADeferredPrompt.userChoice 57 | .then(function (choiceResult) { 58 | window.PWADeferredPrompt = null; 59 | }); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_register-installable-blazor.template.js: -------------------------------------------------------------------------------- 1 |  2 | function showAddToHomeScreen() { 3 | DotNet.invokeMethodAsync(blazorAssembly, blazorInstallMethod) 4 | .then(function () { }, function (er) { setTimeout(showAddToHomeScreen, 1000); }); 5 | } 6 | 7 | window.BlazorPWA = { 8 | installPWA: function () { 9 | if (window.PWADeferredPrompt) { 10 | window.PWADeferredPrompt.prompt(); 11 | window.PWADeferredPrompt.userChoice 12 | .then(function (choiceResult) { 13 | window.PWADeferredPrompt = null; 14 | }); 15 | } 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_register-update-alert.template.js: -------------------------------------------------------------------------------- 1 |  2 | window['updateAvailable'] 3 | .then(isAvailable => { 4 | if (isAvailable) { 5 | alert(updateAlertMessage); 6 | } 7 | }); 8 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/Templates/ServiceWorker/sw_register.template.js: -------------------------------------------------------------------------------- 1 | window.updateAvailable = new Promise(function (resolve, reject) { 2 | var { hostname } = window.location; 3 | if (typeof ignoreHosts !== 'undefined') { 4 | if (ignoreHosts.includes(hostname)) { 5 | return; 6 | } 7 | } 8 | if ('serviceWorker' in navigator) { 9 | navigator.serviceWorker.register(serviceWorkerFileName) 10 | .then(function (registration) { 11 | console.log('Registration successful, scope is:', registration.scope); 12 | registration.onupdatefound = () => { 13 | const installingWorker = registration.installing; 14 | installingWorker.onstatechange = () => { 15 | switch (installingWorker.state) { 16 | case swInstalledEvent: 17 | if (navigator.serviceWorker.controller) { 18 | resolve(true); 19 | } else { 20 | resolve(false); 21 | } 22 | break; 23 | default: 24 | } 25 | }; 26 | }; 27 | }) 28 | .catch(error => 29 | console.log('Service worker registration failed, error:', error)); 30 | } 31 | }); 32 | 33 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/build/BlazorPWA.MSBuild.Manifest.targets: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | $(SolutionName) 12 | $(ProjectName) 13 | 14 | 15 | / 16 | /$(ManifestBaseUrl) 17 | $(ManifestBaseUrl)/ 18 | 19 | 20 | standalone 21 | 22 | 23 | $(MSBuildThisFileDirectory)..\Templates\ 24 | $(ManifestTemplatePath)default-icon-192x192.png 25 | $(ManifestTemplatePath)default-icon-512x512.png 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 192x192 35 | 36 | 37 | 512x512 38 | 39 | 40 | 41 | 42 | 45 | 48 | 49 | 50 | 51 | 52 | 192x192 53 | 54 | 55 | 512x512 56 | 57 | 63 | 64 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | $(WWWRoot)$(ProjectIndexPage) 77 | $(ProjectHomePage) 78 | $([System.IO.File]::ReadAllText($(IndexFile))) 79 | 80 | 81 | 83 | 84 | 89 | 90 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/build/BlazorPWA.MSBuild.ServiceWorker.targets: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | install 14 | 15 | 16 | installed 17 | 18 | 19 | activate 20 | 21 | 22 | fetch 23 | 24 | 25 | 26 | 27 | $(OutputPath)dist\**\*.*; 28 | $(WWWRoot)**\*.*; 29 | 30 | 31 | 32 | 33 | $(ServiceWorkerPreCacheExcludeFiles); 34 | $(OutputPath)dist\_content\**\*.*; 35 | $(WWWRoot)**\.*.*; 36 | 37 | 38 | 39 | 40 | $(ServiceWorkerPreCacheExcludeReleaseFiles); 41 | $(OutputPath)dist\**\*.pdb; 42 | 43 | 44 | 45 | 46 | $(WWWRoot)**\$(ServiceWorkerRegisterFileName); 47 | $(WWWRoot)**\$(ManifestFileName); 48 | 49 | 50 | 51 | 52 | const baseURL = '$(ServiceWorkerBaseUrl)'%3B; 53 | const indexURL = '$(ServiceWorkerBaseUrl)$(ProjectIndexPage)'%3B; 54 | const networkFetchEvent = '$(ServiceWorkerNetworkFetchEvent)'%3B; 55 | const swInstallEvent = '$(ServiceWorkerInstallEvent)'%3B; 56 | const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B; 57 | const swActivateEvent = '$(ServiceWorkerActivateEvent)'%3B; 58 | const staticCachePrefix = '$(ServiceWorkerCacheName)-v'%3B; 59 | const staticCacheName = '$(ServiceWorkerCacheName)-v$(ServiceWorkerCacheVersion)'%3B; 60 | 61 | 62 | 63 | 64 | const baseURL = '$(ServiceWorkerBaseUrl)'%3B; 65 | const indexURL = '$(ServiceWorkerBaseUrl)$(ProjectHomePage)'%3B; 66 | const networkFetchEvent = '$(ServiceWorkerNetworkFetchEvent)'%3B; 67 | const swInstallEvent = '$(ServiceWorkerInstallEvent)'%3B; 68 | const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B; 69 | const swActivateEvent = '$(ServiceWorkerActivateEvent)'%3B; 70 | 71 | 72 | 73 | 74 | 75 | 76 | $(MSBuildThisFileDirectory)..\Templates\ServiceWorker\ 77 | 78 | $(ServiceWorkerTemplatePath)sw_$(ServiceWorkerInstallEvent).template.js 79 | 80 | $(ServiceWorkerTemplatePath)sw_$(ServiceWorkerNetworkFetchEvent)-$(ServiceWorkerPattern).template.js 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 105 | 109 | 113 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/build/BlazorPWA.MSBuild.ServiceWorkerRegister.targets: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | $(MSBuildThisFileDirectory)..\Templates\ServiceWorker\ 13 | 14 | $(ServiceWorkerRegisterTemplatePath)sw_register.template.js 15 | 16 | update-alert 17 | 18 | $(ServiceWorkerRegisterTemplatePath)sw_register-$(ServiceWorkerRegisterUpdateType).template.js 19 | 20 | installable-banner 21 | 22 | $(ServiceWorkerRegisterTemplatePath)sw_register-$(ServiceWorkerRegisterInstallableType).template.js 23 | 24 | beforeinstallprompt 25 | 26 | $(ServiceWorkerRegisterTemplatePath)sw_register-$(ServiceWorkerRegisterBeforeInstallPromptType).template.js 27 | 28 | installed 29 | 30 | Update available. Reload the page when convenient. 31 | 32 | $(ProjectName) 33 | 34 | PWAInstallable 35 | 36 | 37 | const serviceWorkerFileName = '$(ServiceWorkerBaseUrl)$(ServiceWorkerFileName)'%3B; 38 | const swInstalledEvent = '$(ServiceWorkerInstalledEvent)'%3B; 39 | const staticCachePrefix = '$(ServiceWorkerCacheName)-v'%3B; 40 | const updateAlertMessage = '$(ServiceWorkerUpdateAlertText)'%3B; 41 | 42 | 43 | $(ServiceWorkerRegisterConstants); 44 | const ignoreHosts = [$(ServiceWorkerIgnoreHosts)]%3B; 45 | 46 | 47 | $(ServiceWorkerRegisterConstants); 48 | const blazorAssembly = '$(ServiceWorkerBlazorAssembly)'%3B; 49 | const blazorInstallMethod = '$(ServiceWorkerBlazorInstallMethod)'%3B; 50 | 51 | 52 | 53 | 54 | 57 | 58 | 61 | 62 | 65 | 66 | 69 | 70 | 71 | 74 | 77 | 80 | 83 | 84 | 90 | 91 | 95 | 96 | 97 | 98 | 99 | 100 | $(WWWRoot)$(ProjectIndexPage) 101 | $(ProjectHomePage) 102 | $([System.IO.File]::ReadAllText($(IndexFile))) 103 | 104 | 105 | 110 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.Tasks/build/BlazorPWA.MSBuild.targets: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | SSB 10 | SSB 11 | CSB 12 | CSB 13 | 14 | 15 | 16 | 17 | ServiceWorker.js 18 | ServiceWorkerRegister.js 19 | 20 | 21 | 22 | offline-first 23 | none 24 | offline-first 25 | 26 | 27 | blazor-cache 28 | 1 29 | 30 | 31 | / 32 | /$(ServiceWorkerBaseUrl) 33 | $(ServiceWorkerBaseUrl)/ 34 | 35 | 36 | index.html 37 | 38 | 39 | Pages/_Host.cshtml 40 | 41 | 42 | 43 | 44 | 45 | manifest.json 46 | 47 | 48 | $(ServiceWorkerForce) 49 | 50 | 51 | 52 | 53 | $(ServiceWorkerForce) 54 | 55 | 56 | 57 | AfterBuild 58 | 59 | 60 | 61 | 62 | $(MsBuildProjectDirectory)\wwwroot\ 63 | 64 | 65 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /Blazor.PWA.MSBuild.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29123.89 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.PWA.MSBuild.Tasks", "Blazor.PWA.MSBuild.Tasks\Blazor.PWA.MSBuild.Tasks.csproj", "{27D84A0F-8F21-4E52-9318-E4403A87E11E}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BCE600C8-35D7-4AA4-9FFB-156E4526097E}" 9 | ProjectSection(SolutionItems) = preProject 10 | CHANGELOG.md = CHANGELOG.md 11 | LICENSE.txt = LICENSE.txt 12 | README - nuget.md = README - nuget.md 13 | README.md = README.md 14 | EndProjectSection 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {27D84A0F-8F21-4E52-9318-E4403A87E11E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {27D84A0F-8F21-4E52-9318-E4403A87E11E}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {27D84A0F-8F21-4E52-9318-E4403A87E11E}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {27D84A0F-8F21-4E52-9318-E4403A87E11E}.Release|Any CPU.Build.0 = Release|Any CPU 26 | EndGlobalSection 27 | GlobalSection(SolutionProperties) = preSolution 28 | HideSolutionNode = FALSE 29 | EndGlobalSection 30 | GlobalSection(ExtensibilityGlobals) = postSolution 31 | SolutionGuid = {8F44D916-3E04-4715-82E5-7B961362548D} 32 | EndGlobalSection 33 | EndGlobal 34 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | #### 06/12/2019 2 | 3 | - PR #5 from @chucker merged with these changes 4 | - Bug: **Property** **`ManifestBaseUrl`** - used to specify the base Url in the manifest.json was not being used in the index `` link for **manifest.json** 5 | - Bug: **Property** **`ServiceWorkerBaseUrl`** - used to specify the base Url in the ServiceWorker was not being used in the _required files_ list. 6 | - Bug: **Property** **`ServiceWorkerBaseUrl`** - used to specify the base Url in the ServiceWorker was not being used in the index `` link for **ServiceWorker.js** 7 | - Also @SQL-MisterMagoo 8 | - Added code to automatically add leading and trailing slashes to **Property** **`ManifestBaseUrl`** 9 | - Added code to automatically add leading and trailing slashes to **Property** **`ServiceWorkerBaseUrl`** 10 | 11 | #### 11/11/2019 12 | 13 | - Added new **Property** **`ServiceWorkerIgnoreHosts`** - used to prevent service worker installation on specific hosts e.g. localhost 14 | 15 | #### 16/10/2019 16 | 17 | - Added new **Property** **`BlazorProjectType`** - used to select the type of Blazor project (SSB/CSB). This property will auto-configure but can be overridden in your csproj file in cases where auto-configure is wrong. 18 | - Added new **ServiceWorkerPattern** value - "none" - primarily intended for SSB projects (where it is the default) - this service worker does nothing. 19 | - Removed **Property** **`ServiceWorkerIndexUrl`** 20 | - Added new **Property** **`ProjectIndexPage`** - used to identify the name of a _CSB_ index page - defaults to `index.html` 21 | - Added new **Property** **`ProjectHomePage`** - used to identify the name of a _SSB_ index page - defaults to `Pages\_Host.cshtml` 22 | - Added **`BlazorProjectType`** to build output. 23 | 24 | #### 10/08/2019 - 13/08/2019 25 | 26 | - Added new **Property** **`ServiceWorkerUpdateAlertText`** - used to change the default text in the "Update available alert". 27 | - Added new **Property** **`ServiceWorkerRegisterUpdateType`** - used to select the "Update available alert" type. 28 | - Added new **Property** **`ServiceWorkerRegisterUpdateTemplate`** - The name of the template file for the "update available" event. 29 | - Added new **Property** **`ServiceWorkerRegisterInstallableType`** - used to select the "Installable PWA alert" type. 30 | - Added new **Property** **`ServiceWorkerRegisterInstallableTemplate`** - The name of the template file for the "Installable PWA alert" event. 31 | - Added new **Property** **`ServiceWorkerRegisterBeforeInstallPromptType`** - used to select the "Before Install Prompt" type. 32 | - Added new **Property** **`ServiceWorkerRegisterBeforeInstallPromptTemplate`** - The name of the template file for the "Before Install Prompt" event. 33 | - Moved the "Update available alert" to it's own template so we can have alternates 34 | - Moved the "Installable PWA alert" to it's own template so we can have alternates 35 | - Moved the "Before Install Prompt" to it's own template so we can have alternates 36 | - Tidied up default install alert a bit 37 | - Added new Installable PWA Alert type - "installable-blazor" which will call a Blazor static method, which is defined by: 38 | - **`ServiceWorkerBlazorAssembly`** which is used to define the Blazor Assembly namespace - defaults to the project name 39 | - **`ServiceWorkerBlazorInstallMethod`** which is used to define the Blazor method to call - defaults to 'PWAInstallable' 40 | - Updated README with Blazor interop example. 41 | 42 | #### 08/08/2019 Initial Release -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Mister Magoo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README - nuget.md: -------------------------------------------------------------------------------- 1 | # Blazor PWA Builder - MSBuild 2 | 3 | The easiest way to turn your Server Side And Client Side Blazor projects into Progressive Web Apps (PWA). 4 | 5 | This project, when added to your build process, will generate the required files to enable basic PWA abilities for your project. 6 | 7 | It generates a manifest.json, a Service Worker with pre-caching of all required files (Blazor WebAssembly only) and an installer for the Service Worker. 8 | 9 | It currently includes a simple banner, by default, to notify the user that your application can be installed, and a simple alert to notify users when your application has been updated (CSB Only). 10 | 11 | This is now an official release - I am looking for feedback/issues/requests. 12 | 13 | ## Installation 14 | 15 | Install the nuget BlazorPWA.MsBuild 16 | 17 | #### Package Manager: 18 | `Install-Package BlazorPWA.MSBuild -Version 1.0.2` 19 | 20 | #### .NET Cli: 21 | `dotnet add package BlazorPWA.MSBuild --version 1.0.2` 22 | 23 | #### Package Reference 24 | `` 25 | 26 | ## Configuration 27 | 28 | Required configuration: none *. 29 | 30 | **Really! try it!* 31 | 32 | This PWA builder is constructed using MSBuild targets, which get installed in your .nuget cache. 33 | 34 | You are free to inspect those targets and customise any part of the build by adding Properties to your csproj, or to a **.props** file in the source tree. 35 | 36 | For example, by default, the PWA builder will only generate the PWA files once, which means, should you want to, you can add it to your project, build once - you will have the required files for a PWA and will be free to modify them to suit. 37 | 38 | Should you want to re-generate the PWA files every build, you can add this Property to your **csproj** 39 | 40 | **`true`** 41 | 42 | When you want to publish an update to your app, you may need to supply a new browser cache version - if you don't change anything in the Service Worker the end user will not use your new code! 43 | 44 | *Note: PWAs update when they detect a change in the Service Worker code - this cache version number is used in the Service Worker, so updating the cache version number will trigger an update of the PWA for the end user. It's also important as the cache is where your code is stored - if you don't update the cache version, it will not refresh with your new code* 45 | 46 | **`2`** 47 | 48 | The web manifest has properties for the application name, which are taken, by default, from your project and solution names, but you can override them 49 | 50 | **`My Project`** 51 | 52 | **`My Really Great Project`** 53 | 54 | There are dozens of Properties in the *targets* files supplied by this package - you *could* customise them all, but you probably don't need to, so proceed with caution. 55 | 56 | Please see the full README on GitHub for more details. 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blazor PWA Builder - MSBuild (no longer under development due to official support now being available) 2 | 3 | The easiest way to turn your Server Side And Client Side Blazor projects into Progressive Web Apps (PWA). 4 | 5 | This project, when added to your build process, will generate the required files to enable basic PWA abilities for your project. 6 | 7 | It generates a manifest.json, a Service Worker with pre-caching of all required files (Blazor WebAssembly only) and an installer for the Service Worker. 8 | 9 | It currently includes a simple banner, by default, to notify the user that your application can be installed, and a simple alert to notify users when your application has been updated (CSB Only). 10 | 11 | This is now an official release - I am looking for feedback/issues/requests. 12 | 13 | ### Browser compatibility 14 | 15 | Note that chromium browsers will prompt you to install a PWA on a desktop computer. 16 | Firefox does not currently provide this experience on desktop. 17 | I don't have a Mac, but I believe Safari does not prompt on desktop either. 18 | 19 | ## Installation 20 | 21 | Install the nuget BlazorPWA.MsBuild 22 | 23 | #### Package Manager: 24 | `Install-Package BlazorPWA.MSBuild -Version 1.0.2` 25 | 26 | #### .NET Cli: 27 | `dotnet add package BlazorPWA.MSBuild --version 1.0.2` 28 | 29 | #### Package Reference 30 | `` 31 | 32 | ## Configuration 33 | 34 | Required configuration: none *. 35 | 36 | **Really! try it!* 37 | 38 | This PWA builder is constructed using MSBuild targets, which get installed in your .nuget cache. 39 | 40 | You are free to inspect those targets and customise any part of the build by adding Properties to your csproj, or to a **.props** file in the source tree. 41 | 42 | For example, by default, the PWA builder will only generate the PWA files once, which means, should you want to, you can add it to your project, build once - you will have the required files for a PWA and will be free to modify them to suit. 43 | 44 | Should you want to re-generate the PWA files every build, you can add this Property to your **csproj** 45 | 46 | **`true`** 47 | 48 | When you want to publish an update to your app, you may need to supply a new browser cache version - if you don't change anything in the Service Worker the end user will not use your new code! 49 | 50 | *Note: PWAs update when they detect a change in the Service Worker code - this cache version number is used in the Service Worker, so updating the cache version number will trigger an update of the PWA for the end user. It's also important as the cache is where your code is stored - if you don't update the cache version, it will not refresh with your new code* 51 | 52 | **`2`** 53 | 54 | The web manifest has properties for the application name, which are taken, by default, from your project and solution names, but you can override them 55 | 56 | **`My Project`** 57 | 58 | **`My Really Great Project`** 59 | 60 | There are dozens of Properties in the *targets* files supplied by this package - you *could* customise them all, but you probably don't need to, so proceed with caution. 61 | 62 | ### Handing the "Installable PWA" event in Blazor 63 | 64 | When a Chromium based browser detects an installable PWA, it fires an event that your application 65 | can use to display a prompt to the user. 66 | 67 | The default for this package is to simply display a bar at the bottom of the 68 | browser window prompting the user to install your app. 69 | 70 | You can customize the message, or you can override that and pass the event 71 | over to your Blazor application - by setting the property 72 | **`ServiceWorkerRegisterInstallableType`** to **`installable-blazor`** in your **.csproj** 73 | 74 | For example: 75 | ``` 76 | 77 | installable-blazor 78 | true 79 | 80 | ``` 81 | 82 | This will generate code in your **ServiceWorkerRegister.js** to make an 83 | interop call to Blazor when the **beforeinstallprompt** event fires in the browser. 84 | 85 | **ServiceWorkerRegisterInstallableType** tells it you are using Blazor for the install prompt. 86 | **ServiceWorkerForce** just lets it update the generated files (remove it when you have it working). 87 | 88 | In your Blazor application, you will need code to handle this call 89 | - by default it will use the **ProjectName** and method name **"InstallPWA"** to 90 | perform a `DotNet.InvokeAsync()` 91 | - These values can be customised using the properties **`ServiceWorkerBlazorAssembly`** 92 | and **`ServiceWorkerBlazorInstallMethod`** 93 | 94 | Here is an example Blazor implementation which could be added to **MainLayout** 95 | 96 | ``` HTML 97 | @if (Installable) 98 | { 99 |
100 |

Install this app?

101 | 102 |
103 | } 104 | ``` 105 | This will display a bar at the bottom of the browser, which can be dismissed or clicked. 106 | 107 | 108 | The `code` section has the `JSInvokable` method **InstallPWS** that we called 109 | earlier from the browser and some supporting code to toggle the display and 110 | make an interop call back to the browser to trigger the app installation. 111 | ``` C# 112 | @code 113 | { 114 | [Inject] IJSRuntime JSRuntime { get; set; } 115 | static bool Installable = false; 116 | static Action ml; 117 | protected override void OnInitialized() 118 | { 119 | ml = () => InvokeAsync(StateHasChanged); 120 | } 121 | 122 | [JSInvokable("InstallPwaPrompt")] 123 | //a named method so that it is easy to call from JS Interop 124 | // with the following config 125 | // installable-blazor 126 | // TestPwa.Client 127 | // InstallPwaPrompt 128 | public static Task InstallPwaPrompt() 129 | { 130 | Installable = true; 131 | ml.Invoke(); 132 | return Task.CompletedTask; 133 | } 134 | async void InstallClicked(MouseEventArgs args) 135 | { 136 | Installable = false; 137 | await JSRuntime.InvokeAsync("BlazorPWA.installPWA");//this name comes from the nuget package that generates the pwa code, don't change it unless you change the generated code 138 | } 139 | } 140 | ``` 141 | ### Excluding files from the cache (WebAssembly projects) 142 | Due to the way Blazor WebAssembly projects build, some of the required properties we use to generate a PWA are not available until post-build. 143 | 144 | So, to ensure a smooth customisation, we recommend using a custom **Target** in your csproj (or as a separate file which you include in the build process). 145 | 146 | There are two **`Properties`** that you can customise to exclude files from the cache. 147 | 148 | - **`ServiceWorkerPreCacheExcludeFiles`** - by default this will exclude any files under `dist\_content` and any files in `WWWRoot` that start with a dot. 149 | File patterns listed here will be evaluated after build and excluded from the Service Worker cache. 150 | This property is used in both **Debug** and **Release** builds. 151 | 152 | 153 | - **`ServiceWorkerPreCacheExcludeReleaseFiles`** - by default this will exclude any `.pdb` files. This property is only used if you are building in **Release** mode. 154 | 155 | You can add extra files to these **`Properties`** by adding a custom target to your project 156 | 157 | ``` 158 | 159 | 160 | 161 | $(OutputPath)dist\_redist\**\*.*; 162 | 163 | 164 | 165 | 166 | ``` 167 | 168 | The `Name` can be anything you like, but is required by `MSBuild`. 169 | 170 | `$(OutputPath)dist` is the path to the distribution folder for a **Blazor WebAssembly** build. 171 | 172 | So, this example is excluding all files under the `_redist` folder from the Service Worker Cache. 173 | 174 | ### Prevent PWA installation / caching on localhost (or any host) 175 | 176 | If you want to exclude *localhost* (or any host) so that the developer inner loop is not affected, you can now use the **`ServiceWorkerIgnoreHosts`** property. 177 | 178 | Add it to you `csproj` file under `PropertyGroup` and list the hosts you want to exclude. 179 | 180 | ``` XML 181 | 182 | 'localhost','127.0.0.1','::1','KarensPC' 183 | 184 | ``` 185 | 186 | The service worker will not register itself when the `hostname` matches anything in the list. 187 | 188 | *Note: the single quotes around each hostname are required for now* 189 | 190 | ### Handle different base Urls for different configurations 191 | 192 | Sample csproj file for two different base Urls 193 | 194 | ``` XML 195 | 196 | 197 | 198 | netcoreapp3.1 199 | true 200 | true 201 | 202 | 203 | 204 | prod 205 | prod 206 | 207 | 208 | 209 | dev 210 | dev 211 | 212 | 213 | 214 | 215 | all 216 | runtime; build; native; contentfiles; analyzers; buildtransitive 217 | 218 | 219 | 220 | 221 | ``` 222 | This project file use **`ManifestForce`** and **`ServiceWorkerForce`** to ensure that the **manifest.json** and **ServiceWorker.js** files are rebuilt - otherwise they would not change when you changed configuration. 223 | 224 | ## Roadmap 225 | 226 | - [ ] At the moment, there is only one choice for caching strategy - Cache First/Network Fallback - I will add more (https://developers.google.com/web/ilt/pwa/introduction-to-progressive-web-app-architectures#caching_strategies_supported_by_sw-toolbox) 227 | - [x] The current method for alerting the user that the app is installable is semi-hard coded (you can adjust it manually after generation) - this will change to allow hooks/callbacks into Blazor via project properties 228 | - [x] The current method for alerting the user when an update is available is semi-hard coded (you can adjust it manually after generation) - this will change to allow hooks/callbacks into Blazor via project properties 229 | - [ ] Document all of the configuration Properties (they all have comments in the code - so you are able to understand their purpose without documentation...) 230 | - [ ] Bug fixes 231 | - [x] Allow hosts exclusions - so working on localhost can avoid PWA caching 232 | 233 | ## Contribute 234 | 235 | Please feel free to create issues, ask questions and submit Pull Requests. 236 | --------------------------------------------------------------------------------