question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stop ajax loading with `esc`

See original GitHub issue

Issuehunt badges

Current

  1. Click on the “Issues” tab above
  2. Change your mind
  3. Can’t do anything, Issues keeps loading
  4. When loaded, go back

Desired

  1. Click on the “Issues” tab above
  2. Change your mind
  3. Press esc, loading stops

⬆️ this is how regular pages work, but pjax doesn’t listen to esc


IssueHunt Summary

artusm artusm has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:25 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
notlmncommented, Jul 22, 2019

I’m not sure if https://github.com/defunkt/jquery-pjax is the one GitHub is using, because most of the events are missing from GitHub’s JS files.

So, I came up with a hack using AbortController, that involves replacing fetch at global level.

const actualFetch = self.fetch; // Saving original fetch for later use
const controller = new AbortController();

// Custom fetch, replacing `AbortSignal` on request object
self.fetch = async (request) => {
	console.log(request);
	const newRequest = new Request(request, {signal: controller.signal}); // Create new request, with our own signal
	try {
		return actualFetch(newRequest);
	} catch (error) {
		console.error(error);
	}
};

// Handler to abort fetch
window.addEventListener('keydown', (event) => {
	if (event.key === 'Escape') {
		console.log('Aborting...');
		controller.abort();
	}
});

One catch with the above snippet is that after the fetch is aborted, the user is directly redirected to the target page, which is no the intended behavior, we need the user to stay on the current page.

I think we can manage this using as very clever combination of mousedown, click, and mouseup event handlers and using stopPropagation() if the click actually caused that aborted fetch (haven’t tested that part though).

2reactions
fregantecommented, Sep 10, 2020

From “impossible” to “done”

Thanks @artusm!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Abort AJAX file upload by pressing escape key - Stack Overflow
I'm trying to abort() an AJAX file upload. I've tried adding event listeners and am trying to abort() for any keypress at all...
Read more >
Problem with Ajax Request and Escape - jQuery Forum
In most browsers esc stops page loading, in the event of ajax, it aborts the loading. This is just a shot in the...
Read more >
Firefox abort all AJAX request when you press ESC
Why firefox abort all AJAX request when you press the ESC button? I don't see any value in a feature like this.
Read more >
Google May Stop Crawling Old AJAX Scheme With Escape ...
As you know, Google deprecated their old AJAX crawling scheme proposal just about two years ago. But currently they still do crawl and...
Read more >
How to avoid unwanted clicks while processing an AJAX request
My solution to this is to define a boolean such as bRequestActive and set it to true as soon as the request goes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found