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

serveStatic should apply Last-Modified header #189

Open
t-mart opened this issue Aug 20, 2024 · 3 comments
Open

serveStatic should apply Last-Modified header #189

t-mart opened this issue Aug 20, 2024 · 3 comments

Comments

@t-mart
Copy link

t-mart commented Aug 20, 2024

Clients can make requests with an If-Modified-Since header to indicate that they only want a data-ful response (200) if the file has changed since the date value in that header. If the file has not changed since that date, the server can respond with a 304 and empty body.

Clients set the value of the If-Modified-Since from an earlier response's Last-Modified header, which Hono would need to provide.

src/serve-static.ts:serveStatic doesn't seem to implement this, but seems very close to being able to. We already pull the file's stats for various size headers and the Date header.

It seems like we'd need to do two things:

  1. Add another header for Last-Modified. Something perhaps like:
c.header('Last-Modified', stats.mTime.toUTCString())
  1. Add logic to read a If-Modified-Since header and return 304 in the appropriate case.

To maintain backwards-compatibility, a new optional property could be added to the ServeStaticOptions object, which is the parameter to serveStatic, to gate this behavior. For now, I guess it could be a boolean, named something like lastModified (I'm not sure what would be a good name here).

@yusukebe
Copy link
Member

Hi @t-mart

Quick response. I think we can implement it as a middleware like the ETag Middleware.

@t-mart
Copy link
Author

t-mart commented Aug 21, 2024

@yusukebe, that would make it the most "pluggable", yes.

However, with a general request/response (not a filesystem mapping like serveStatic), I'm not sure how the server would (statelessly) know how to set the Last-Modified header correctly.

@yusukebe
Copy link
Member

@t-mart

However, with a general request/response (not a filesystem mapping like serveStatic), I'm not sure how the server would (statelessly) know how to set the Last-Modified header correctly.

Indeed. As you mentioned, adding this feature to serveStatic might be better. And it's a little bit of code.

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