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

Check if link has been prefetched successfully #568

Closed
Tracked by #703
TheFunkyMonk opened this issue Sep 26, 2020 · 4 comments · Fixed by #704
Closed
Tracked by #703

Check if link has been prefetched successfully #568

TheFunkyMonk opened this issue Sep 26, 2020 · 4 comments · Fixed by #704

Comments

@TheFunkyMonk
Copy link

TheFunkyMonk commented Sep 26, 2020

When using the @barba/prefetch plugin, new pages are usually ready to go before they're clicked on and I can transition them in. In the event they aren't ready, I would like to animate in a loading spinner so the user isn't left without feedback (though this should be bypassed if not needed).

Something like:

before: (data) => {
	// Only if page isn't ready yet
	if (!data.trigger.fetched) {
		// Animation sequence to load spinner
		// Once animation has completed, continue lifecycle
	}
},
enter: (data) => {
	// Entrance animation for new page
}

It seems the only way to currently do something similar to this would be to always trigger the entrance of the loading spinner in the before hook, and let the entrance of the new page fade in over the top of it, effectively obscuring it from view if it's not needed.

Though this sometimes leads to odd occurrences where the enter hook is ready to fire when the loader entrance has partially completed, so the user just sees a brief flash of the loader. It would be nice if the lifecycle progress could he halted for an animation in the before hook only if the prefetch hasn't completed yet.

@xavierfoucrier xavierfoucrier changed the title @barba/prefetch Feature Request: Check if link has been prefetched successfully Check if link has been prefetched successfully Sep 28, 2020
@xavierfoucrier
Copy link
Member

Hi @TheFunkyMonk,

Thanks for opening the issue.
As discussed together on Slack, I am adding @thierrymichel and he will give you his opinion on this feature request.

Have a nice day 😉

@TheFunkyMonk
Copy link
Author

Thanks @xavierfoucrier!

For clarity around what I'm asking, here is a workaround I've come up with that seems to work ok:

const initBarba = () => {
	let cachedPages = [];

	barba.use(barbaPrefetch);
	barba.init({
		transitions: [
			{
				before: (data) => {
					barba.cache.getRequest(data.trigger.href).then(() => {
						cachedPages.push(data.trigger.href);
					});
				},
				leave: (data) => {
					const showLoader = !cachedPages.includes(data.trigger.href);
					
					if (showLoader) {
						// Pause lifecycle to animate in loader
					}
				},
				enter: (data) => {
					// Pause lifecycle to animate in new page
				}
			}
		]
	});
}

Basically, in the before hook I'm hooking into the promise returned from barba.cache.getRequest(data.trigger.href) when an eligible link is clicked and manually adding it to an array of cached pages. Then I can check that array in the leave hook, similar to how you could just check barba.cache directly if you weren't using @barba/prefetch.

It would be nice if barba.cache.get(data.trigger.href) could also return a status according to whether the request promise had been fulfilled to avoid this workaround. Hopefully, that makes sense!

@stale stale bot added the wontfix label Nov 30, 2020
@barbajs barbajs deleted a comment from stale bot Nov 30, 2020
@stale stale bot added the wontfix label Jun 9, 2021
@barbajs barbajs deleted a comment from stale bot Aug 16, 2021
@thierrymichel thierrymichel added this to the barba@next milestone Jan 31, 2022
@thierrymichel thierrymichel self-assigned this Jan 31, 2022
@xavierfoucrier
Copy link
Member

@TheFunkyMonk Hi 👋

Sorry for the long long reply...

I will work on this soon, and I wanted to let you know that there are currently two possibilities for your use case:

  • use getRequest option
  • use sync option
  1. with the barba.cache.getRequest method you can obtain the current pending request from the Cache class, and act when the request is fulfilled:
let request;

barba.init({
  transitions: [{
    name: 'opacity-transition',
    before(data) {
      request = barba.cache.getRequest(data.next.url.href);
    },
    leave(data) {
      return request.then(() => {
        gsap.to(data.current.container, {
          opacity: 0
        });
      });
    },
    enter(data) {
      return gsap.from(data.next.container, {
        opacity: 0
      });
    },
  }],
});
  1. use the sync: true option in a transition, see sync mode:
barba.init({
  transitions: [{
    name: 'opacity-transition',
    sync: true,
    leave(data) {
      return gsap.to(data.current.container, {
        opacity: 0
      });
    },
    enter(data) {
      return gsap.from(data.next.container, {
        opacity: 0
      });
    },
  }],
});

Hope this is clear 😉

@xavierfoucrier xavierfoucrier linked a pull request Apr 26, 2023 that will close this issue
xavierfoucrier added a commit that referenced this issue Apr 29, 2023
@xavierfoucrier xavierfoucrier mentioned this issue Apr 29, 2023
15 tasks
@xavierfoucrier
Copy link
Member

Closed by #704.

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

Successfully merging a pull request may close this issue.

3 participants