Patternslib/Patterns

autoload-visible should not trigger injection for page elements above visible page top

Closed this issue · 10 comments

gyst commented

doTrigger = reltop <= $scrollable.innerHeight();

Scenario: I have a document with 100 previews, each of which has a trigger: autoload-visible. It also has a comment sentinel with a page fragment url that takes me directly to the bottom of the page. Jumping to the bottom of the page should only trigger the injection of the last page preview image; pages 2-99 do not need to be loaded since they are in "flyover country".

As a complication, when I then move away from this page via injection, this does not work, or rather it waits for a long time for all the 98 spurious preview injections to complete, before I can navigate away.

References:

I felt this one coming ;)

I see it's on @thet's name but I think it should be on my name.

gyst commented

I don't see how you can fix this in markup. IMO it needs a javascript change on the line I quoted.

thet commented

@cornae does this need a definition phase?

@gyst I can't currently fix this in markup. @thet Yes.

thet commented

@cornae I'd like to work on that this evening. Can you give this feature a thought this afternoon?
Although, currently I think this is just a "bugfix". If something isn't visible above the fold, it shouldn't be loaded, right?

Yes. I'll IM you.

thet commented

tnx

thet commented

I'm off this afternoon and will check my mails/messages later

thet commented

First attempt to fix this issue: #941
Second attempt to fix this issue: #942

The idea would be:

  • Use an intersection observer to check if an element is in the viewport. Trigger the loading then.
  • If an element is out of the viewport cancel a previous trigger.

The canceling would be done by aborting the jquery ajax call or by sending an abortsignal for fetch. for that to work we would need access to the ajax request object and that would need a refactoring of pat-inject use patternslib base class and proper scoping/encapsulation. pat ajax would return the ajax request object and pat inject could store it on the pattern instance. work has begun here: #943
During this effort we could switch from jQuery ajax to fetch and use async/await which would give us the possibility to await for an pat-inject call to be finished.
However, that needs a little more time.

Instead the PR #941 uses an intersection observer and a window timeout to schedule the triggering which can be cleared if the element is moved out of the viewport.
The delay is something we normally want to avoid, but it was already there with the original implementation, so this would be acceptable.

Status: the PR #941 needs more fine tuning and testing, currently it doesn't behave as it should.