User:SD0001/Making user scripts load faster
User scripts can be made to load faster with the help of caching. However, this is experimental, let me know if you face any issues on the talk page.
Add the following snippet of minified code to the top of your your common JavaScript page:
// Enable caching for resource loads, see [[User:SD0001/Making_user_scripts_load_faster]], @revision 7
if(!/\bnocache=\b/.test(location.href)){let e=mw.config.values,t="text/javascript",r="text/css",n=(n,o,i)=>(n=n.replace(/special:mypage/i,"User:"+e.wgUserName),$.get("https://"+o+"/w/api.php?titles="+n+"&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400&action=query&prop=revisions|info&rvprop=content&rvlimit=1&inprop=protection").then(e=>{let a=e.query.pages[0];if(!a.missing){if(2!==a.ns&&8!==a.ns&&!a.protection.find(e=>"edit"===e.type&&"sysop"===e.level))return $.Deferred().reject('Refused to load "'+n+'"@'+o+": unprotected page");let l=a.revisions[0].content;if(i&&i!==t||"javascript"!==a.contentmodel){if(i!==r||"css"!==a.contentmodel)return $.Deferred().reject('Refused to load "'+n+'"@'+o+": content type mismatch");mw.loader.addStyleTag(l)}else document.head.appendChild(document.createElement("script")).innerHTML=l}})),o=e.wgServerName,i=e=>{let t=/^(?:(?:https:)?\/\/(.*))?\/w\/index.php/.exec(e),r=/\btitle=([^=?&]*)/.exec(e);return t&&r&&/\baction=raw\b/.test(e)&&/\bctype=/.test(e)?[r[1],t[1]||o]:null};window.importScript=e=>{n(encodeURIComponent(e),o,t)},window.importStyleSheet=e=>{n(encodeURIComponent(e),o,r)};let a=mw.loader.load;mw.loader.load=function(e,t){let r=i(e);r?n(r[0],r[1],t):a.apply(mw.loader,[...arguments])};let l=mw.loader.getScript;mw.loader.getScript=function(e){let r=i(e);return r?n(r[0],r[1],t):l.apply(mw.loader,[...arguments])}}
If you don't like this ugly blob of incomprehensible code, you can instead add the fully and pretty version (but this takes up a lot of lines):
Unminified full code (readable version)
|
---|
// Enable caching for resource loads, see [[User:SD0001/Making_user_scripts_load_faster]], @revision 7
if (!/\bnocache=\b/.test(location.href)) { // Don't enable if nocache=1 url parameter is given
let config = mw.config.values;
let ctypeJs = 'text/javascript';
let ctypeCss = 'text/css';
let loadResource = (page, sitename, ctype) => {
page = page.replace(/special:mypage/i, 'User:' + config.wgUserName);
return $.get(
'https://' + sitename + '/w/api.php?titles=' + page + // page is already URL-encoded
'&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400' +
'&action=query&prop=revisions|info&rvprop=content&rvlimit=1&inprop=protection'
).then((apiResponse) => {
let apiPage = apiResponse.query.pages[0];
if (!apiPage.missing) {
if (apiPage.ns !== 2 && apiPage.ns !== 8 && !apiPage.protection.find(p => p.type === 'edit' && p.level === 'sysop')) {
return $.Deferred().reject('Refused to load "' + page + '"@' + sitename + ': unprotected page');
}
let content = apiPage.revisions[0].content;
if ((!ctype || ctype === ctypeJs) && apiPage.contentmodel === 'javascript') {
let scriptTag = document.head.appendChild(document.createElement('script'));
scriptTag.innerHTML = content;
} else if (ctype === ctypeCss && apiPage.contentmodel === 'css') {
mw.loader.addStyleTag(content);
} else {
return $.Deferred().reject('Refused to load "' + page + '"@' + sitename + ': content type mismatch');
}
}
});
};
let serverName = config.wgServerName;
let getSiteTitle = (url) => {
let siteRgx = /^(?:(?:https:)?\/\/(.*))?\/w\/index.php/.exec(url),
titleRgx = /\btitle=([^=?&]*)/.exec(url);
if (siteRgx && titleRgx && /\baction=raw\b/.test(url) && /\bctype=/.test(url)) {
return [titleRgx[1], siteRgx[1] || serverName];
} else return null;
};
window.importScript = (page) => {
loadResource(encodeURIComponent(page), serverName, ctypeJs);
};
window.importStyleSheet = (page) => {
loadResource(encodeURIComponent(page), serverName, ctypeCss);
};
let oldMwLoaderLoad = mw.loader.load;
mw.loader.load = function(url, type) {
let linkParts = getSiteTitle(url);
if (linkParts) {
loadResource(linkParts[0], linkParts[1], type);
} else {
oldMwLoaderLoad.apply(mw.loader, [...arguments]);
}
};
let oldMwLoaderGetScript = mw.loader.getScript;
mw.loader.getScript = function(url) {
let linkParts = getSiteTitle(url);
if (linkParts) {
return loadResource(linkParts[0], linkParts[1], ctypeJs);
} else {
return oldMwLoaderGetScript.apply(mw.loader, [...arguments]);
}
};
}
|
(The two code snippets are functionally the very same).
Note that the code block necessarily needs to be at the top of your common.js page for it to take effect.
It replaces the existing implementations of mw.loader.load
, mw.loader.getScript
, importScript
and importStyleSheet
. No changes are needed in the way you normally add new user scripts.
Impact
[edit]The effect can be seen by keeping the network tab of your browser dev tools open and navigating through different pages. (Don't reload the same page – that's seen as a hard reload in some browsers and causes bypassing of cache.) Also ensure you have the "Disable cache" option unchecked in the devtools. In Chrome, you should see that most script fetches are resolved by the disk cache itself which is very fast.
Caveats
[edit]- The snippet above makes all scripts and stylesheets cached by your browser for up to one day (86400 seconds). If any of the scripts are updated, it may take you up to 1 day to see the updates. Doing a hard reload will clear the caches. On Google Chrome, just hitting the "reload" button on any page causes a hard reload. On other browsers, you may need to use Ctrl+F5.
Technical details
[edit]loadResource function:
- The API is used instead of index.php because requests to the latter never appear to be cached for logged-in users. With the API's caching parameters, responses get the Cache-Control header
s-maxage=86400, max-age=86400, public
while the one set by index.php isprivate, max-age=0, s-maxage=0
. - The
uselang=content
attribute enables public caching in Varnish, etc. See phab:T97096. However, no public caching takes place for requests by users with ability to view revision-deleted or suppressed content (ref: [1]). - The
origin=*
causes the API to set theAccess-Control-Allow-Origin: *
header to avoid CORS issue while loading cross-wiki scripts. - The contentmodel of the page is checked before the js/css code is evaluated. Non-javascript contentmodel pages won't be evaluated as JS, consistent with a call to index.php for non-JS page with ctype=text/javascript failing with a 403 error.
- Using mw.loader.getScript to load a missing page gives a resolved promise. This is consistent with index.php call to load JS from a non-existent page with a protected title (i.e, a userspace title ending with ".js" or a mediawiki namespace title) giving a 200 response.
- However, using index.php to load JS from a non-existent page but with non-protected title gives a 403. loadResource() will give a resolved promise instead.