ReviewOS

also looking at this

stacks/stx

fix(serve): render every request when cache is false

#2106
Open mkantautas wants to merge fix/serve-cache-option into main
2 files +109 -4
packages/stx/src/serve.tsmodified+11-4
Changes to packages/stx/src/serve.ts
@@ -150,6 +150,11 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> {
150150 */
151151 const fileCache = new Map<string, { content: string, mtime: number, status?: number, headers?: Record<string, string> }>()
152152
153 /** `cache: false` in the stx options turns this cache off. Read per use: the config can reload. */
154 function useFileCache(): boolean {
155 return stxOptions.cache !== false
156 }
157
153158 /**
154159 * Process a stx file
155160 */
@@ -158,7 +163,7 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> {
158163 const cacheKey = `${filePath}:${stats.mtimeMs}`
159164
160165 // Check cache
161 const cached = fileCache.get(cacheKey)
166 const cached = useFileCache() ? fileCache.get(cacheKey) : undefined
162167 if (cached) {
163168 return { content: cached.content, status: cached.status, headers: cached.headers }
164169 }
@@ -262,7 +267,8 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> {
262267 const headers = readResponseHeaders(context)
263268
264269 // Cache result
265 fileCache.set(cacheKey, { content: output, mtime: stats.mtimeMs, status, headers })
270 if (useFileCache())
271 fileCache.set(cacheKey, { content: output, mtime: stats.mtimeMs, status, headers })
266272
267273 return { content: output, status, headers }
268274 }
@@ -275,7 +281,7 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> {
275281 const cacheKey = `${filePath}:${stats.mtimeMs}`
276282
277283 // Check cache
278 const cached = fileCache.get(cacheKey)
284 const cached = useFileCache() ? fileCache.get(cacheKey) : undefined
279285 if (cached) {
280286 return cached.content
281287 }
@@ -284,7 +290,8 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> {
284290 const { content } = await readMarkdownFile(filePath, stxOptions)
285291
286292 // Cache result
287 fileCache.set(cacheKey, { content, mtime: stats.mtimeMs })
293 if (useFileCache())
294 fileCache.set(cacheKey, { content, mtime: stats.mtimeMs })
288295
289296 return content
290297 }