I’m trying to implement barba.js on a HubSpot site for page transitions.
I’m currently building my site using custom modules and JS from custom modules do not get minified into a single .js file. An explanation of this is below.
Let’s say I have on single .js file called scripts-min.js. This file is added onto the page via src, i.e.
<script src=".../scripts-min.min.js"></script>
Then, let’s say I have a custom module called hero, which has some JS in the module block. That markup is rendered onto the page within <script> tags. So, in short, it appears something like this:
<script>
// js from hero custom module is here
</script>
<script src=".../scripts-min.min.js"></script>
Now, consider the following two pages:
- Resources
- Customers
With my current barba.js implementation, this is the current flow I’m experiencing:
- I access the resources page (not by transitioning to it, by directly accessing it via /resources).
- At this point, all my js is working (slick-sliders, scroll functions etc)
- I then use the navigation to access the customers page. The page loads, but all of the js specific to modules and forms for that page are not working.
In short, js for pages that I transition to, do not work.
To resolve this, what I’m trying to do is to reload all scripts within the container beforeEnter(). Trying to refresh scripts that have src and those that HubSpot renders inline.
$(function() {
function delay(n){
n = n || 2000;
return new Promise((done) => {
setTimeout(() => {
done();
}, n);
});
}
barba.init({
sync: true,
prefetchIgnore: true,
debug: true,
transitions: [{
async leave(data){
const done = this.async();
await delay(200);
done();
},
async beforeEnter({ next, container }) {
$(container).find('script').each(function (i, script) {
var $script = $(script);
$.ajax({
url: $script.attr('src'),
cache: true,
dataType: 'script',
success: function () {
$script.trigger('load');
}
});
});
},
async enter(data){
let scrollX = 0
However, my current beforeEnter() still yields the same results. Any way around this?
I haven’t seen any discussion involving barba and HubSpot, which was surpring as barba is really popular. This makes me think, is it feasible to use in HubSpot? I can imagine form js will need some workarounds too when transitioning over to new pages?