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

Turbo Drive - Swiper-like View Transitions
19 November 2024

Use the View Transitions API to drive powerful native-like animations

Turbo Drive - Use ULIDs for Optimistic UI
13 August 2024

Render deterministic optimistic UI elements using client-side ULIDs

Optimistic UI with Turbo 8 Morphs
26 March 2024

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

Cookies
essential