koajs / koajs/send

[feat] DESCRIPTIVE TITLE

Open
#150 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
433
Forks
86
PR merge metrics
No merged PRs in 30d

Description

Hello, I am from China, my English is not very good, this article is translated by Google, please forgive me if you can't understand it.

Can you support negotiated cache? Automatically generate and add ETag and Last-Modified request headers, and process If-Modified-Since and If-None-Match request headers to implement this function, which meets the basic functions of a static resource server.

Currently I implement this function like this:

```js
const Koa = require('koa')
const static = require('@koa/static') // koa-send is used internally

const app = new Koa()

let koaStaticMiddleware

app.use((ctx, next) => {
if (!koaStaticMiddleware) {
koaStaticMiddleware = static('./public', {
setHeaders(res, path, stats) {
const fileHash = getFileHash(path)
res.setHeader('Cache-Control', 'no-cache')
res.setHeader('ETag', fileHash)
res.setHeader('Last-Modified', stats.mtime.toUTCString())

const etag = ctx.headers['if-none-match']
const lastModified = ctx.headers['if-modified-since']

if (
(etag && etag === fileHash) ||
(lastModified && lastModified === stats.mtime.toUTCString())
) {
ctx.throw(304, 'Not Modified')
}
}
})
}
return koaStaticMiddleware(ctx, next)
})

function getFileHash(filePath) {
// ...
}
```

Contributor guide

Open the contributing guide

Research direction

Start by tracing the static middleware and its internal koa-send path, since the issue identifies that implementation boundary. Verify how static files are served, then define completion as generating ETag and Last-Modified headers and handling matching If-Modified-Since and If-None-Match requests with a 304 response.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.