Пример #1
0
        /// <summary>
        /// Return the ES5 Cache Store folder
        /// </summary>
        /// <returns></returns>
        public static string GetES5CacheStoreFolder()
        {
            if (!string.IsNullOrEmpty(_computedCacheFolder))
            {
                return(_computedCacheFolder);
            }

            string basePath = ECMAScript5Middleware.GetWebHostEnvironment().WebRootPath;

            if (string.IsNullOrEmpty(basePath))
            {
                throw new InvalidOperationException($"BlazorPolyfill: Unable to retrieve the ES5 Cache Store folder. {nameof(IWebHostEnvironment)}.{nameof(IWebHostEnvironment.WebRootPath)} returned null, but is needed for computing the current cache store");
            }

            _computedCacheFolder = Path.Combine(basePath, ES5CacheFolderName);
            if (!Directory.Exists(_computedCacheFolder))
            {
                Directory.CreateDirectory(_computedCacheFolder);
            }

            return(_computedCacheFolder);
        }
Пример #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);
        }