Skip to content

serveStatic should apply Last-Modified header #189

@t-mart

Description

@t-mart

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions