Exemple #1
0
        public async Task <IActionResult> ServiceWorkerAsync()
        {
            Response.ContentType = "application/javascript; charset=utf-8";
            Response.Headers[HeaderNames.CacheControl] = $"max-age={_options.ServiceWorkerCacheControlMaxAge}";

            if (_options.Strategy == ServiceWorkerStrategy.CustomStrategy)
            {
                string js = _customServiceworker.GetCustomServiceworker(_options.CustomServiceWorkerStrategyFileName);
                return(Content(InsertStrategyOptions(js)));
            }

            else
            {
                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();

                    return(Content(InsertStrategyOptions(js)));
                }
            }
        }
        private static async Task ServiceWorkerHandlerAsync(HttpContext context, PwaOptions options, RetrieveCustomServiceworker customServiceworker)
        {
            context.Response.ContentType = "application/javascript; charset=utf-8";
            context.Response.Headers[HeaderNames.CacheControl] = $"max-age={options.ServiceWorkerCacheControlMaxAge}";

            if (options.Strategy == ServiceWorkerStrategy.CustomStrategy)
            {
                string js = customServiceworker.GetCustomServiceworker(options.CustomServiceWorkerStrategyFileName);
                await context.Response.WriteAsync(InsertStrategyOptions(js, options));
            }
            else
            {
                string   fileName       = options.Strategy + ".js";
                Assembly assembly       = typeof(ApplicationBuilderExtensions).Assembly;
                Stream   resourceStream = assembly.GetManifestResourceStream($"WebEssentials.AspNetCore.Pwa.ServiceWorker.Files.{fileName}");

                using (var reader = new StreamReader(resourceStream))
                {
                    string js = await reader.ReadToEndAsync();

                    await context.Response.WriteAsync(InsertStrategyOptions(js, options));
                }
            }
        }