Example #1
0
 /// <summary>
 /// Creates an instance of the controller.
 /// </summary>
 public PwaController(PwaOptions options, RetrieveCustomServiceworker customServiceworker)
 {
     _options             = options;
     _customServiceworker = customServiceworker;
 }
        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));
                }
            }
        }