Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

history.pushState() error #20

Closed
PoorBillyPilgrim opened this issue Dec 10, 2020 · 5 comments
Closed

history.pushState() error #20

PoorBillyPilgrim opened this issue Dec 10, 2020 · 5 comments

Comments

@PoorBillyPilgrim
Copy link
Owner

After adding gallery navigation, an error code appeared in the console.

In Firefox 83: Uncaught DOMException: The operation is insecure. Too many calls to Location or History APIs within a short timeframe.

In Safari 13.1.1: SecurityError: Attempt to use history.pushState() more than 100 times per 30 seconds

Performance takes a massive hit once you start moving past those errors.

I might be able to fix by debouncing when history.pushState() fires. Say, fire only after if $('.left'), $('.right'), and $('.art-crawl-item')` are not clicked in such quick succession. The trick will be figuring out what constitutes 'too many clicks in succession.'

@PoorBillyPilgrim
Copy link
Owner Author

Just realized that the current build at art-crawl-herokuapp.com does not have this problem, even if I attempt to replicate this issue. It is possible this problem comes from calling changeImage() or handleGallery().

@PoorBillyPilgrim
Copy link
Owner Author

After testing current commit b920cad970de488d6f467808f72d106e9c9f5b72, it is clear that the issue lies with handleGallery() and not renderMetadata(). I selected several images in the grid - which call renderMetadata and the error did not occur. However, even after only a few clicks which fire handleGallery the error occurred. In fact, the error seems to have been fired 68 times over the span of less than 10 clicks. It seems then that handleGallery is firing history.pushState() way more times than what is apparent.

@PoorBillyPilgrim
Copy link
Owner Author

I think this is the problem:

    function changeImage(id) {
        dataAttributes = getDataAttributes($(id));
        img = $(id).find('img').attr('src').replace('thumbnails', 'artcrawl');
        $('#metadata.active').css('opacity', 0);
    
        setTimeout(function () {
            $('#metadata-img').attr('src', img);
            let loadedImg = document.querySelector('#metadata-img');
            loadedImg.addEventListener('load', function(event) {
                 $('#metadata.active').css('opacity', 1);
                 renderMetadataImg($('#metadata-caption'), dataAttributes);
            });
            gridItemID = $('.art-crawl-item.highlight').attr('id');
            let username = $('.art-crawl-item.highlight').attr('data-username');
            history.pushState({'item_id': gridItemID}, 'Art Crawl', '#username=' + username + '&id=' + gridItemID);
            handleGallery(window.location.hash.substr(1));
        }, 550);
    }
    
    function handleGallery(hash) {
        let params = getHashParams(hash);
        let lastGridItem = document.querySelector('#grid').children.length - 2;
        let gallery = parseInt(params.id);
        $('.left').click(function() {
            let highlights = Array.from(document.querySelectorAll('.highlight'));
            for (let highlight of highlights) {
                highlight.classList.remove('highlight');
            }
            gallery -= 1;
            if (gallery < 0) {
                gallery = lastGridItem;
            }
            changeImage('#' + gallery);
            $('#' + gallery).addClass('highlight');
        });
    
        $('.right').click(function() {
            let highlights = Array.from(document.querySelectorAll('.highlight'));
            for (let highlight of highlights) {
                highlight.classList.remove('highlight');
            }
            gallery += 1;
            if (gallery > lastGridItem) {
                gallery = 0;
            }
            changeImage('#' + gallery);
            $('#' + gallery).addClass('highlight');
        }); 
    }

handleGallery() and changeImage() are fired inside each other. This is likely causing a recursive issue.

@PoorBillyPilgrim
Copy link
Owner Author

Resolved by removing the redundant firing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant