Exemplo n.º 1
0
        public static IApplicationBuilder UseBlazorPolyfill(
            this IApplicationBuilder builder)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            InitReact(builder);

            //This is a kind of hack for caching the data at boot
            //It's better to prevent anything at first request by caching instead of
            //making the user wait the file generation
            builder.Use((context, next) =>
            {
                if (!IsBlazorPolyfillLibCached())
                {
                    //Avoiding first client to await the file generation
                    CacheBlazorPolyfillLib();
                }

                //Normal behavior
                return(next());
            });

            builder.MapWhen(ctx =>
                            ctx.Request.BrowserNeedES5Fallback() &&
                            ctx.Request.Path.StartsWithSegments("/_framework") &&
                            ctx.Request.Path.StartsWithSegments("/_framework/blazor.server.js"),
                            subBuilder =>
            {
                subBuilder.Run(async(context) =>
                {
                    var fileContent = GetPatchedBlazorServerFile();
                    await HttpRequestManager.ManageRequest(context, fileContent);
                });
            });

            //Should return the resource file if IE11
            builder.MapWhen(ctx =>
                            ctx.Request.Path.StartsWithSegments("/_framework") &&
                            (
                                ctx.Request.Path.StartsWithSegments("/_framework/blazor.polyfill.js") ||
                                ctx.Request.Path.StartsWithSegments("/_framework/blazor.polyfill.min.js")
                            ),
                            subBuilder =>
            {
                subBuilder.Run(async(context) =>
                {
                    //Eval if the requested file is the minified version or not
                    bool isMinified = context.Request.Path.StartsWithSegments("/_framework/blazor.polyfill.min.js");

                    var fileContent = GetIE11BlazorPolyfill(context.Request.BrowserNeedES5Fallback(), isMinified);
                    await HttpRequestManager.ManageRequest(context, fileContent);
                });
            });

            return(builder);
        }
Exemplo n.º 2
0
        public static IApplicationBuilder UseBlazorPolyfill(
            this IApplicationBuilder builder, BlazorPolyfillOptions options)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            SetOptions(options);

            InitReact(builder);

            //This is a kind of hack for caching the data at boot
            //It's better to prevent anything at first request by caching instead of
            //making the user wait the file generation
            builder.Use((context, next) =>
            {
                if (!IsBlazorPolyfillLibCached())
                {
                    //Avoiding first client to await the file generation
                    CacheBlazorPolyfillLib();
                }

                //Normal behavior
                return(next());
            });

            //This is used in order to transpile dynamically StaticFiles to ES5 when needed
            builder.UseMiddleware <ECMAScript5Middleware>();

            //BrowserNeedES5Fallback is written hiere and not in the builder
            //because we only want to use MapWhen when we need ES5 fallback.
            //If this is false, the request will be redirected to the Microsoft
            //default request management for this file.
            builder.MapWhen(ctx =>
                            ECMAScript5Middleware.RequestedFileIsBlazorServerJS(ctx.Request) &&
                            ctx.Request.BrowserNeedES5Fallback(),
                            subBuilder =>
            {
                subBuilder.Run(async(context) =>
                {
                    var fileContent = GetPatchedBlazorServerFile();
                    await HttpRequestManager.ManageRequest(context, fileContent);
                });
            });

            //As blazor.polyfill.js files does not exist really on theses path and
            //does not have a real fallback request mangement (as for blazor.server.js)
            //we should intercept them at any time with MapWhen, but change the result
            //behavior lately in the builder. Otherwise this would return a 404 error.
            builder.MapWhen(ctx =>
                            ECMAScript5Middleware.RequestedFileIsBlazorPolyfill(ctx.Request, out bool isMinified),
                            subBuilder =>
            {
                subBuilder.Run(async(context) =>
                {
                    ECMAScript5Middleware.RequestedFileIsBlazorPolyfill(context.Request, out bool isMinified);

                    //Eval if the requested file is the minified version or not
                    var fileContent = GetBlazorPolyfillFile(context.Request.BrowserNeedES5Fallback(), isMinified);
                    await HttpRequestManager.ManageRequest(context, fileContent);
                });
            });

            return(builder);
        }