Third party scripts
Using Barba means that your site is going to behave like a SPA (Single Page Application), so all your inline scripts will be executed once, and they won’t be executed after a page change done through Barba.
This might be result in some unexpected behaviors when using classic third party scripts.
We encourage you to check the specific documentation provided by the vendors on how handle Single Page Applications or HTML Pushstate API in general.
Summary
Google Analytics
Page view
With Google Analytics
, you can manually send a pageview
with the following snippet.
The best place to do that is in the after
global hook, as it is applied to all your pages:
1 | // disable automatic page view |
See Google SPA guide for more details.
Google Tag Manager
Although you can manually send a page view, as Google Tag Manager has a History change trigger
made specifically to track virtual pageview
in SPA.
See Google Tag Manager support center for more details.
Know issue: How to fix rogue referral with Google Tag Manager
Google ReCaptcha
Start by generating your public/private keys and add the script inside your page:
1 | <script src="https://www.google.com/recaptcha/api.js?render=RECAPTCHA_PUBLIC_KEY"></script> |
Then generate the captcha on the appropriate page using a View
:
1 | barba.init({ |
Be sure to call
grecaptcha.ready()
after adding the reCaptchascript
.See Google reCAPTCHA v3 guide for more details.
Locomotive scroll
This library is well compatible with Barba, but you can encounter issues when implementing it. You need to deal with your page template, Barba lifecycle and LocomotiveScroll settings.
Having absolute or fixed elements on your page could end in a bad height calculation from LocomotiveScroll, in this case, always put the data-scroll-container
attribute on the section of your site that scroll, defaults is window.document
.
Viewport detection
When only using viewport detection, you can implement it this way:
1 | const scroll = new LocomotiveScroll(); |
Using a global hook allows you to reset the scroll in order to detect DOM elements to observe on the next page.
Smooth scroll
When using smooth scrolling, the more relevant solution is to nest the data-barba="container"
inside the data-scroll-container
, like this:
1 | <body data-barba="wrapper"> |
1 | // init LocomotiveScroll on page load |
The LocomotiveScroll implementation really depends on where you set the
data-scroll-container
attribute: it can be whenever you want on the page, no matter outside or inside Barba wrapper/container, but keep in mind that when using it inside the Barba container, it will be duplicated. Bear with this!Note that this code assume that you have the
data-scroll-container
attribute on every pages, so you need to add the appropriate checks if LocomotiveScroll is not used on the whole site.
Hooks workflow
Inside a SPA, it’s not needed to have multiple instance of LocomotiveScroll, but in order to properly update it, it’s necessary to use Barba hooks to have a consistent result when transitioning.
1 | const scroll = new LocomotiveScroll(); |
Depending on what’s inside your website, you can also reset/update the LocomotiveScroll instance inside the Barba
after
hook instead.
Lenis scroll
Unlike LocomotiveScroll that uses scroll hijacking, Lenis is using browser native scrolling, meaning you don’t have to update/refresh it between your website pages. It’s simple as this:
1 | import barba from '@barba/core'; |
Note that if you link Lenis with ScrollTrigger, you will need to deal with triggers.
Scroll Trigger
There are no specific issues when trying to implement this library in a website that uses Barba. Depending on how your website animations are shared along all your pages, and also how you have planned to deal with scroll “triggers”, you may prefer using a view hook instead of a global hook.
View hook
1 | <body data-barba="wrapper"> |
1 | import barba from '@barba/core'; |
Keep in mind that it is very important to kill ScrollTrigger instances when navigating between two pages, otherwise your instances will overlap together.
WordPress
Update body
classes
When using the WordPress CMS you might notice that body
classes could be different from one page to another. If you need to keep consistency between pages, you need to manually update the classes, mainly because the body
element is placed outside of the data-barba="container"
.
1 | import barba from '@barba/core'; |
The implementation above replace all
body
classes from the current page with all classes from the next page. Be sure to adapt it depending on your needs.
Other third party scripts
This is a draft section that may evolve in time, and many other important third party scripts will be added.
Don’t hesitate to ask the Barba team on Slack if you want to request one to be present on this page.