Route-based code splitting in ClojureScript with :modules and :target :esm
Split a ClojureScript SPA into route-specific ES modules and load them on demand with import(), plus what to watch for under advanced compilation.
06 Oct 2025, 11:52 UTC

The problem: your SPA downloads every route upfront
A ClojureScript single-page application usually compiles to one JavaScript bundle. That means the browser downloads code for every route—dashboard, settings, reports—before the user visits any of them. On slow connections or mobile devices, that delays the first paint and wastes bandwidth.
The useful takeaway: ClojureScript’s `:modules` option, paired with `:target :esm`, emits separate native ES modules. You can load a route’s module only when the user navigates to it, using the browser’s native `import()`.
Split output with `:modules` and `:target :esm`
`:modules` tells the compiler to emit multiple JavaScript files instead of one bundle. `:target :esm` makes those files native ES modules, which modern browsers can fetch dynamically. Shared code—Reagent, re-frame, routing helpers—goes into a common module; route-specific namespaces go into their own modules.
Version assumption: this applies to recent ClojureScript releases that support `:target :esm`. Exact `:modules` syntax differs between `cljs.main` and Shadow CLJS, so examples for one build tool may not transfer directly.
Example build configuration
Run this in your project root. No special permissions are required beyond normal file access. Replace namespace placeholders with your own values.
;; deps.edn (cljs.main example; syntax varies by build tool)
{:deps {org.clojure/clojurescript {:mvn/version "1.11.x"}}
:aliases
{:build {:main-opts
["-m" "cljs.main"
"-d" "target/public"
"-c" "myapp.core"
"-co"
{:modules {:common {:entries [myapp.shared]}
:dashboard {:entries [myapp.routes.dashboard]}
:settings {:entries [myapp.routes.settings]}}
:target :esm
:optimizations :advanced}}]}}
Risk: under `:optimizations :advanced`, the compiler renames functions. A function called from another module must be stabilized with `^:export` or externs, otherwise the call breaks after minification.
Load a route module when the user navigates
Because `import()` is asynchronous, your router must handle loading and error states instead of assuming the module appears instantly.
Example router skeleton
(ns myapp.router
(:require [reagent.core :as reagent]))
(defonce current-view (reagent/atom nil))
(defonce loading? (reagent/atom false))
(defonce error (reagent/atom nil))
;; Schematic placeholder only; not copy-paste production code.
(defn load-route [route]
(reset! loading? true)
(reset! error nil)
(try
(let [mod (js/import (str "./modules/" route ".js"))]
(reset! current-view (:default mod)))
(catch js/Error e
(reset! error (.message e)))
(finally
(reset! loading? false))))
(defn route-link [route label]
[:button {:on-click #(load-route route)} label])
Note: the `js/import` call above is a schematic placeholder. Your module paths depend on the compiler output directory and module names, and you must await the returned promise in real code.
Trade-offs and how to verify
- Total JavaScript can grow because module boundaries prevent some cross-module optimizations.
- Dynamic imports add a network round-trip when the user first visits a route, so consider a loading indicator.
- Advanced compilation renaming can break inter-module calls unless exports are stabilized.
Practical check
- Compile the project with the configuration above.
- Open the app and open browser DevTools to the Network panel.
- Reload the page. Confirm only the entry and common modules load initially.
- Navigate to a route. Confirm a separate `import()` request fires for that route’s module.
- Recompile with `:optimizations :advanced` and inspect the minified output to confirm cross-module functions still resolve.
If a route module fails to load or a cross-module call throws after advanced compilation, add `^:export` to the affected namespace or function and rebuild.
Closing
Route-based lazy loading with `:modules` and `:target :esm` lets a ClojureScript SPA ship less code upfront. Pair it with explicit loading and error handling, verify module requests in DevTools, and stabilize exports before enabling advanced compilation.