Closed Bug 1568628 Opened 7 years ago Closed 6 years ago

Inefficient inline javascript for section edit links

Categories

(developer.mozilla.org Graveyard :: General, enhancement, P3)

enhancement

Tracking

(Not tracked)

RESOLVED WONTFIX

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.

Actually, it only ever shows up for authenticated users
That's a small portion of users.

Actually, $('#wikiArticle').children('h2[id]') means only when there's an immediate <h2> after the <div id="wikiArticle">. Not sure when that ever happens.

(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?

Priority: -- → P3
MDN Web Docs' bug reporting has now moved to GitHub. From now on, please file content bugs at https://github.com/mdn/sprints/issues/ and platform bugs at https://github.com/mdn/kuma/issues/.
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → WONTFIX
Product: developer.mozilla.org → developer.mozilla.org Graveyard
You need to log in before you can comment on or make changes to this bug.