Turbo Drive - Render Interception

Spice up Turbo Drive transitions using rendering interception.

View Solution on Patreon
Turbo Drive - Render Interception

Premise

Turbo Drive intercepts regular link clicks and swaps out the HTML <body> without doing a full reload of all the JavaScript and CSS included with it. The concept behind this is called an (Application) Visit.

After fetching the new content, but before actually rendering, the turbo:before-render event is called. This event gives you as an application developer the opportunity to pause rendering and do something. A good example are custom animations or page transitions, as in the powerful Turn library by Dom Christie. The event’s detail property contains a resume method you have to call once you are done.

Starting Point

Looking at this StackBlitz example, there’s a mock-up storefront with two pages (index.html and page2.html) and a pagination bar at the bottom. In app.js, there’s some boilerplate code introducing a turbo:before-render event listener. Your challenge is to fork this StackBlitz project, hook into the event listener and do something interesting.

Challenges

  • Can you add a custom CSS transition from one visit to another? (below is an example GIF of how that could look like.)
  • Can you configure the duration of a transition? (I.e. setTimeout?)
  • Can you invoke a third party API (using fetch)?

Caveats

  • beware of restoration visits! check <html data-turbo-preview> or opt out of caching altogether

Teasers

  • How could you make use of this in an app of your own today?

More from

Optimistic UI with Turbo 8 Morphs
26 March 2024

Provide Optimistic UI updates using inline Turbo Stream Actions, and reconcile using Turbo 8 Morphs

Turbo Drive - Conditional InstantClick
13 February 2024

Conditionally opt out of Turbo Drive InstantClick using the Strategy pattern

Turbo Drive - Re-Use the Turbo Progress Bar
18 July 2023

Update the progress bar according to some background process' progress.

Cookies
essential