Recipes
Become a Barba cooker!
Based on your project and website features to implement, those recipes may help you during the development process: debugging, managing containers, scroll position, browser requests, etc..
Summary
Debug
debug
This property displays useful console.info
about transition used, for debugging purpose only.
It sets logLevel to debug
. Default is false
.
Enable it like this:
1 | barba.init({ |
We recommend you to enable the debugger only in development environment to properly catch transition errors.
logLevel
When debugging with Barba, you can easily set the log level of debug informations that the browser display:
off
→ log is disablederror
→ useconsole.error()
warning
→ useconsole.warn()
info
→ useconsole.info()
debug
→ useconsole.log()
Use this syntax to adjust the log level:
1 | barba.init({ |
Containers
During a page transition, Barba use two data-barba="container"
to discern current
and next
page, leading the wrapper to contain both at the same time until the transition is done. In order to make a fluent transition and prevent containers to be badly positioned, use one of this mechanism:
- default: do nothing and let Barba remove the container itself
data.current.container.remove()
: manually remove the current container when you need to, this is useful if you need to “clean” the DOM before calling some custom scriptsposition: absolute
: keep containers superimposed, this is useful when making async
(crossfade) transition
Scroll position
After a page transition, the browser won’t “hard reload” the current tab, meaning that you will need to “reset” the scroll position before entering a new page. This can be easily done using a global hook
.
Wait… my browser still continue to restore the scroll position when I am using backward/forward buttons, why? Because this is the default behavior in most modern browsers. To disable it, you will need to set the scroll restoration to be “manual” instead of “auto”:
1 | if (history.scrollRestoration) { |
Note that some third party plugins like LocomotiveScroll implement this feature.
If you need to restore the scroll position while history.scrollRestoration
is enabled, you can store the x/y scroll position using the Barba history
object, like this:
1 | let scrollX = 0 |
Browser requests
requestError
Allows you to catch request errors.
If this function returns false
, wrong links will not be “force” triggered.
Argument | Type | Description |
---|---|---|
trigger |
HTMLElement | The clicked/hovered HTMLElement |
String 'barba' |
Programmatic navigation | |
String 'back' | 'forward' |
Browser backward/forward button | |
action |
String 'enter' |
Page was reached on mouseover regarding the prefetchIgnore strategy |
String 'click' |
Page was reached with a user click | |
String 'prefetch' |
Page was reached with a programmatic prefetch or the @barba/prefetch plugin |
|
url |
String | Requested URL |
response |
Object | Fetch error with message or response with status , statusText , etc. |
Here is an example to manage 404 server responses with a custom page:
1 | barba.init({ |
Note that if you use
barba.go()
directive without returningfalse
, you will be redirected to the requested URL because Barba usesbarba.force()
to reach the page.
timeout
On slow network or with a high page weight, the server can take time to give a response to the user. In case the page take more than timeout to be loaded, it lead Barba to abort the transition and display a Timeout error message.
To prevent this behavior, you can increase the timeout
:
1 | barba.init({ |
In addition, you can properly catch the error by using the requestError
callback.
If a timeout occurs when you are trying to go to another page, Barba will redirect you instead of reloading the page.
Custom headers
You can pass custom request headers to the XMLHttpRequest
object and manage them using exposed methods:
1 | // set request headers |
Keep in mind that if you want a request header to be present server-side, you need to set it before Barba starts the page transition.
By default, Barba sends a non-removable custom HTTP Header named
x-barba
.
Partial output
If you are using a server side language like PHP, you can detect your custom HTTP headers and output just the container instead of the entire page: this could result in less bandwidth usage and less server-side load.
1 |
|
Note that doing so, you have to manually handle the update of the page
title
.