feat: add a cache option (#338)

* feat: add query param `cache`

* chore: bump dependencies

* chore: bump semver to 7.1.1

* chore: set default cdn cache to 6hrs

* chore: better wording
pull/339/head
晋晓炜 Amio / 2020-01-19 00:08:00 +08:00 zatwierdzone przez GitHub
rodzic fd3b1d0aff
commit f2e179e827
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 1022 dodań i 569 usunięć

Wyświetl plik

@ -43,6 +43,11 @@ export default function HomeIntro ({ isFlat = false }) {
Custom badge scale Custom badge scale
<a href='/badge/docker/v1.2.3/blue?icon=docker&scale=2'>e.g.</a> <a href='/badge/docker/v1.2.3/blue?icon=docker&scale=2'>e.g.</a>
</li> </li>
<li>
<code>cache</code>
Live badge are cached in cdn for 24hrs (86400), you may limit it to a minimum of 5min (300).
<a href='/npm/dm/express?cache=600'>e.g.</a>
</li>
</ul> </ul>
<h3>Advanced usage</h3> <h3>Advanced usage</h3>

Wyświetl plik

@ -13,10 +13,10 @@ type ServeBadgeOptions = {
} }
export default function (req: IncomingMessage, res: ServerResponse, options: ServeBadgeOptions) { export default function (req: IncomingMessage, res: ServerResponse, options: ServeBadgeOptions) {
const { code = 200, sMaxAge = 300, query = {}, params } = options const { code = 200, sMaxAge = 21600, query = {}, params } = options
const { subject, status, color } = params const { subject, status, color } = params
const { label, labelColor, icon, iconWidth, list, scale } = query const { label, labelColor, icon, iconWidth, list, scale, cache } = query
const _icon = resolveIcon(icon, iconWidth) const _icon = resolveIcon(icon, iconWidth)
// TODO: review usage of list // TODO: review usage of list
@ -33,8 +33,8 @@ export default function (req: IncomingMessage, res: ServerResponse, options: Ser
scale: parseFloat(scale || '1'), scale: parseFloat(scale || '1'),
}) })
const cacheControl = `public, max-age=120, s-maxage=${sMaxAge}, stale-while-revalidate=86400` const cacheMaxAge = cache ? Math.max(parseInt(cache as string) || 0, 300) : sMaxAge
res.setHeader('Cache-Control', cacheControl) res.setHeader('Cache-Control', `public, max-age=120, s-maxage=${cacheMaxAge}, stale-while-revalidate=86400`)
res.setHeader('Content-Type', 'image/svg+xml;charset=utf-8') res.setHeader('Content-Type', 'image/svg+xml;charset=utf-8')
res.statusCode = code res.statusCode = code
res.end(badge) res.end(badge)

1558
package-lock.json wygenerowano

Plik diff jest za duży Load Diff

Wyświetl plik

@ -22,8 +22,8 @@
] ]
}, },
"dependencies": { "dependencies": {
"@sentry/integrations": "^5.11.0", "@sentry/integrations": "^5.11.1",
"@sentry/node": "^5.11.0", "@sentry/node": "^5.11.1",
"badgen": "^3.0.1", "badgen": "^3.0.1",
"badgen-icons": "^0.13.0", "badgen-icons": "^0.13.0",
"byte-size": "^6.2.0", "byte-size": "^6.2.0",
@ -37,7 +37,7 @@
"my-way": "^2.0.0", "my-way": "^2.0.0",
"original-url": "^1.2.3", "original-url": "^1.2.3",
"react-debounce-render": "^5.0.0", "react-debounce-render": "^5.0.0",
"semver": "^6.3.0", "semver": "^7.1.1",
"serve-handler": "^6.1.2", "serve-handler": "^6.1.2",
"serve-marked": "^2.0.2", "serve-marked": "^2.0.2",
"universal-analytics": "^0.4.20", "universal-analytics": "^0.4.20",
@ -49,26 +49,26 @@
"@types/got": "^9.6.9", "@types/got": "^9.6.9",
"@types/lodash.debounce": "^4.0.6", "@types/lodash.debounce": "^4.0.6",
"@types/micro": "^7.3.3", "@types/micro": "^7.3.3",
"@types/node": "^12.12.24", "@types/node": "^12.12.25",
"@types/react": "^16.9.17", "@types/react": "^16.9.17",
"@types/react-dom": "^16.9.4", "@types/react-dom": "^16.9.4",
"@types/semver": "^6.2.0", "@types/semver": "^6.2.0",
"@types/supertest": "^2.0.8", "@types/supertest": "^2.0.8",
"@types/url-parse": "^1.4.3", "@types/url-parse": "^1.4.3",
"@typescript-eslint/eslint-plugin": "^2.15.0", "@typescript-eslint/eslint-plugin": "^2.16.0",
"@typescript-eslint/parser": "^2.15.0", "@typescript-eslint/parser": "^2.16.0",
"@zeit/next-typescript": "^1.1.1", "@zeit/next-typescript": "^1.1.1",
"csstype": "^2.6.8", "csstype": "^2.6.8",
"eslint": "^6.8.0", "eslint": "^6.8.0",
"eslint-plugin-react": "^7.17.0", "eslint-plugin-react": "^7.18.0",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"next": "^9.1.7", "next": "^9.2.0",
"nodemon": "^2.0.2", "nodemon": "^2.0.2",
"react": "^16.12.0", "react": "^16.12.0",
"react-dom": "^16.12.0", "react-dom": "^16.12.0",
"supertest": "^4.0.2", "supertest": "^4.0.2",
"ts-node": "^8.6.1", "ts-node": "^8.6.2",
"typescript": "^3.7.4" "typescript": "^3.7.5"
}, },
"engines": { "engines": {
"node": "12.x" "node": "12.x"