mirror of
https://github.com/fmhy/edit.git
synced 2025-07-30 07:42:18 +10:00
feat(api): utilise more caching for single-page route
This commit is contained in:
parent
4a6f20c12c
commit
4d4311d312
5 changed files with 1317 additions and 875 deletions
|
@ -15,8 +15,10 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { fetcher } from 'itty-fetcher'
|
import { fetcher } from 'itty-fetcher'
|
||||||
|
import { createStorage } from 'unstorage'
|
||||||
|
import cloudflareKVBindingDriver from 'unstorage/drivers/cloudflare-kv-binding'
|
||||||
|
|
||||||
// Look inside tbe docs directory
|
// Look inside the docs directory
|
||||||
const GITHUB_REPO = 'https://api.github.com/repos/fmhy/edit/contents/docs/'
|
const GITHUB_REPO = 'https://api.github.com/repos/fmhy/edit/contents/docs/'
|
||||||
const EXCLUDE_FILES = [
|
const EXCLUDE_FILES = [
|
||||||
'README.md',
|
'README.md',
|
||||||
|
@ -45,6 +47,10 @@ interface File {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
|
const markdownStorage = createStorage({
|
||||||
|
driver: cloudflareKVBindingDriver({ binding: 'STORAGE' })
|
||||||
|
})
|
||||||
|
|
||||||
let body = '<!-- This is autogenerated content, do not edit manually. -->\n'
|
let body = '<!-- This is autogenerated content, do not edit manually. -->\n'
|
||||||
const f = fetcher({
|
const f = fetcher({
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -54,7 +60,13 @@ export default defineEventHandler(async (event) => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Fetch the list of files in the repository
|
// Fetch the list of files in the repository
|
||||||
const files = await f.get<File[]>(GITHUB_REPO)
|
const indexCacheKey = "INDEX"
|
||||||
|
let files = await markdownStorage.getItem<File[]>(indexCacheKey)
|
||||||
|
|
||||||
|
if (!files) {
|
||||||
|
files = await f.get(GITHUB_REPO)
|
||||||
|
await markdownStorage.setItem(indexCacheKey, files, { ttl: 60 * 60 * 24 * 7 })
|
||||||
|
}
|
||||||
|
|
||||||
// Filter out the excluded files and non-markdown files
|
// Filter out the excluded files and non-markdown files
|
||||||
const markdownFiles = files.filter((file: File) => {
|
const markdownFiles = files.filter((file: File) => {
|
||||||
|
@ -67,12 +79,17 @@ export default defineEventHandler(async (event) => {
|
||||||
return isMarkdownFile && !isExcludedFile && !isInExcludedDirectory
|
return isMarkdownFile && !isExcludedFile && !isInExcludedDirectory
|
||||||
})
|
})
|
||||||
|
|
||||||
// console.info(markdownFiles.map((f) => f.name))
|
// Fetch and concatenate the contents of the markdown files with caching
|
||||||
|
|
||||||
// Fetch and concatenate the contents of the markdown files
|
|
||||||
const contents = await Promise.all(
|
const contents = await Promise.all(
|
||||||
markdownFiles.map(async (file: File) => {
|
markdownFiles.map(async (file: File) => {
|
||||||
|
const cached = await markdownStorage.getItem(file.name)
|
||||||
|
if (cached) return cached
|
||||||
|
|
||||||
const content = await f.get<string>(file.download_url)
|
const content = await f.get<string>(file.download_url)
|
||||||
|
if (content) {
|
||||||
|
await markdownStorage.setItem(file.name, content, { ttl: 60 * 60 })
|
||||||
|
}
|
||||||
|
|
||||||
return content
|
return content
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
@ -86,6 +103,9 @@ export default defineEventHandler(async (event) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// biome-ignore lint/correctness/noUndeclaredVariables: <explanation>
|
// biome-ignore lint/correctness/noUndeclaredVariables: <explanation>
|
||||||
appendResponseHeader(event, 'content-type', 'text/markdown;charset=utf-8')
|
appendResponseHeaders(event, {
|
||||||
|
'content-type': 'text/markdown;charset=utf-8',
|
||||||
|
'cache-control': 'public, max-age=3600'
|
||||||
|
})
|
||||||
return body
|
return body
|
||||||
})
|
})
|
||||||
|
|
|
@ -15,6 +15,8 @@
|
||||||
*/
|
*/
|
||||||
//https://nitro.unjs.io/config
|
//https://nitro.unjs.io/config
|
||||||
export default defineNitroConfig({
|
export default defineNitroConfig({
|
||||||
|
preset: 'cloudflare_module',
|
||||||
|
compatibilityDate: '2024-11-01',
|
||||||
runtimeConfig: {
|
runtimeConfig: {
|
||||||
WEBHOOK_URL: process.env.WEBHOOK_URL
|
WEBHOOK_URL: process.env.WEBHOOK_URL
|
||||||
},
|
},
|
||||||
|
|
|
@ -30,11 +30,11 @@
|
||||||
"feed": "^4.2.2",
|
"feed": "^4.2.2",
|
||||||
"itty-fetcher": "^0.9.4",
|
"itty-fetcher": "^0.9.4",
|
||||||
"nitro-cors": "^0.7.1",
|
"nitro-cors": "^0.7.1",
|
||||||
"nitropack": "^2.9.7",
|
"nitropack": "^2.10.4",
|
||||||
"nprogress": "^0.2.0",
|
"nprogress": "^0.2.0",
|
||||||
"pathe": "^1.1.2",
|
"pathe": "^1.1.2",
|
||||||
"unocss": "^0.63.4",
|
"unocss": "^0.63.4",
|
||||||
"vitepress": "^1.4.1",
|
"vitepress": "^1.5.0",
|
||||||
"vue": "^3.5.12",
|
"vue": "^3.5.12",
|
||||||
"x-satori": "^0.2.0",
|
"x-satori": "^0.2.0",
|
||||||
"zod": "^3.23.8"
|
"zod": "^3.23.8"
|
||||||
|
@ -61,7 +61,7 @@
|
||||||
"unplugin-auto-import": "^0.18.3",
|
"unplugin-auto-import": "^0.18.3",
|
||||||
"vite-plugin-optimize-exclude": "^0.0.1",
|
"vite-plugin-optimize-exclude": "^0.0.1",
|
||||||
"vite-plugin-terminal": "^1.2.0",
|
"vite-plugin-terminal": "^1.2.0",
|
||||||
"wrangler": "^3.80.5"
|
"wrangler": "^3.99.0"
|
||||||
},
|
},
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
"peerDependencyRules": {
|
"peerDependencyRules": {
|
||||||
|
|
2145
pnpm-lock.yaml
generated
2145
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
|
@ -2,12 +2,17 @@ name = "api"
|
||||||
main = ".output/server/index.mjs"
|
main = ".output/server/index.mjs"
|
||||||
workers_dev = false
|
workers_dev = false
|
||||||
account_id = "02f3b11d8d1017a20f95de4ba88fb5d6"
|
account_id = "02f3b11d8d1017a20f95de4ba88fb5d6"
|
||||||
compatibility_date = "2022-09-10"
|
compatibility_flags = [ "nodejs_compat" ]
|
||||||
|
compatibility_date = "2024-11-01"
|
||||||
|
|
||||||
routes = [
|
routes = [
|
||||||
{ pattern = "api.fmhy.net", custom_domain = true, zone_id = "b4212298934f7c1998b4d15f6d261715" },
|
{ pattern = "api.fmhy.net", custom_domain = true, zone_id = "b4212298934f7c1998b4d15f6d261715" },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[kv_namespaces]]
|
||||||
|
binding = "STORAGE"
|
||||||
|
id = "6f18adea26a64d6b8858ffbdfd3f4cf2"
|
||||||
|
|
||||||
[[unsafe.bindings]]
|
[[unsafe.bindings]]
|
||||||
name = "RATE_LIMITER"
|
name = "RATE_LIMITER"
|
||||||
type = "ratelimit"
|
type = "ratelimit"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue