Skip to content

Commit 3a31da5

Browse files
committed
Enable workers cache and bump wrangler
1 parent 9f437f7 commit 3a31da5

9 files changed

Lines changed: 16507 additions & 106 deletions

File tree

‎bun.lock‎

Lines changed: 40 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎proxy/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"scripts": {
55
"build": "bunx esbuild --bundle --outdir=dist --format=esm src/index.ts",
66
"buildContributors": "bun bin/buildContributors.ts",
7+
"cf:types": "wrangler types",
78
"checkContributors": "bun bin/checkContributors.ts",
89
"combineDocs": "bun bin/combineContent.ts",
910
"buildDocs": "bun bin/buildAssets.ts && bun bin/buildSitemap.ts",
@@ -23,7 +24,7 @@
2324
"@cloudflare/workers-types": "4.20250922.0",
2425
"@types/bun": "1.3.3",
2526
"@typescript/native-preview": "7.0.0-dev.20250619.1",
26-
"wrangler": "4.77.0"
27+
"wrangler": "4.158.0"
2728
},
2829
"dependencies": {
2930
"@clickhouse/client-web": "1.11.1",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { afterEach, describe, expect, spyOn, test } from 'bun:test'
2+
import type { Context } from 'hono'
3+
import type { Env } from '../types'
4+
import { fetchContent } from './fetchContent'
5+
6+
describe('fetchContent', () => {
7+
let fetchSpy: ReturnType<typeof spyOn> | undefined
8+
9+
afterEach(() => {
10+
fetchSpy?.mockRestore()
11+
})
12+
13+
test('should return page content with Cache-Control header when fetched from remote', async () => {
14+
const mockRepoItems = [
15+
{
16+
path: 'test',
17+
label: 'Test Item',
18+
},
19+
]
20+
21+
const mockMarkdown = `---
22+
title: Remote Test
23+
description: Remote Description
24+
---
25+
26+
# Remote Content
27+
This is remote content.`
28+
29+
const mockFetch = Object.assign(
30+
async (input: RequestInfo | URL) => {
31+
const url = typeof input === 'string' ? input : input.toString()
32+
if (url.endsWith('docs/index.json')) {
33+
return new Response(JSON.stringify(mockRepoItems), {
34+
status: 200,
35+
headers: { 'Content-Type': 'application/json' },
36+
})
37+
}
38+
return new Response(mockMarkdown, {
39+
status: 200,
40+
headers: { 'Content-Type': 'text/plain' },
41+
})
42+
},
43+
{ preconnect: () => {} },
44+
)
45+
46+
fetchSpy = spyOn(globalThis, 'fetch').mockImplementation(mockFetch)
47+
48+
const response = await fetchContent({
49+
params: {
50+
owner: 'other',
51+
repository: 'repo',
52+
branch: 'feat',
53+
path: 'test-item',
54+
},
55+
ctx: {} as Context<{ Bindings: Env }>,
56+
})
57+
58+
expect(response.status).toBe(200)
59+
expect(response.headers.get('Cache-Control')).toBe('public, max-age=600')
60+
const body = (await response.json()) as {
61+
seo: { title?: string }
62+
meta: { title?: string }
63+
}
64+
expect(body.seo.title).toBe('Remote Test')
65+
expect(body.meta.title).toBe('Test Item')
66+
})
67+
68+
test('should return 500 when remote fetch fails', async () => {
69+
const consoleSpy = spyOn(console, 'log').mockImplementation(() => {})
70+
71+
fetchSpy = spyOn(globalThis, 'fetch').mockResolvedValue(
72+
new Response('Not Found', { status: 404 }),
73+
)
74+
75+
const response = await fetchContent({
76+
params: {
77+
owner: 'other',
78+
repository: 'repo',
79+
branch: 'feat',
80+
path: 'test',
81+
},
82+
ctx: {} as Context<{ Bindings: Env }>,
83+
})
84+
85+
expect(response.status).toBe(500)
86+
consoleSpy.mockRestore()
87+
})
88+
})

‎proxy/src/routes/fetchContent.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ export const fetchContent = async ({
5858

5959
pageData = await fetchPageData({ path: pagePath, title: pageTitle })
6060

61-
return json({ ...pageData })
61+
return json(
62+
{ ...pageData },
63+
{
64+
headers: {
65+
'Cache-Control': `public, max-age=${10 * 60}`,
66+
},
67+
},
68+
)
6269
} catch (err) {
6370
// eslint-disable-next-line no-console
6471
console.log(err)

‎proxy/src/routes/fetchMenu.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { afterEach, describe, expect, spyOn, test } from 'bun:test'
2+
import type { Context } from 'hono'
3+
import type { Env } from '../types'
4+
import { fetchMenu } from './fetchMenu'
5+
6+
describe('fetchMenu', () => {
7+
let fetchSpy: ReturnType<typeof spyOn> | undefined
8+
9+
afterEach(() => {
10+
fetchSpy?.mockRestore()
11+
})
12+
13+
test('should return menu with Cache-Control header when fetched from remote', async () => {
14+
const mockRepoItems = [
15+
{
16+
path: 'test',
17+
label: 'Test Item',
18+
},
19+
]
20+
21+
fetchSpy = spyOn(globalThis, 'fetch').mockResolvedValue(
22+
new Response(JSON.stringify(mockRepoItems), {
23+
status: 200,
24+
headers: { 'Content-Type': 'application/json' },
25+
}),
26+
)
27+
28+
const response = await fetchMenu({
29+
params: { owner: 'other', repository: 'repo', branch: 'feat' },
30+
ctx: {} as Context<{ Bindings: Env }>,
31+
})
32+
33+
expect(response.status).toBe(200)
34+
expect(response.headers.get('Cache-Control')).toBe('public, max-age=600')
35+
const body = (await response.json()) as { menu: unknown[] }
36+
expect(body.menu).toBeDefined()
37+
expect(body.menu.length).toBe(1)
38+
})
39+
40+
test('should return 500 when fetch fails', async () => {
41+
const consoleSpy = spyOn(console, 'log').mockImplementation(() => {})
42+
43+
fetchSpy = spyOn(globalThis, 'fetch').mockResolvedValue(
44+
new Response('Not Found', { status: 404 }),
45+
)
46+
47+
const response = await fetchMenu({
48+
params: { owner: 'other', repository: 'repo', branch: 'feat' },
49+
ctx: {} as Context<{ Bindings: Env }>,
50+
})
51+
52+
expect(response.status).toBe(500)
53+
consoleSpy.mockRestore()
54+
})
55+
})

‎proxy/src/routes/fetchMenu.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ export const fetchMenu = async ({
1717
throw new Error('Could not fetch menu items.')
1818
}
1919

20-
return json({ menu })
20+
return json(
21+
{ menu },
22+
{
23+
headers: {
24+
'Cache-Control': `public, max-age=${10 * 60}`,
25+
},
26+
},
27+
)
2128
} catch (err) {
2229
// eslint-disable-next-line no-console
2330
console.log(err)

‎proxy/tsconfig.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/* Specify how TypeScript looks up a file from a given module specifier. */
1515
"moduleResolution": "Bundler",
1616
/* Specify type package names to be included without being referenced in a source file. */
17-
"types": ["@cloudflare/workers-types/2023-07-01", "bun-types"],
17+
"types": ["bun-types"],
1818
/* Enable importing .json files */
1919
"resolveJsonModule": true,
2020

@@ -40,5 +40,5 @@
4040
"skipLibCheck": true,
4141
"verbatimModuleSyntax": true
4242
},
43-
"include": ["src/**/*.ts", "bin/**/*.ts"]
43+
"include": ["src/**/*.ts", "bin/**/*.ts", "worker-configuration.d.ts"]
4444
}

0 commit comments

Comments
 (0)