Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:middleware doesn't effect static files. #7

Open
Zaphodious opened this issue Apr 13, 2018 · 4 comments
Open

:middleware doesn't effect static files. #7

Zaphodious opened this issue Apr 13, 2018 · 4 comments

Comments

@Zaphodious
Copy link

Middleware added to the :duct.core/handler :middleware vector doesn't seem to have any effect on static files. This is unfortunate, as gzip (added via the ring-gzip middleware) is hugely important for compiled cljs.

Middleware correctly effects routes using :duct.router/cascading. However, "resource" compojure routes added using :duct.router/cascading don't seem to do anything, so that's little help.

The best solution I've been able to find is to explicitely declare a :duct.server.http with a handler that wraps the :duct.core/handler in whatever middleware I desire. This, I feel, is kludgy and inelegant compared to declaring a :handler :middleware.

@weavejester
Copy link
Contributor

Static files are served with middleware, so in order for gzip middleware to work, it needs to be outside the static file middleware. In all probability, the gzip middleware you're using is just ordered wrong.

Can you provide an example of your configuration, and what it looks like when you've preped it?

@Zaphodious
Copy link
Author

Zaphodious commented Apr 13, 2018

@weavejester
Copy link
Contributor

weavejester commented Apr 14, 2018

You can see that your prepped handler looks like this (at least when nicely formatted):

 :duct.core/handler
 {:router     #ig/ref :key :duct/router
  :middleware [#ig/ref :anathema-re.middleware/gzip
               #ig/ref :duct.middleware.web/not-found
               #ig/ref :duct.middleware.web/webjars
               #ig/ref :duct.middleware.web/defaults
               #ig/ref :duct.middleware.web/log-requests
               #ig/ref :duct.middleware.web/log-errors
               #ig/ref :duct.middleware.web/stacktrace]}

The GZip middleware is appearing at the innermost layer, when we want it almost at the outermost layer. Ideally we still want errors and logging to be further out still, so if something goes wrong with the GZip middleware we'll have error messages in our logs.

 :duct.core/handler
 {:router     #ig/ref :key :duct/router
  :middleware [#ig/ref :duct.middleware.web/not-found
               #ig/ref :duct.middleware.web/webjars
               #ig/ref :duct.middleware.web/defaults
               #ig/ref :anathema-re.middleware/gzip
               #ig/ref :duct.middleware.web/log-requests
               #ig/ref :duct.middleware.web/log-errors
               #ig/ref :duct.middleware.web/stacktrace]}

To achieve this, add in the parts of the middleware that are different to your configuration. Middleware is checked to be distinct by the module, so the order should be able to be overridden:

 :duct.core/handler
 {:middleware [#ig/ref :duct.middleware.web/not-found
               #ig/ref :duct.middleware.web/webjars
               #ig/ref :duct.middleware.web/defaults
               #ig/ref :anathema-re.middleware/gzip]}

We can leave off the :router key and the final 3 middleware refs, since they're the same. This should work, but I haven't had chance to try it.

@Zaphodious
Copy link
Author

This seems to have worked. I'll post another comment here if I can find any problems with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants