diff --git a/lib/amd/build/prefetch.min.js b/lib/amd/build/prefetch.min.js index 94652dc48f7..95f70ab1cf8 100644 --- a/lib/amd/build/prefetch.min.js +++ b/lib/amd/build/prefetch.min.js @@ -1,2 +1,2 @@ -define ("core/prefetch",["exports","core/config"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;b=function(a){return a&&a.__esModule?a:{default:a}}(b);var i="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function c(a){return g(a)||f(a)||e(a)||d()}function d(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function e(a,b){if(!a)return;if("string"==typeof a)return h(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);if("Object"===c&&a.constructor)c=a.constructor.name;if("Map"===c||"Set"===c)return Array.from(c);if("Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c))return h(a,b)}function f(a){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(a))return Array.from(a)}function g(a){if(Array.isArray(a))return h(a)}function h(a,b){if(null==b||b>a.length)b=a.length;for(var c=0,d=Array(b);c=b.default.jsrev){return}if(m){return}if(j){n();return}m=setTimeout(function(){j=!0;m=null;("function"==typeof i.define&&i.define.amd?new Promise(function(a,c){i.require([b.default.iconsystemmodule],a,c)}):"undefined"!=typeof module&&module.exports&&"undefined"!=typeof require||"undefined"!=typeof module&&module.component&&i.require&&"component"===i.require.loader?Promise.resolve(require((b.default.iconsystemmodule))):Promise.resolve(i[b.default.iconsystemmodule])).then(function(a){var b=new a;q(b.getTemplateName());return b}).then(function(a){n();a.init()}).catch()},500)},p=function(a){k=k.concat(a);o()},q=function(a){return p([a])},r=function(a,b){if(!l[a]){l[a]=[]}l[a]=l[a].concat(b);o()},s=function(a,b){if(!l[a]){l[a]=[]}l[a].push(b);o()};p([].concat(["core/loading"],["core/modal"],["core/modal_backdrop"]));r("core",["cancel","closebuttontitle","loading","savechanges"]);r("core_form",["showless","showmore"]);a.default={prefetchTemplate:q,prefetchTemplates:p,prefetchString:s,prefetchStrings:r};return a.default}); +define ("core/prefetch",["exports","core/config"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;b=function(a){return a&&a.__esModule?a:{default:a}}(b);var i="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function c(a){return g(a)||f(a)||e(a)||d()}function d(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function e(a,b){if(!a)return;if("string"==typeof a)return h(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);if("Object"===c&&a.constructor)c=a.constructor.name;if("Map"===c||"Set"===c)return Array.from(c);if("Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c))return h(a,b)}function f(a){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(a))return Array.from(a)}function g(a){if(Array.isArray(a))return h(a)}function h(a,b){if(null==b||b>a.length)b=a.length;for(var c=0,d=Array(b);c.\n\n/**\n * Prefetch module to help lazily load content for use on the current page.\n *\n * @module core/prefetch\n * @class prefetch\n * @package core\n * @copyright 2020 Andrew Nicols \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport Config from 'core/config';\n\n// Keep track of whether the initial prefetch has occurred.\nlet initialPrefetchComplete = false;\n\n// Prefetch templates.\nlet templateList = [];\n\n// Prefetch strings.\nlet stringList = {};\n\nlet prefetchTimer;\n\n/**\n * Fetch all queued items in the queue.\n *\n * Should only be called via processQueue.\n */\nconst fetchQueue = () => {\n // Prefetch templates.\n if (templateList) {\n const templatesToLoad = templateList.slice();\n templateList = [];\n import('core/templates')\n .then(Templates => Templates.prefetchTemplates(templatesToLoad))\n .catch();\n }\n\n // Prefetch strings.\n const mappedStringsToFetch = stringList;\n stringList = {};\n\n const stringsToFetch = [];\n Object.keys(mappedStringsToFetch).forEach(component => {\n stringsToFetch.push(...mappedStringsToFetch[component].map(key => {\n return {component, key};\n }));\n });\n\n if (stringsToFetch) {\n import('core/str')\n .then(Str => Str.get_strings(stringsToFetch))\n .catch();\n }\n};\n\n/**\n * Process the prefetch queues as required.\n *\n * The initial call will queue the first fetch after a delay.\n * Subsequent fetches are immediate.\n */\nconst processQueue = () => {\n if (Config.jsrev <= 0) {\n // No point pre-fetching when cachejs is disabled as we do not store anything in the cache anyway.\n return;\n }\n\n if (prefetchTimer) {\n // There is a live prefetch timer. The initial prefetch has been scheduled but is not complete.\n return;\n }\n\n // The initial prefetch has compelted. Just queue as normal.\n if (initialPrefetchComplete) {\n fetchQueue();\n\n return;\n }\n\n // Queue the initial prefetch in a short while.\n prefetchTimer = setTimeout(() => {\n initialPrefetchComplete = true;\n prefetchTimer = null;\n\n // Ensure that the icon system is loaded.\n // This can be quite slow and delay UI interactions if it is loaded on demand.\n import(Config.iconsystemmodule)\n .then(IconSystem => {\n const iconSystem = new IconSystem();\n prefetchTemplate(iconSystem.getTemplateName());\n\n return iconSystem;\n })\n .then(iconSystem => {\n fetchQueue();\n iconSystem.init();\n\n return;\n })\n .catch();\n }, 500);\n};\n\n/**\n * Add a set of templates to the prefetch queue.\n *\n * @param {Array} templatesNames\n */\nconst prefetchTemplates = templatesNames => {\n templateList = templateList.concat(templatesNames);\n\n processQueue();\n};\n\n/**\n * Add a single template to the prefetch queue.\n *\n * @param {String} templateName\n * @returns {undefined}\n */\nconst prefetchTemplate = templateName => prefetchTemplates([templateName]);\n\n/**\n * Add a set of strings from the same component to the prefetch queue.\n *\n * @param {String} component\n * @param {String[]} keys\n */\nconst prefetchStrings = (component, keys) => {\n if (!stringList[component]) {\n stringList[component] = [];\n }\n\n stringList[component] = stringList[component].concat(keys);\n\n processQueue();\n};\n\n/**\n * Add a single string to the prefetch queue.\n *\n * @param {String} component\n * @param {String} key\n */\nconst prefetchString = (component, key) => {\n if (!stringList[component]) {\n stringList[component] = [];\n }\n\n stringList[component].push(key);\n\n processQueue();\n};\n\n// Prefetch some commonly-used templates.\nprefetchTemplates([].concat(\n ['core/loading'],\n ['core/modal'],\n ['core/modal_backdrop'],\n));\n\n// And some commonly used strings.\nprefetchStrings('core', [\n 'cancel',\n 'closebuttontitle',\n 'loading',\n 'savechanges',\n]);\nprefetchStrings('core_form', [\n 'showless',\n 'showmore',\n]);\n\nexport default {\n prefetchTemplate,\n prefetchTemplates,\n prefetchString,\n prefetchStrings,\n};\n"],"file":"prefetch.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/prefetch.js"],"names":["initialPrefetchComplete","templateList","stringList","prefetchTimer","fetchQueue","templatesToLoad","slice","then","Templates","prefetchTemplates","catch","mappedStringsToFetch","stringsToFetch","Object","keys","forEach","component","push","map","key","Str","get_strings","processQueue","setTimeout","Config","iconsystemmodule","IconSystem","iconSystem","prefetchTemplate","getTemplateName","init","templatesNames","concat","templateName","prefetchStrings","prefetchString"],"mappings":"+IAwBA,uD,+1BAGIA,CAAAA,CAAuB,G,CAGvBC,CAAY,CAAG,E,CAGfC,CAAU,CAAG,E,CAEbC,C,CAOEC,CAAU,CAAG,UAAM,CAErB,GAAIH,CAAJ,CAAkB,CACd,GAAMI,CAAAA,CAAe,CAAGJ,CAAY,CAACK,KAAb,EAAxB,CACAL,CAAY,CAAG,EAAf,CACA,mSAAO,gBAAP,0CACCM,IADD,CACM,SAAAC,CAAS,QAAIA,CAAAA,CAAS,CAACC,iBAAV,CAA4BJ,CAA5B,CAAJ,CADf,EAECK,KAFD,EAGH,CAGD,GAAMC,CAAAA,CAAoB,CAAGT,CAA7B,CACAA,CAAU,CAAG,EAAb,CAEA,GAAMU,CAAAA,CAAc,CAAG,EAAvB,CACAC,MAAM,CAACC,IAAP,CAAYH,CAAZ,EAAkCI,OAAlC,CAA0C,SAAAC,CAAS,CAAI,CACnDJ,CAAc,CAACK,IAAf,OAAAL,CAAc,GAASD,CAAoB,CAACK,CAAD,CAApB,CAAgCE,GAAhC,CA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c,CAAD,CAAV,CAAwBd,CAAU,CAACc,CAAD,CAAV,CAAsBgB,MAAtB,CAA6BlB,CAA7B,CAAxB,CAEAQ,CAAY,EACf,C,CAQKa,CAAc,CAAG,SAACnB,CAAD,CAAYG,CAAZ,CAAoB,CACvC,GAAI,CAACjB,CAAU,CAACc,CAAD,CAAf,CAA4B,CACxBd,CAAU,CAACc,CAAD,CAAV,CAAwB,EAC3B,CAEDd,CAAU,CAACc,CAAD,CAAV,CAAsBC,IAAtB,CAA2BE,CAA3B,EAEAG,CAAY,EACf,C,CAGDb,CAAiB,CAAC,GAAGuB,MAAH,CACd,CAAC,cAAD,CADc,CAEd,CAAC,YAAD,CAFc,CAGd,CAAC,qBAAD,CAHc,CAAD,CAAjB,CAOAE,CAAe,CAAC,MAAD,CAAS,CACpB,QADoB,CAEpB,kBAFoB,CAGpB,SAHoB,CAIpB,aAJoB,CAAT,CAAf,CAMAA,CAAe,CAAC,WAAD,CAAc,CACzB,UADyB,CAEzB,UAFyB,CAAd,CAAf,C,UAKe,CACXN,gBAAgB,CAAhBA,CADW,CAEXnB,iBAAiB,CAAjBA,CAFW,CAGX0B,cAAc,CAAdA,CAHW,CAIXD,eAAe,CAAfA,CAJW,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Prefetch module to help lazily load content for use on the current page.\n *\n * @module core/prefetch\n * @class prefetch\n * @package core\n * @copyright 2020 Andrew Nicols \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport Config from 'core/config';\n\n// Keep track of whether the initial prefetch has occurred.\nlet initialPrefetchComplete = false;\n\n// Prefetch templates.\nlet templateList = [];\n\n// Prefetch strings.\nlet stringList = {};\n\nlet prefetchTimer;\n\n/**\n * Fetch all queued items in the queue.\n *\n * Should only be called via processQueue.\n */\nconst fetchQueue = () => {\n // Prefetch templates.\n if (templateList) {\n const templatesToLoad = templateList.slice();\n templateList = [];\n import('core/templates')\n .then(Templates => Templates.prefetchTemplates(templatesToLoad))\n .catch();\n }\n\n // Prefetch strings.\n const mappedStringsToFetch = stringList;\n stringList = {};\n\n const stringsToFetch = [];\n Object.keys(mappedStringsToFetch).forEach(component => {\n stringsToFetch.push(...mappedStringsToFetch[component].map(key => {\n return {component, key};\n }));\n });\n\n if (stringsToFetch) {\n import('core/str')\n .then(Str => Str.get_strings(stringsToFetch))\n .catch();\n }\n};\n\n/**\n * Process the prefetch queues as required.\n *\n * The initial call will queue the first fetch after a delay.\n * Subsequent fetches are immediate.\n */\nconst processQueue = () => {\n if (prefetchTimer) {\n // There is a live prefetch timer. The initial prefetch has been scheduled but is not complete.\n return;\n }\n\n // The initial prefetch has compelted. Just queue as normal.\n if (initialPrefetchComplete) {\n fetchQueue();\n\n return;\n }\n\n // Queue the initial prefetch in a short while.\n prefetchTimer = setTimeout(() => {\n initialPrefetchComplete = true;\n prefetchTimer = null;\n\n // Ensure that the icon system is loaded.\n // This can be quite slow and delay UI interactions if it is loaded on demand.\n import(Config.iconsystemmodule)\n .then(IconSystem => {\n const iconSystem = new IconSystem();\n prefetchTemplate(iconSystem.getTemplateName());\n\n return iconSystem;\n })\n .then(iconSystem => {\n fetchQueue();\n iconSystem.init();\n\n return;\n })\n .catch();\n }, 500);\n};\n\n/**\n * Add a set of templates to the prefetch queue.\n *\n * @param {Array} templatesNames\n */\nconst prefetchTemplates = templatesNames => {\n templateList = templateList.concat(templatesNames);\n\n processQueue();\n};\n\n/**\n * Add a single template to the prefetch queue.\n *\n * @param {String} templateName\n * @returns {undefined}\n */\nconst prefetchTemplate = templateName => prefetchTemplates([templateName]);\n\n/**\n * Add a set of strings from the same component to the prefetch queue.\n *\n * @param {String} component\n * @param {String[]} keys\n */\nconst prefetchStrings = (component, keys) => {\n if (!stringList[component]) {\n stringList[component] = [];\n }\n\n stringList[component] = stringList[component].concat(keys);\n\n processQueue();\n};\n\n/**\n * Add a single string to the prefetch queue.\n *\n * @param {String} component\n * @param {String} key\n */\nconst prefetchString = (component, key) => {\n if (!stringList[component]) {\n stringList[component] = [];\n }\n\n stringList[component].push(key);\n\n processQueue();\n};\n\n// Prefetch some commonly-used templates.\nprefetchTemplates([].concat(\n ['core/loading'],\n ['core/modal'],\n ['core/modal_backdrop'],\n));\n\n// And some commonly used strings.\nprefetchStrings('core', [\n 'cancel',\n 'closebuttontitle',\n 'loading',\n 'savechanges',\n]);\nprefetchStrings('core_form', [\n 'showless',\n 'showmore',\n]);\n\nexport default {\n prefetchTemplate,\n prefetchTemplates,\n prefetchString,\n prefetchStrings,\n};\n"],"file":"prefetch.min.js"} \ No newline at end of file diff --git a/lib/amd/src/prefetch.js b/lib/amd/src/prefetch.js index e8e6fb857d9..b69606f23ad 100644 --- a/lib/amd/src/prefetch.js +++ b/lib/amd/src/prefetch.js @@ -75,11 +75,6 @@ const fetchQueue = () => { * Subsequent fetches are immediate. */ const processQueue = () => { - if (Config.jsrev <= 0) { - // No point pre-fetching when cachejs is disabled as we do not store anything in the cache anyway. - return; - } - if (prefetchTimer) { // There is a live prefetch timer. The initial prefetch has been scheduled but is not complete. return; diff --git a/lib/outputrequirementslib.php b/lib/outputrequirementslib.php index 236b0ed139a..7bca2a770a9 100644 --- a/lib/outputrequirementslib.php +++ b/lib/outputrequirementslib.php @@ -1390,9 +1390,7 @@ class page_requirements_manager { // First include must be to a module with no dependencies, this prevents multiple requests. $prefix = 'M.util.js_pending("core/first");'; $prefix .= "require(['core/first'], function() {\n"; - if ($cachejs) { - $prefix .= "require(['core/prefetch']);\n"; - } + $prefix .= "require(['core/prefetch']);\n"; $suffix = 'M.util.js_complete("core/first");'; $suffix .= "\n});"; $output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);