Some quick background: in the Angular world, the router allows you to define primary routes and auxiliary routes. I was able to use these auxiliary routes to deep link to all sorts of secondary interfaces like modal windows and fly-out panels. It’s a very attractive feature of the Angular framework.
I’ve been contemplating how to do something similar (albeit more limited / constrained) in the Hotwire framework. So far, the approach I’ve been thinking about does this:
- Auxiliary link opens in Turbo Frame.
- Auxiliary link advances URL to point to Auxiliary view.
The upside to this approach is that it can be 100% progressively enhanced (ie, work without JavaScript) and is the least complex. The downside to this approach is that if the user refreshes the page at that point, I’ll lose the state of the primary route and render a page that contains only the auxiliary view.
For the last few days, I’ve been trying to noodle on ways to both advance the URL and keep both routes in play. I think for that, I’d have to use a query-string param. Imagine something like:
/my/main/route.htm?auxiliary=some.other.action
This could still open into a Turbo Frame; and, on the server-side I could probably have some logic that:
- If
auxiliary
is defined. - And, I’m in a
Turbo-Frame
(HTTP header) - Only render the auxiliary view.
But, then I get stuck on the “what if the user refreshes the page?” I’ve seen some writing where people will look at the URL and then use client-side JavaScript to programmatically trigger the Turbo Frame to be visited. I could probably simplify that to just predefine the src
attribute of the <turbo-frame>
to point to the same URL.
But, at that point, I have to use JavaScript in order to get the same view state to render. (to fetch the Turbo Frame content).
Oh, I just had a thought as I’m writing this! What if the refresh renders a Turbo Frame that contains a fallback link to point to the auxiliary view itself. Meaning, something like:
<turbo-frame src="/path/to/view.htm?auxiliary=some.other.action">
<!-- Static button that will show for when Hotwire hasn't loaded. -->
<a href="/some/other/action.htm">
Open Auxiliary View
</a>
</turbo-frame>
This all becomes much simpler if I depend on Hotwire loading, in so much as I can depend on the Turbo Frame to kick into action. But, I’m still trying to approach Hotwire as a progressive enhancement with functional fallbacks. But, maybe by the time I’m thinking about auxiliary routes, I need to start depending on Hotwire.
Apologies for the stream of conscience here - I’ve been going around in circles in my head for days and my Google searches haven’t yielded too much. If anyone has any suggestions, I’m all ears!