When running with bigpipe enabled, back to top repeats

Created on 19 January 2023, almost 2 years ago
Updated 3 October 2023, about 1 year ago

Problem/Motivation

After a core 9.5 update, we noticed repeating "Back to top" links for each element in body of toc.

Steps to reproduce

enable bigpipe, toc_js .add back to top link for a content type and add some content.

Proposed resolution

verify that the context object in toc_js.js is of type HTMLDocument and not HTMLBodyElement

Remaining tasks

verify others have the issue and that this is the best solution.

User interface changes

none

API changes

none

Data model changes

none

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States el1_1el

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to RTBC over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States emerham

    Can confirm that this does fix the issue when big pipe is on.

  • πŸ‡«πŸ‡·France flocondetoile Lyon

    if ($(context).toArray()[0].constructor.name === 'HTMLDocument')

    This looks a little ugly. Why not use the library core/once to ensure backToTop element are added only once ?

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States emerham
  • πŸ‡«πŸ‡·France flocondetoile Lyon

    Hum you should be able to target multiple elements on the same page. And the syntax looks strange. See https://www.drupal.org/node/3158256 β†’

    var toc_js = $(once('processBackToTop', '.toc-js', context));
    toc_js.each(...);

    or

    var toc_js = once('processBackToTop', '.toc-js', context);
    toc_js.forEach(...);
  • πŸ‡ΊπŸ‡ΈUnited States emerham

    Rewrote to match code example.

    Tested with Multiple headings on my page as well as mutliple ToC JS blocks on the same page and can confirm that every heading that was added to the ToC list gets a back to the top. On the headings of the same level in both ToC blocks I do see two links so I can confirm that this is working once for each block.

  • πŸ‡ΊπŸ‡ΈUnited States emerham

    I did attempt to apply the each to the back to top section

            var opts = $.extend({}, nav.toc.defaults, options);
            if (opts.backToTop) {
              var scrollTo = function(e, callback) {
                if (opts.smoothScrolling && typeof opts.smoothScrolling === 'function') {
                  e.preventDefault();
                  var elScrollTo = $(e.target).attr('href');
    
                  opts.smoothScrolling(elScrollTo, opts, callback);
                }
                $('li', self).removeClass(opts.activeClass);
                $(e.target).parent().addClass(opts.activeClass);
              };
    
              nav.find('a').each(function () {
                var container = $(opts.container);
                var anchor = $(this).attr('href').replace(/^#/, '');
                var anchorNameToTop = anchor + '-to-top';
                $(this).attr('id', anchorNameToTop);
    
                var $span = container.find('span[id="' + anchor + '"]');
                var anchorToTop = $('<a/>')
                  .attr('href', '#' + anchorNameToTop)
                  .addClass('back-to-top')
                  .text(opts.backToTopLabel)
                  .insertAfter($span)
                  .bind('click', function(e) {
                    scrollTo(e, function() {
                    });
                    nav.trigger('selected', $(this).attr('href'));
                  });
              });
            }

    but I believe that since this is inside the .each from line 14 it got called mutliple times so the once didn't matter.

    • emerham β†’ authored 5f2d1ecc on 2.0.x
      Issue #3334941 by emerham, el1_1el, flocondetoile: When running with...
  • Status changed to Fixed over 1 year ago
  • πŸ‡«πŸ‡·France flocondetoile Lyon

    Committed. Thanks. For #8 I think it's another issue. If you two block toc-js which target the same heading, then it's normal to have two back-to-top links added. If you wan't limit the number of back-to-top links added, then the question is which anchor choose to only get one back-to-top link. The easier way should be to add a class on the span targeted and then target only the span whitout this class. But it's another issue.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed about 1 year ago
  • πŸ‡¦πŸ‡ΉAustria tgoeg

    Is there anything open that prevents releasing this as a stable/supported version? Drupal 9 will soon reach EOL, so a stable release for D10 would be important.

Production build 0.71.5 2024