magnars / optimus

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

Customise caching for serving live assets #34

Open antonyshchenko opened 9 years ago

antonyshchenko commented 9 years ago

I would like to tweak the way assets are cached by optimus.strategies/serve-live-assets.

It would be nice to have an ability to pass through the options a function that would wrap get-optimized-assets defined in optimus.strategies/serve-live-assets and implement it's own caching.

The reason I need this is that for me serving an uncached assets is slow (page loading takes up to 5 seconds, probably because of several regular expressions in assets definition). TTL caching is not an option for me, because I wan't to see changes instantly when I edit the files. So I decided to implement a strategy with directory watching and cache invalidation when files in directory are changed. So far it's working fine for me, but I had to copy-paste all private functions from optimus.strategies.

magnars commented 9 years ago

The strategy you have made sounds like an excellent addition to the strategies offered by Optimus, and I would be happy to accept a pull request with the feature.

antonyshchenko commented 9 years ago

Great! Then I will implement it and submit a PR in a few days.

magnars commented 9 years ago

:+1:

magnars commented 9 years ago

@env0der Hi Anton! Any progress on this?

antonyshchenko commented 9 years ago

@magnars , could you please take a look at my commit? It's a rough idea (quick hack) that works for me.

How can I get rid of hardcoded "resources" path? Is there a function in optimus that returns a base directory for assets?

Tell me if you like the idea and I will polish the code and write tests.

magnars commented 9 years ago

Optimus serves assets from the classpath, so the file location is unknown. We do pass in a folder (public most commonly) where the assets are, but these could be coming from anywhere.

To make this work, we'll have to presume that people are serving their assets from a single directory on disk, make resources a default, and add an option to override it. We'll also have to warn people not to use this strategy in production. :)

Thoughts?

magnars commented 9 years ago

Any progress? :)

antonyshchenko commented 9 years ago

Here it is. Sorry for long waiting - I have time for this only during weekends.

magnars commented 9 years ago

No worries, mate :) Thanks for taking the time!