magnars / optimus

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

Serving static assets gets "weird" when adding compojure in front of stasis/optimus #46

Closed stonetwig closed 9 years ago

stonetwig commented 9 years ago
(defroutes app
  "This is the function we pass to Ring. It will be called with a
   request map for every request."
  (GET "/client-type-endpoints" [] (render-file "templates/client-type-endpoints.html" {:somedata "data"}))
  (route/resources "/")
  (route/not-found (-> get-pages
      (stasis/serve-pages)
      wrap-exceptions
      (optimus/wrap get-assets optimize
                    (if freeze-assets? serve-frozen-assets serve-live-assets))
                    )))

This is a snippet from my current code. We're using stasis and optimus to serve static files like markdown. However, I was in a need to add new routes to this project with more than just static content.

I added compojure and selmer because I suck at clojure, and while it works I now receive a very weird 404 on the files stasis/optimus is supposed to serve.

It renders everything perfectly except I get 404 on all static files.

An image:

As you can see, the compiled static files all gets 404 even the initial request even if it renders the correct html (which is weird indeed).

But if I do a request to the resource requested by the browser, it renders perfectly well:

The problem started when I added the compojure dependency because I wanted more simple routing, however the only way I found to send all request that didn't fit a specific route where the route/not-found function and my guess is that's what making the problem occur.

My question is if you know a simple way to keep all existing stasis/optimus code and how to add a new route that can render a specific page? If it could be done with the compojure dep it would be awesome. This might not be stasis related exactly (I think), but since I am a noob at clojure I am not sure.

stonetwig commented 9 years ago

I think the issue is that I am using the route/not-found function since this is the source:

(defn not-found
  "A route that returns a 404 not found response, with its argument as the
  response body."
  [body]
  (rfn request
    (-> (response/render body request)
        (status 404))))

I'll ask in the compojure repo instead.

magnars commented 9 years ago

Did you find this out? I can help out a bit if you tell me where you're at now.

stonetwig commented 9 years ago

That's alright, I solved it :)