Esempio n. 1
0
        private void RenderScriptReferences()
        {
            // Get scripts that are added by the framework
            var frameworkScriptPaths = GetFrameworkScripts();

            // Construct smart list
            var smartList = new List <string>();

            // Hard-code WebForms.js - it will be rendered here, and not in Page (like by default)
            smartList.Add(GetUrl(new ScriptReference(this.Page.ClientScript.GetWebResourceUrl(typeof(System.Web.UI.Page), "WebForms.js"))));
            // Add scripts needed by the framework
            smartList.AddRange(frameworkScriptPaths);
            // Add scripts previously added to this control
            smartList.AddRange(this.Scripts.Select(s => GetUrl(s)));
            // Add scripts from the smart loader
            smartList.AddRange(SmartLoader.GetScriptsToLoad());

            // Clear previous scripts (they are now part of smartList)
            Scripts.Clear();

            // Initialize bundling
            var allowJsBundling = PortalContext.Current.BundleOptions.AllowJsBundling;
            var bundle          = allowJsBundling ? new JsBundle() : null;

            // Go through all scripts
            foreach (var spath in smartList)
            {
                var lower = spath.ToLower();

                if (lower.EndsWith(".css"))
                {
                    UITools.AddStyleSheetToHeader(UITools.GetHeader(), spath);
                }
                else
                {
                    var isPostponed       = PortalBundleOptions.JsIsBlacklisted(spath);
                    var isFrameworkScript = frameworkScriptPaths.Contains(spath);

                    if (isPostponed)
                    {
                        _postponedList.Add(spath);
                    }
                    if (allowJsBundling && !isPostponed)
                    {
                        bundle.AddPath(spath);
                    }
                    if (!isPostponed && !isFrameworkScript)
                    {
                        Scripts.Add(new ScriptReference(spath));
                    }
                }
            }

            // Go through postponed scripts
            foreach (var spath in _postponedList)
            {
                Scripts.Add(new ScriptReference(spath));
            }

            // NOTE: At this point, script order is the following:
            // 1) scripts added by the framework
            // 2) scripts added directly to this control
            // 3) scripts from SmartLoader (that are not blacklisted from bundling)
            // 4) scripts from SmartLoader (that are blacklisted from bundling)

            // Finalize bundling
            if (allowJsBundling)
            {
                // If bundling is allowed, close the bundle and process it
                bundle.Close();
                BundleHandler.AddBundleIfNotThere(bundle);
                ThreadPool.QueueUserWorkItem(x => BundleHandler.AddBundleToCache(bundle));

                if (BundleHandler.IsBundleInCache(bundle))
                {
                    // If the bundle is complete, use its path to replace the path of all the scripts that are not postponed
                    _bundle = bundle;
                }
            }
        }
Esempio n. 2
0
        private void RenderScriptReferences()
        {
            var           smartList       = SmartLoader.GetScriptsToLoad();
            var           allowJsBundling = PortalContext.Current.BundleOptions.AllowJsBundling;
            JsBundle      bundle          = null;
            List <string> postponedList   = null;

            if (allowJsBundling)
            {
                bundle        = new JsBundle();
                postponedList = new List <string>();
            }

            foreach (var spath in smartList)
            {
                var lower = spath.ToLower();

                if (lower.EndsWith(".css"))
                {
                    UITools.AddStyleSheetToHeader(UITools.GetHeader(), spath);
                }
                else
                {
                    if (allowJsBundling)
                    {
                        // If bundling is allowed, add the path to the bundle - if it is not blacklisted
                        if (PortalBundleOptions.JsIsBlacklisted(spath))
                        {
                            postponedList.Add(spath);
                        }
                        else
                        {
                            bundle.AddPath(spath);
                        }
                    }
                    else
                    {
                        // If bundling is disabled, fall back to the old behaviour
                        var sref = new ScriptReference(spath);
                        Scripts.Add(sref);
                    }
                }
            }

            if (allowJsBundling)
            {
                // If bundling is allowed, closing the bundle and adding it as a single script reference
                bundle.Close();
                BundleHandler.AddBundleIfNotThere(bundle);
                ThreadPool.QueueUserWorkItem(x => BundleHandler.AddBundleToCache(bundle));

                if (BundleHandler.IsBundleInCache(bundle))
                {
                    // If the bundle is complete, add it as a single script reference
                    var sref = new ScriptReference("/sn-bundles/" + bundle.FakeFilename);
                    Scripts.Add(sref);
                }
                else
                {
                    // The bundle will be complete in a few seconds; disallow caching the page until then
                    this.Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

                    // Fallback to adding every script again as separate script references
                    foreach (var path in bundle.Paths)
                    {
                        var sref = new ScriptReference(path);
                        Scripts.Add(sref);
                    }
                }

                //add blacklisted js path's to the script collection individually
                foreach (var path in postponedList)
                {
                    Scripts.Add(new ScriptReference(path));
                }
            }
        }