also looking at this
fix(serve): render every request when cache is false
#2106
2 files
+109
-4
| @@ -150,6 +150,11 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> { | ||
| 150 | 150 | */ |
| 151 | 151 | const fileCache = new Map<string, { content: string, mtime: number, status?: number, headers?: Record<string, string> }>() |
| 152 | 152 | |
| 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 | ||
| 153 | 158 | /** |
| 154 | 159 | * Process a stx file |
| 155 | 160 | */ |
| @@ -158,7 +163,7 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> { | ||
| 158 | 163 | const cacheKey = `${filePath}:${stats.mtimeMs}` |
| 159 | 164 | |
| 160 | 165 | // Check cache |
| 161 | const cached = fileCache.get(cacheKey) | |
| 166 | const cached = useFileCache() ? fileCache.get(cacheKey) : undefined | |
| 162 | 167 | if (cached) { |
| 163 | 168 | return { content: cached.content, status: cached.status, headers: cached.headers } |
| 164 | 169 | } |
| @@ -262,7 +267,8 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> { | ||
| 262 | 267 | const headers = readResponseHeaders(context) |
| 263 | 268 | |
| 264 | 269 | // 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 }) | |
| 266 | 272 | |
| 267 | 273 | return { content: output, status, headers } |
| 268 | 274 | } |
| @@ -275,7 +281,7 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> { | ||
| 275 | 281 | const cacheKey = `${filePath}:${stats.mtimeMs}` |
| 276 | 282 | |
| 277 | 283 | // Check cache |
| 278 | const cached = fileCache.get(cacheKey) | |
| 284 | const cached = useFileCache() ? fileCache.get(cacheKey) : undefined | |
| 279 | 285 | if (cached) { |
| 280 | 286 | return cached.content |
| 281 | 287 | } |
| @@ -284,7 +290,8 @@ export async function serve(options: ServeOptions = {}): Promise<ServeResult> { | ||
| 284 | 290 | const { content } = await readMarkdownFile(filePath, stxOptions) |
| 285 | 291 | |
| 286 | 292 | // Cache result |
| 287 | fileCache.set(cacheKey, { content, mtime: stats.mtimeMs }) | |
| 293 | if (useFileCache()) | |
| 294 | fileCache.set(cacheKey, { content, mtime: stats.mtimeMs }) | |
| 288 | 295 | |
| 289 | 296 | return content |
| 290 | 297 | } |