Unpoly Show archive.org snapshot is a library we use to get many advantages of SPAs Show archive.org snapshot , but with much less code.
Important
Work on this lesson in
advisormode.
Learning goals
- You can explain what problem Unpoly solves: SPA-like navigation and interactions on a server-rendered app, with very little JavaScript.
- You can explain how a fragment update works — a link or form targets a fragment, the server renders a normal page, only the target is swapped — and what the
unpoly-railsgem adds on the server side. - You can enhance links and forms so they update fragments, submit automatically or navigate without full page loads, and configure this app-wide instead of per link.
- You can build a subinteraction in an overlay: open a form in a new layer, show validation errors in place, and close the overlay and update the parent page on success.
- You can attach JavaScript behavior to elements with compilers and clean up when elements are removed, and explain why that matters when pages are swapped instead of reloaded.
- You replace hand-written JavaScript with Unpoly's built-in features wherever they fit.
- You can explain how Unpoly compares to Hotwire/Turbo, the Rails default.
Resources
Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.
Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.
- 📄 Getting started Show archive.org snapshot — start here: how fragment updates work, then links, forms, overlays and compilers, one short page each
- 📄 Learn Unpoly Show archive.org snapshot — the guides, chapter by chapter; read Links, Forms, Overlays and Scripting for this card, and skim the other chapters' overviews to see what else the library does
- 📄 It's not you, it's us: we're breaking up with JavaScript frontends Show archive.org snapshot — Henning's slides on the idea behind Unpoly
- 📄 unpoly-rails README Show archive.org snapshot — how the gem complements the frontend library
- 🎮 Unpoly demo app Show archive.org snapshot with its source code Show archive.org snapshot — click through it, then read how it's built
- 📄 Enhancing elements with JavaScript Show archive.org snapshot — compilers: attaching JavaScript to elements, and cleaning up when they leave
- 📄 Fixing memory leaks in web applications Show archive.org snapshot — why an app whose pages are swapped instead of reloaded must not leak
Exercises
Reduce JavaScript with Unpoly
Use Unpoly in MovieDB Show archive.org snapshot :
- All your
compiler()functions should useup.compiler()instead. - Replace as much JavaScript as possible with built-in Unpoly features:
- E.g. code that loads HTML with AJAX and replaces a page fragment can be replaced with
a[up-target]Show archive.org snapshot .- Configure Unpoly according to Handling all links Show archive.org snapshot and Handling all forms Show archive.org snapshot before proceeding to the next exercise.
- E.g. find-as-you-type search can be implemented with
form[up-target]andform[up-autosubmit]Show archive.org snapshot . Is your fix for out-of-order responses from Unobtrusive JavaScript components still needed? See Aborting requests Show archive.org snapshot . - E.g. code that opens modal dialogs can be replaced with
a[up-layer=new]Show archive.org snapshot .
- E.g. code that loads HTML with AJAX and replaces a page fragment can be replaced with
Unpoly overlays
- The form to create a new actor should open in an overlay Show archive.org snapshot .
- If the user submits an invalid form, validation errors should be shown within the same overlay.
- When the user successfully saves a valid actor, the overlay should close and the list of actors should reload.
Tip
To reload the list when the overlay is accepted, set
[up-on-accepted-reload]Show archive.org snapshot on the link that opens it. It runs no JavaScript, so MovieDB's content security policy Show archive.org snapshot won't block it. For a JavaScript callback likeup-on-accepted, wrap it in unpoly-rails'up.safe_callbackShow archive.org snapshot to give it a valid nonce.
Alternatives
Rails ships with Hotwire (Turbo and Stimulus) for the same job: updating fragments of a server-rendered page without full reloads. Turbo Drive and Turbo Frames correspond roughly to Unpoly's navigation and fragment targeting, Stimulus to compilers. Unpoly goes further with layers/overlays, form validation and its handling of complex form interactions, which is why it is our default — see Our Rails stack. You will meet Hotwire in tutorials and in some client projects; the concepts transfer.