Turbo Drive - Re-Use the Turbo Progress Bar

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

View Solution on Patreon
Turbo Drive - Re-Use the Turbo Progress Bar

Premise

Turbo Drive ships with a progress bar displayed on top of the browser window that you are probably all aware of. By default, it appears when a Turbo Drive visit is taking longer than a specific timeout. In today’s fun little experiment, we’ll look at how we can re-use it for our own purposes.

Starting Point

On the page you’ll find a “Start!” button. I’ve written a small server side script that upon POST submit starts an interval that opens a websocket and transmits fake progress data in lieu of a real, long-running process:

https://stackblitz.com/edit/turbo-drive-progress-bar?file=index.js%3AL30,app.js

Note: You can only transmit specified data types such as strings and certain binary types, i.e. you have to (de)serialize JSON (see the docs)

Challenge

  • Write the websocket handler code to update the Turbo Progress Bar programmatically:

Hint: Look at your window.Turbo.navigator object for clues!

  • Can you come up with a way to enable/disable the behavior dynamically?

Caveats

  • You might have to restart the node server while testing if the websocket doesn’t connect. You can do this easily in the Stackblitz terminal: Restarting Node

Teaser

  • Personally I wouldn’t hijack the Turbo Progress bar for tasks other than navigation, because it would result in bad UX. However, certain user actions in the “extended navigation space” such as filtering a large dataset that takes some time, could be a place to provide some feedback that a background process is running. How would you approach implementing this in an app of yours?

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 - Form Activity Indicators
06 June 2023

Provide user feedback for slow running form submits.

Cookies
essential