Inefficient inline javascript for section edit links
Categories
(developer.mozilla.org Graveyard :: General, enhancement, P3)
Tracking
(Not tracked)
People
(Reporter: peterbe, Unassigned)
Details
First of all, every document (on the wiki) has this block:
<script>
(function($) {
$('#wikiArticle').children('h2[id]').each(function() {
var id = $(this).attr('id');
var href = '/en-US/docs/Web$edit#' + id;
var pencil = window.mdnIcons ? window.mdnIcons.getIcon('pencil') : '';
$('<a>').attr({
href: href,
'class': 'button section-edit only-icon',
'rel': 'nofollow, noindex'
})
.append(pencil)
.append($('<span>'))
.find('span')
.text(gettext('Edit'))
.end()
.on('click', function(e) {
e.preventDefault();
mdn.analytics.trackEvent({
category: 'Section Edit',
action: id
}, function() {
window.location = href;
});
})
.appendTo(this);
});
})(jQuery);
</script>
It comes from: https://github.com/mozilla/kuma/blob/2aa50afee492d4290da3ef1c0d526a6fe1372700/kuma/wiki/jinja2/wiki/document.html#L303-L334
It depends on a Jinja variable doc_edit_url which could be implicit from the current URL maybe or from a data attribute in the DOM.
Being inline means it definitely blocks (it depends on jQuery so it can't be async but it can be defer) and it would make it impossible to CSP protect.
The code also creates one new onclick event handler for every possible #wikiArticle h2[id]. On this page there's 3 for example.
It's only 1KB of code added to every document but that adds up.
| Reporter | ||
Comment 1•7 years ago
|
||
Actually, it only ever shows up for authenticated users
That's a small portion of users.
| Reporter | ||
Comment 2•7 years ago
|
||
Actually, $('#wikiArticle').children('h2[id]') means only when there's an immediate <h2> after the <div id="wikiArticle">. Not sure when that ever happens.
Comment 3•7 years ago
|
||
(In reply to Peter Bengtsson [:peterbe] from comment #2)
Actually,
$('#wikiArticle').children('h2[id]')means only when there's an immediate<h2>after the<div id="wikiArticle">. Not sure when that ever happens.
Oh no, that will find all h2 elements with an id attribute that is inside wikiArticle. I wonder if we cannot do this more simply inside Jinja? That is a wiki only feature so, we should be sure to serve it to the read-only UI.
I am planning on starting the same process for JS as I am doing for CSS in this regard i.e. have a wiki-shared.js(or something like that). Will add this to the tracker as a blocker?
Updated•7 years ago
|
Comment 4•6 years ago
|
||
Updated•6 years ago
|
Description
•