private static string InsertStrategyOptions(string javascriptString, PwaOptions options)
 {
     return(javascriptString
            .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)
            .Replace("{ignoreRoutes}", string.Join(",", options.RoutesToIgnore.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(r => "'" + r.Trim() + "'"))));
 }
        public ServiceWorkerTagHelperComponent(IWebHostEnvironment env, IHttpContextAccessor accessor, PwaOptions options)
        {
            _env      = env;
            _accessor = accessor;
            _options  = options;

            _script = "\r\n\t<script" + (_options.EnableCspNonce ? Constants.CspNonce : string.Empty) + ">'serviceWorker'in navigator&&navigator.serviceWorker.register('" + options.BaseRoute + Constants.ServiceworkerRoute + "', { scope: '" + options.BaseRoute + "/' })</script>";
        }
        private static async Task WebManifestHandlerAsync(HttpContext context, PwaOptions options)
        {
            var wm = context.RequestServices.GetService <WebManifest>();

            if (wm == null)
            {
                throw new BadHttpRequestException("", 404);
            }

            context.Response.ContentType = "application/manifest+json; charset=utf-8";

            context.Response.Headers[HeaderNames.CacheControl] = $"max-age={options.WebManifestCacheControlMaxAge}";

            await context.Response.WriteAsync(wm.RawJson);
        }
        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));
                }
            }
        }
 /// <summary>
 /// Creates an instance of the controller.
 /// </summary>
 public PwaController(PwaOptions options)
 {
     _options = options;
 }
 public WebmanifestTagHelperComponent(PwaOptions options, IServiceProvider serviceProvider)
 {
     _options         = options;
     _link            = "\t<link rel=\"manifest\" href=\"" + options.BaseRoute + Constants.WebManifestRoute + "\" />\r\n";
     _serviceProvider = serviceProvider;
 }
Example #7
0
 public WebmanifestTagHelperComponent(PwaOptions options, IServiceProvider serviceProvider)
 {
     _options         = options;
     _serviceProvider = serviceProvider;
 }
Example #8
0
 /// <summary>
 /// Creates an instance of the controller.
 /// </summary>
 public PwaController(PwaOptions options, RetrieveCustomServiceworker customServiceworker)
 {
     _options             = options;
     _customServiceworker = customServiceworker;
 }
Example #9
0
 public ServiceWorkerTagHelperComponent(IHostingEnvironment env, IHttpContextAccessor accessor, PwaOptions options)
 {
     _env      = env;
     _accessor = accessor;
     _options  = options;
 }