magnars / optimus

A Ring middleware for frontend performance optimization.
364 stars 23 forks source link

Get rid of request dependency for asset url generation #33

Closed antonyshchenko closed 9 years ago

antonyshchenko commented 9 years ago

First of all: thanks for the great library!

Now the problem: currently it's not very convenient to carry the request hash down via the function calls chain to the function that calls link/file-path.

What is the reason for storing :optimus-assets in request? Can we store them in an atom maybe and thus get rid of request dependency? I could implement that change if you are fine with this idea.

magnars commented 9 years ago

I'm generally not a fan of global state, and certainly not of libraries that enforce it on me. The assets are placed on the request because that's where Optimus enters the world (in your Ring middleware stack).

There's no problem writing a new middleware that exposes the assets globally. Something like this:

(def optimus-assets (atom))

(defn expose-optimus-assets [handler]
  (fn [req]
    (swap! optimus-assets (:optimus-assets req))
    (handler req)))

Another alternative is to use Enlive to transform your HTML after the fact, something like this: https://gist.github.com/sritchie/7794646

I've used a similar idea here, to replace all image URLs: https://github.com/magnars/emacsrocks.com/blob/master/src/emacs_rocks/web.clj#L24-L33

antonyshchenko commented 9 years ago

Fair enough. Thank you!

I will have to combine both approaches, because besides html I need to serve asset urls in json API output.