@@ -161,6 +161,7 @@ public override bool Execute()
161
161
GeneratedAOTProfile ( ) ;
162
162
GenerateIndexHtml ( ) ;
163
163
GenerateEmbeddedJs ( ) ;
164
+ RemoveDuplicateAssets ( ) ;
164
165
GenerateConfig ( ) ;
165
166
RemoveDuplicateAssets ( ) ;
166
167
}
@@ -184,7 +185,7 @@ private void RemoveDuplicateAssets()
184
185
foreach ( var existingAsset in existingAssets )
185
186
{
186
187
Log . LogMessage ( MessageImportance . Low , $ "Existing asset to remove [{ existingAsset . ItemSpec } ]") ;
187
- }
188
+ }
188
189
189
190
// remove existingAssets from StaticWebContent
190
191
StaticWebContent = StaticWebContent
@@ -342,7 +343,29 @@ private void ExtractAdditionalJS()
342
343
{
343
344
_dependencies . Add ( name ) ;
344
345
345
- CopyResourceToOutput ( name , resource ) ;
346
+ if (
347
+ // Process the service worker file separately to adjust its contents
348
+ source . Name . Name . Equals ( GetType ( ) . Assembly . GetName ( ) . Name , StringComparison . OrdinalIgnoreCase )
349
+ && name . Equals ( "service-worker.js" , StringComparison . OrdinalIgnoreCase ) )
350
+ {
351
+ using var resourceStream = resource . GetResourceStream ( ) ;
352
+ using var reader = new StreamReader ( resourceStream ) ;
353
+
354
+ var worker = TouchServiceWorker ( reader . ReadToEnd ( ) ) ;
355
+ var memoryStream = new MemoryStream ( ) ;
356
+
357
+ using var writer = new StreamWriter ( memoryStream , Encoding . UTF8 ) ;
358
+ writer . Write ( worker ) ;
359
+ writer . Flush ( ) ;
360
+
361
+ memoryStream . Position = 0 ;
362
+
363
+ CopyStreamToOutput ( name , memoryStream ) ;
364
+ }
365
+ else
366
+ {
367
+ CopyResourceToOutput ( name , resource ) ;
368
+ }
346
369
347
370
Log . LogMessage ( $ "Additional JS { name } ") ;
348
371
}
@@ -431,6 +454,18 @@ private void CopyResourceToOutput(string name, EmbeddedResource resource)
431
454
AddStaticAsset ( name , dest ) ;
432
455
}
433
456
457
+ private void CopyStreamToOutput ( string name , Stream stream )
458
+ {
459
+ var dest = Path . Combine ( _intermediateAssetsPath , name ) ;
460
+
461
+ using ( var destStream = new FileStream ( dest , FileMode . Create , FileAccess . Write ) )
462
+ {
463
+ stream . CopyTo ( destStream ) ;
464
+ }
465
+
466
+ AddStaticAsset ( name , dest ) ;
467
+ }
468
+
434
469
private void AddStaticAsset ( string targetPath , string filePath , bool overrideExisting = false )
435
470
{
436
471
var contentRoot = targetPath . StartsWith ( _intermediateAssetsPath )
@@ -539,7 +574,14 @@ private void GenerateConfig()
539
574
var config = new StringBuilder ( ) ;
540
575
541
576
var enablePWA = ! string . IsNullOrEmpty ( PWAManifestFile ) ;
542
- //var offlineFiles = enablePWA ? string.Join(", ", GetPWACacheableFiles().Select(f => $"\".{f}\"")) : "";
577
+
578
+ var sanitizedOfflineFiles = StaticWebContent
579
+ . Select ( f => f . GetMetadata ( "Link" )
580
+ . Replace ( "\\ " , "/" )
581
+ . Replace ( "wwwroot/" , "" ) )
582
+ . Concat ( [ $ "uno-config.js", "_framework/blazor.boot.json" , "." ] ) ;
583
+
584
+ var offlineFiles = enablePWA ? string . Join ( ", " , sanitizedOfflineFiles . Select ( f => $ "\" { WebAppBasePath } { f } \" ") ) : "" ;
543
585
544
586
var emccExportedRuntimeMethodsParams = string . Join (
545
587
"," ,
@@ -564,7 +606,7 @@ private void GenerateConfig()
564
606
config . AppendLine ( $ "config.uno_dependencies = [{ dependencies } ];") ;
565
607
config . AppendLine ( $ "config.uno_runtime_options = [{ runtimeOptionsSet } ];") ;
566
608
config . AppendLine ( $ "config.enable_pwa = { enablePWA . ToString ( ) . ToLowerInvariant ( ) } ;") ;
567
- // config.AppendLine($"config.offline_files = ['{WebAppBasePath}', {offlineFiles}];");
609
+ config . AppendLine ( $ "config.offline_files = ['{ WebAppBasePath } ', { offlineFiles } ];") ;
568
610
config . AppendLine ( $ "config.uno_shell_mode = \" { _shellMode } \" ;") ;
569
611
config . AppendLine ( $ "config.uno_debugging_enabled = { ( ! Optimize ) . ToString ( ) . ToLowerInvariant ( ) } ;") ;
570
612
config . AppendLine ( $ "config.uno_enable_tracing = { EnableTracing . ToString ( ) . ToLowerInvariant ( ) } ;") ;
@@ -686,8 +728,7 @@ private void GeneratePWAContent(StringBuilder extraBuilder)
686
728
687
729
extraBuilder . AppendLine ( $ "<link rel=\" manifest\" href=\" $(WEB_MANIFEST)\" />") ;
688
730
689
- // See https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
690
- extraBuilder . AppendLine ( $ "<meta name=\" apple-mobile-web-app-capable\" content=\" yes\" >") ;
731
+ extraBuilder . AppendLine ( $ "<meta name=\" mobile-web-app-capable\" content=\" yes\" >") ;
691
732
692
733
if ( manifestDocument [ "icons" ] is JArray array
693
734
&& array . Where ( v => v [ "sizes" ] ? . Value < string > ( ) == "1024x1024" ) . FirstOrDefault ( ) is JToken img )
@@ -720,7 +761,11 @@ string s when s.StartsWith("./") => $"{WebAppBasePath}/" + s.Substring(2),
720
761
}
721
762
}
722
763
723
- AddStaticAsset ( Path . GetFileName ( PWAManifestFile ) , PWAManifestFile ! ) ;
764
+ var pwaManifestFileName = Path . GetFileName ( PWAManifestFile ) ;
765
+ var pwaManifestOutputPath = Path . Combine ( _intermediateAssetsPath , pwaManifestFileName ) ;
766
+ File . WriteAllText ( pwaManifestOutputPath , manifestDocument . ToString ( ) ) ;
767
+
768
+ AddStaticAsset ( Path . GetFileName ( PWAManifestFile ) , pwaManifestOutputPath ) ;
724
769
}
725
770
}
726
771
@@ -860,6 +905,14 @@ private void GenerateEmbeddedJs()
860
905
AddStaticAsset ( "index.html" , htmlPath ) ;
861
906
}
862
907
908
+ private string TouchServiceWorker ( string workerBody )
909
+ {
910
+ workerBody = workerBody . Replace ( "$(CACHE_KEY)" , Guid . NewGuid ( ) . ToString ( ) ) ;
911
+ workerBody = workerBody . Replace ( "$(REMOTE_WEBAPP_PATH)" , WebAppBasePath ) ;
912
+
913
+ return workerBody ;
914
+ }
915
+
863
916
private string TryConvertLongPath ( string path )
864
917
=> Environment . OSVersion . Platform == PlatformID . Win32NT
865
918
&& ! string . IsNullOrEmpty ( path )
0 commit comments