public PrplMiddleware(RequestDelegate next, string root, IOptions <PrplConfiguration> options, IHostingEnvironment env)
 {
     _next   = next;
     _root   = Path.Combine(env.ContentRootPath, root);
     _config = options.Value;
     _builds = LoadBuilds();
 }
Пример #2
0
 static Build[] LoadBuilds(string root, PrplConfiguration config)
 {
     return(config.Builds.Select((b, i) => new Build(Path.Combine(root, b.Name), root)
     {
         ConfigOrder = i,
         Name = b.Name,
         Requirements = new HashSet <BrowserCapability>(b.BrowserCapabilities ?? new BrowserCapability[] { }),
         EntryPoint = b.EntryPoint,
     }).OrderBy(bb => bb).ToArray());
 }
Пример #3
0
        static Action <StaticFileResponseContext> PrepareStaticFiles(PrplConfiguration config, Build build)
        {
            return((StaticFileResponseContext ctx) =>
            {
                if (ctx.File.Name.EndsWith("service-worker.js", StringComparison.Ordinal))
                {
                    // A service worker may only register with a scope above its own path if
                    // permitted by this header.
                    // https://www.w3.org/TR/service-workers-1/#service-worker-allowed
                    ctx.Context.Response.Headers["service-worker-allowed"] = "/";
                }

                // Don't set the Cache-Control header if it's already set. This way another
                // middleware can control caching, and we won't touch it.
                if (!ctx.Context.Response.Headers["Cache-Control"].Any())
                {
                    ctx.Context.Response.Headers["Cache-Control"] = ctx.File.Name == build.EntryPoint ? "max-age=0"
                        : config.CacheControl;
                }

                if (build.PushManifest != null)
                {
                    var urlPath = ctx.Context.Request.Path;

                    var linkHeaders = new List <string>();

                    var appRouteFeature = ctx.Context.Features.Get <ApplicationRouteFeature>();

                    if (appRouteFeature != null && !string.IsNullOrEmpty(appRouteFeature.Path))
                    {
                        // Also check the filename against the push manifest. In the case of
                        // the entrypoint, these will be different (e.g. "/my/app/route" vs
                        // "/es2015/index.html"), and we want to support configuring pushes in
                        // terms of both.
                        linkHeaders.AddRange(build.PushManifest.LinkHeaders(appRouteFeature.Path));
                    }

                    linkHeaders.AddRange(build.PushManifest.LinkHeaders(urlPath));

                    ctx.Context.Response.Headers.Add("Link", linkHeaders.ToArray());
                }
            });
        }