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

allow different cache times for different urls #94

Closed
lanterndev opened this issue Jan 25, 2014 · 11 comments
Closed

allow different cache times for different urls #94

lanterndev opened this issue Jan 25, 2014 · 11 comments

Comments

@lanterndev
Copy link

From http-party/http-server#63:

Currently http-server allows you to configure the "cache-control: max-age" value for all urls. But often you want a short max-age for e.g. /{index.html} and a long max-age for urls to versioned assets like /styles/f35ac111.main.css.

Would you accept a patch to allow specifying different max-ages for different urls?

I realized http-server is just using ecstatic under the hood, which also only allows setting a single max-age for all responses.

Any suggestions on the simplest approach to giving versioned assets a longer max-age than non-versioned assets? Would be happy to submit a patch if there is interest in supporting this.

@jfhbrook
Copy link
Owner

I'm down to support it, but I'm not sure what the API would look like. Presumably it would be a hash keyed by req.url.

@lanterndev
Copy link
Author

I'm down to support it

excellent, thanks!

but I'm not sure what the API would look like. Presumably it would be a hash keyed by req.url.

The most general way might be a hash keyed by regular expressions that the url is successively matched against (and /.*/ could come last). But if it's simpler, just giving a long max-age to every asset with a name like a1b2c3d4.filename.ext would probably work for many use cases.

@jfhbrook
Copy link
Owner

Best to do it the right way, which sounds like hashes.

@grrowl
Copy link

grrowl commented Sep 2, 2014

+1, my use case being we have a directory with largeish, never changing assets in them, but the rest of the assets should be ~5min cache time.

I haven't looked at the code yet, but my thoughts would be to check if opts.cache is a function on request, if so call it with res, req and allow it to return a value in seconds, to be treated the same as the existing option.

Is this good or is there a more ecstatic way of doing things? I can submit a PR within the next week, since I expect this to be an important optimisation step I'll have to tackle anyway.

@jfhbrook
Copy link
Owner

jfhbrook commented Sep 2, 2014

function on request

I'm not sure what you mean here.

I assumed that this would be a backwards-compatible change to opts.cache where keys can be regexps or some other sort of route matcher, and values would correspond to the cache value. But I haven't thought about it too hard.

@grrowl
Copy link

grrowl commented Sep 2, 2014

Sorry, I was a little rushed. Something like this:

var opts = {
  cache: function (req) {
    if (req.url.indexOf('assets/') === 0)
      return 21600;
    else
      return 3600;
  }
}

It would provide more flexibility and better performance than restricting matching to regexes IMO.

@jfhbrook
Copy link
Owner

jfhbrook commented Sep 3, 2014

Yeah okay, I see what you're saying. Seems easier to implement for sure.

Yeah, I'd take a PR for that.

@TehShrike
Copy link
Contributor

That pull request never materialized apparently? :-) I could use this, and the function mechanism seems perfect to me.

@jfhbrook
Copy link
Owner

Yeah, to my recollection no PR.

@grrowl
Copy link

grrowl commented Oct 23, 2015

👍 unfortunately we changed course and didn't need this functionality, kudos to @TehShrike for addressing this

@TehShrike
Copy link
Contributor

Oh, I don't care about you guys, I need this! ;-) <3

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

4 participants