aantron / dream

Tidy, feature-complete Web framework
https://aantron.github.io/dream/
MIT License
1.6k stars 129 forks source link

[RFC] Assets hashing #106

Open thangngoc89 opened 3 years ago

thangngoc89 commented 3 years ago

As I said on Discord, I wanted to write a ppx for static assets hashing and cache bursting. The idea is like this:

let css_url = [%assets "style.css" ]

However after careful consideration, I decided that it would be a runtime library rather than build time. Here are some reasons I can think of:

What I am proposing would be configuration file in JSON format like this:

{
  prefix: "/" // this could be CDN URL or absolute path to where you mount your static assets
  assets: {
    "style.css":  "style.content_hash.css",
    "script.css": "script.content_hash.css"
  }
}

How is this loaded is up to the implementor, could be a file during application start up, could be via POST request.

TODO:

What do you think?

thangngoc89 commented 3 years ago

On the other hand, using a build time approach, one could inline the content of small images directly to string like webpack/rollup does. Or one could write a ppx that does different things based on the arguments. But I don’t know how to write ppxes that is more complex than replacing a string with a string 😂

thangngoc89 commented 3 years ago

On the topic of hashing, rollup uses

dream is using mirage-digestif but it's library for cryptographic hashes. I would prefer a non-cryptographic hash for tasks like this

aantron commented 3 years ago

It all looks feasible. It looks largely independent from Dream itself. We would basically just call out from templates (or anywhere else) to some library to figure out the real name of an asset, right? I guess that library would need to know how to read the output of the hasher, and serve as the "runtime library" linked into server deployments.

thangngoc89 commented 3 years ago

@aantron yes. It should work independent of dream but I only interested in build for dream right now. So I want to open discussions here and looking for comments/feedbacks on the design