src/PwaController.cs
/// <summary>
/// Serves a service worker based on the provided settings.
/// </summary>
Serves a service worker based on the provided settings.
Serves a service worker based on the provided settings .
[Route(Constants.ServiceworkerRoute)]
        [HttpGet]
        public async Task<IActionResult> ServiceWorkerAsync()
        {
            Response.ContentType = "application/javascript; charset=utf-8";
            Response.Headers[HeaderNames.CacheControl] = $"max-age={_options.ServiceWorkerCacheControlMaxAge}";
            string fileName = _options.Strategy + ".js";
            Assembly assembly = typeof(PwaController).Assembly;
            Stream resourceStream = assembly.GetManifestResourceStream($"WebEssentials.AspNetCore.Pwa.ServiceWorker.Files.{fileName}");
            using (var reader = new StreamReader(resourceStream))
            {
                string js = await reader.ReadToEndAsync();
                string modified = js
                    .Replace("{version}", _options.CacheId + "::" + _options.Strategy)
                    .Replace("{routes}", string.Join(",", _options.RoutesToPreCache.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(r => "'" + r.Trim() + "'")))
                    .Replace("{offlineRoute}", _options.BaseRoute + _options.OfflineRoute);
                return Content(modified);
            }
        }
[ Route ( Constants . ServiceworkerRoute ) ] [ HttpGet ] public async Task < IActionResult > ServiceWorkerAsync ( ) { Response . ContentType = " " ; Response . Headers [ HeaderNames . CacheControl ] = $" max-age= { _options . ServiceWorkerCacheControlMaxAge } " ; string fileName = _options . Strategy + " " ; Assembly assembly = typeof ( PwaController ) . Assembly ; Stream resourceStream = assembly . GetManifestResourceStream ( $" WebEssentials.AspNetCore.Pwa.ServiceWorker.Files. { fileName } " ) ; using ( var reader = new StreamReader ( resourceStream ) ) { string js = await reader . ReadToEndAsync ( ) ; string modified = js . Replace ( " " , _options . CacheId + " " + _options . Strategy ) . Replace ( " " , string . Join ( " " , _options . RoutesToPreCache . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) . Select ( r => " " + r . Trim ( ) + " " ) ) ) . Replace ( " " , _options . BaseRoute + _options . OfflineRoute ) ; return Content ( modified ) ; } }
Serves a service worker based on the provided settings.
Serves a service worker based on the provided settings .