@jasonshimmy/vite-plugin-cer-app 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/IMPLEMENTATION_PLAN.md +2 -2
- package/commits.txt +1 -1
- package/dist/cli/commands/preview-isr.d.ts +9 -0
- package/dist/cli/commands/preview-isr.d.ts.map +1 -1
- package/dist/cli/commands/preview-isr.js +16 -0
- package/dist/cli/commands/preview-isr.js.map +1 -1
- package/dist/cli/commands/preview.d.ts.map +1 -1
- package/dist/cli/commands/preview.js +44 -1
- package/dist/cli/commands/preview.js.map +1 -1
- package/dist/cli/create/templates/spa/package.json.tpl +2 -2
- package/dist/cli/create/templates/ssg/package.json.tpl +2 -2
- package/dist/cli/create/templates/ssr/package.json.tpl +2 -2
- package/dist/plugin/build-ssg.d.ts.map +1 -1
- package/dist/plugin/build-ssg.js +17 -3
- package/dist/plugin/build-ssg.js.map +1 -1
- package/dist/plugin/dev-server.d.ts.map +1 -1
- package/dist/plugin/dev-server.js +33 -0
- package/dist/plugin/dev-server.js.map +1 -1
- package/dist/plugin/virtual/routes.d.ts.map +1 -1
- package/dist/plugin/virtual/routes.js +24 -2
- package/dist/plugin/virtual/routes.js.map +1 -1
- package/dist/runtime/entry-server-template.d.ts +2 -2
- package/dist/runtime/entry-server-template.d.ts.map +1 -1
- package/dist/runtime/entry-server-template.js +57 -19
- package/dist/runtime/entry-server-template.js.map +1 -1
- package/dist/runtime/isr-handler.d.ts +40 -0
- package/dist/runtime/isr-handler.d.ts.map +1 -0
- package/dist/runtime/isr-handler.js +152 -0
- package/dist/runtime/isr-handler.js.map +1 -0
- package/dist/types/page.d.ts +14 -0
- package/dist/types/page.d.ts.map +1 -1
- package/docs/data-loading.md +69 -2
- package/docs/rendering-modes.md +66 -5
- package/docs/routing.md +33 -0
- package/e2e/cypress/e2e/error-boundary.cy.ts +43 -0
- package/e2e/cypress/e2e/isr-nested-runtime.cy.ts +6 -4
- package/e2e/cypress/e2e/per-route-render.cy.ts +70 -0
- package/e2e/kitchen-sink/app/error.ts +7 -2
- package/e2e/kitchen-sink/app/pages/loader-error-test.ts +13 -0
- package/e2e/kitchen-sink/app/pages/render-server-test.ts +12 -0
- package/e2e/kitchen-sink/app/pages/render-spa-test.ts +12 -0
- package/package.json +7 -3
- package/src/__tests__/cli/preview-isr.test.ts +44 -0
- package/src/__tests__/plugin/build-ssg-render.test.ts +46 -0
- package/src/__tests__/plugin/build-ssg.test.ts +126 -1
- package/src/__tests__/plugin/dev-server.test.ts +91 -0
- package/src/__tests__/plugin/entry-server-template.test.ts +76 -5
- package/src/__tests__/plugin/virtual/routes.test.ts +65 -0
- package/src/__tests__/runtime/isr-handler.test.ts +331 -0
- package/src/cli/commands/preview-isr.ts +19 -0
- package/src/cli/commands/preview.ts +46 -0
- package/src/cli/create/templates/spa/package.json.tpl +2 -2
- package/src/cli/create/templates/ssg/package.json.tpl +2 -2
- package/src/cli/create/templates/ssr/package.json.tpl +2 -2
- package/src/plugin/build-ssg.ts +15 -3
- package/src/plugin/dev-server.ts +33 -0
- package/src/plugin/virtual/routes.ts +24 -2
- package/src/runtime/entry-server-template.ts +57 -19
- package/src/runtime/isr-handler.ts +183 -0
- package/src/types/page.ts +14 -0
|
@@ -335,6 +335,71 @@ describe('generateRoutesCode — meta.transition', () => {
|
|
|
335
335
|
})
|
|
336
336
|
})
|
|
337
337
|
|
|
338
|
+
// ─── meta.render ──────────────────────────────────────────────────────────────
|
|
339
|
+
|
|
340
|
+
describe('generateRoutesCode — meta.render', () => {
|
|
341
|
+
beforeEach(() => {
|
|
342
|
+
vi.mocked(existsSync).mockReturnValue(true)
|
|
343
|
+
vi.mocked(scanDirectory).mockResolvedValue([])
|
|
344
|
+
vi.mocked(readFile).mockResolvedValue('' as never)
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
it('emits meta.render "server" when declared', async () => {
|
|
348
|
+
vi.mocked(scanDirectory).mockResolvedValue([`${PAGES}/dashboard.ts`])
|
|
349
|
+
vi.mocked(readFile).mockResolvedValue(
|
|
350
|
+
`export const meta = { render: 'server' }` as never,
|
|
351
|
+
)
|
|
352
|
+
const code = await generateRoutesCode(PAGES)
|
|
353
|
+
expect(code).toContain('render: "server"')
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
it('emits meta.render "spa" when declared', async () => {
|
|
357
|
+
vi.mocked(scanDirectory).mockResolvedValue([`${PAGES}/profile.ts`])
|
|
358
|
+
vi.mocked(readFile).mockResolvedValue(
|
|
359
|
+
`export const meta = { render: 'spa' }` as never,
|
|
360
|
+
)
|
|
361
|
+
const code = await generateRoutesCode(PAGES)
|
|
362
|
+
expect(code).toContain('render: "spa"')
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
it('emits meta.render "static" when declared', async () => {
|
|
366
|
+
vi.mocked(scanDirectory).mockResolvedValue([`${PAGES}/about.ts`])
|
|
367
|
+
vi.mocked(readFile).mockResolvedValue(
|
|
368
|
+
`export const meta = { render: 'static' }` as never,
|
|
369
|
+
)
|
|
370
|
+
const code = await generateRoutesCode(PAGES)
|
|
371
|
+
expect(code).toContain('render: "static"')
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
it('omits render meta when not declared', async () => {
|
|
375
|
+
vi.mocked(scanDirectory).mockResolvedValue([`${PAGES}/about.ts`])
|
|
376
|
+
vi.mocked(readFile).mockResolvedValue(
|
|
377
|
+
`component('page-about', () => html\`<h1>About</h1>\`)` as never,
|
|
378
|
+
)
|
|
379
|
+
const code = await generateRoutesCode(PAGES)
|
|
380
|
+
expect(code).not.toContain('render:')
|
|
381
|
+
})
|
|
382
|
+
|
|
383
|
+
it('ignores unknown render values', async () => {
|
|
384
|
+
vi.mocked(scanDirectory).mockResolvedValue([`${PAGES}/about.ts`])
|
|
385
|
+
vi.mocked(readFile).mockResolvedValue(
|
|
386
|
+
`export const meta = { render: 'unknown' }` as never,
|
|
387
|
+
)
|
|
388
|
+
const code = await generateRoutesCode(PAGES)
|
|
389
|
+
expect(code).not.toContain('render:')
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
it('can combine render with layout', async () => {
|
|
393
|
+
vi.mocked(scanDirectory).mockResolvedValue([`${PAGES}/dashboard.ts`])
|
|
394
|
+
vi.mocked(readFile).mockResolvedValue(
|
|
395
|
+
`export const meta = { render: 'server', layout: 'admin' }` as never,
|
|
396
|
+
)
|
|
397
|
+
const code = await generateRoutesCode(PAGES)
|
|
398
|
+
expect(code).toContain('render: "server"')
|
|
399
|
+
expect(code).toContain('layout: "admin"')
|
|
400
|
+
})
|
|
401
|
+
})
|
|
402
|
+
|
|
338
403
|
// ─── nested layouts (layoutChain) ────────────────────────────────────────────
|
|
339
404
|
|
|
340
405
|
describe('generateRoutesCode — layoutChain (nested layouts)', () => {
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
3
|
+
import { createIsrHandler } from '../../runtime/isr-handler.js'
|
|
4
|
+
|
|
5
|
+
// ─── Test helpers ─────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
function mockReq(url = '/page') {
|
|
8
|
+
return { url, headers: {}, method: 'GET' } as unknown as IncomingMessage
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type MockRes = ServerResponse & { header(k: string): string | undefined; body(): string }
|
|
12
|
+
|
|
13
|
+
function mockRes(): MockRes {
|
|
14
|
+
const headers: Record<string, string> = {}
|
|
15
|
+
let body = ''
|
|
16
|
+
let status = 200
|
|
17
|
+
return {
|
|
18
|
+
get statusCode() { return status },
|
|
19
|
+
set statusCode(v: number) { status = v },
|
|
20
|
+
setHeader: vi.fn((k: string, v: string) => { headers[k.toLowerCase()] = v }),
|
|
21
|
+
write: vi.fn(),
|
|
22
|
+
end: vi.fn((b?: string) => { if (b) body = b }),
|
|
23
|
+
header: (k: string) => headers[k.toLowerCase()],
|
|
24
|
+
body: () => body,
|
|
25
|
+
} as unknown as MockRes
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
afterEach(() => { vi.useRealTimers() })
|
|
29
|
+
|
|
30
|
+
// ─── Pass-through for non-ISR routes ─────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
describe('createIsrHandler — non-ISR routes', () => {
|
|
33
|
+
it('calls handler directly when route has no revalidate', async () => {
|
|
34
|
+
const routes = [{ path: '/about' }]
|
|
35
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('ok'))
|
|
36
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
37
|
+
await wrapped(mockReq('/about'), mockRes())
|
|
38
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('does not set X-Cache header for non-ISR routes', async () => {
|
|
42
|
+
const routes = [{ path: '/about' }]
|
|
43
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('ok'))
|
|
44
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
45
|
+
const res = mockRes()
|
|
46
|
+
await wrapped(mockReq('/about'), res)
|
|
47
|
+
expect(res.header('x-cache')).toBeUndefined()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('passes through when no routes match the URL', async () => {
|
|
51
|
+
const routes = [{ path: '/contact', meta: { ssg: { revalidate: 60 } } }]
|
|
52
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('ok'))
|
|
53
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
54
|
+
const res = mockRes()
|
|
55
|
+
await wrapped(mockReq('/about'), res) // /about has no route
|
|
56
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
57
|
+
expect(res.header('x-cache')).toBeUndefined()
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// ─── Cache miss (first request) ───────────────────────────────────────────────
|
|
62
|
+
|
|
63
|
+
describe('createIsrHandler — cache miss', () => {
|
|
64
|
+
it('serves X-Cache: HIT on the first request to an ISR route', async () => {
|
|
65
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
66
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('<html>v1</html>'))
|
|
67
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
68
|
+
const res = mockRes()
|
|
69
|
+
await wrapped(mockReq('/page'), res)
|
|
70
|
+
expect(res.header('x-cache')).toBe('HIT')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('renders via handler on cache miss', async () => {
|
|
74
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
75
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('<html>content</html>'))
|
|
76
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
77
|
+
await wrapped(mockReq('/page'), mockRes())
|
|
78
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('passes the correct URL to the handler during cache rendering', async () => {
|
|
82
|
+
const routes = [{ path: '/blog/:slug', meta: { ssg: { revalidate: 60 } } }]
|
|
83
|
+
let capturedUrl = ''
|
|
84
|
+
const handler = vi.fn((req: IncomingMessage, res: ServerResponse) => {
|
|
85
|
+
capturedUrl = req.url ?? ''
|
|
86
|
+
res.end('<html/>')
|
|
87
|
+
})
|
|
88
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
89
|
+
await wrapped(mockReq('/blog/hello'), mockRes())
|
|
90
|
+
expect(capturedUrl).toBe('/blog/hello')
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
// ─── Cache hit (within TTL) ───────────────────────────────────────────────────
|
|
95
|
+
|
|
96
|
+
describe('createIsrHandler — cache hit', () => {
|
|
97
|
+
it('serves X-Cache: HIT from cache on second request within TTL', async () => {
|
|
98
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
99
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('<html>v1</html>'))
|
|
100
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
101
|
+
await wrapped(mockReq('/page'), mockRes()) // prime
|
|
102
|
+
const res2 = mockRes()
|
|
103
|
+
await wrapped(mockReq('/page'), res2)
|
|
104
|
+
expect(res2.header('x-cache')).toBe('HIT')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('does not call handler again on cache hit', async () => {
|
|
108
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
109
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('<html>v1</html>'))
|
|
110
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
111
|
+
await wrapped(mockReq('/page'), mockRes()) // prime
|
|
112
|
+
await wrapped(mockReq('/page'), mockRes())
|
|
113
|
+
expect(handler).toHaveBeenCalledTimes(1) // no re-render
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('serves the cached HTML body', async () => {
|
|
117
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
118
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('<html>cached body</html>'))
|
|
119
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
120
|
+
await wrapped(mockReq('/page'), mockRes()) // prime
|
|
121
|
+
const res2 = mockRes()
|
|
122
|
+
await wrapped(mockReq('/page'), res2)
|
|
123
|
+
expect(res2.body()).toBe('<html>cached body</html>')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('forwards cached response headers', async () => {
|
|
127
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
128
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => {
|
|
129
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
|
130
|
+
res.end('<html/>')
|
|
131
|
+
})
|
|
132
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
133
|
+
await wrapped(mockReq('/page'), mockRes()) // prime
|
|
134
|
+
const res2 = mockRes()
|
|
135
|
+
await wrapped(mockReq('/page'), res2)
|
|
136
|
+
expect(res2.header('content-type')).toBe('text/html; charset=utf-8')
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('forwards cached HTTP status code', async () => {
|
|
140
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
141
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => {
|
|
142
|
+
res.statusCode = 200
|
|
143
|
+
res.end('<html/>')
|
|
144
|
+
})
|
|
145
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
146
|
+
await wrapped(mockReq('/page'), mockRes()) // prime
|
|
147
|
+
const res2 = mockRes()
|
|
148
|
+
await wrapped(mockReq('/page'), res2)
|
|
149
|
+
expect(res2.statusCode).toBe(200)
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
// ─── Stale (TTL expired) ──────────────────────────────────────────────────────
|
|
154
|
+
|
|
155
|
+
describe('createIsrHandler — stale-while-revalidate', () => {
|
|
156
|
+
it('serves X-Cache: STALE when TTL has expired (revalidate: 0)', async () => {
|
|
157
|
+
// revalidate: 0 means TTL is always expired after first render
|
|
158
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 0 } } }]
|
|
159
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('<html>v1</html>'))
|
|
160
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
161
|
+
await wrapped(mockReq('/page'), mockRes()) // prime (HIT)
|
|
162
|
+
const res2 = mockRes()
|
|
163
|
+
await wrapped(mockReq('/page'), res2)
|
|
164
|
+
expect(res2.header('x-cache')).toBe('STALE')
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('serves stale HTML body immediately while revalidating in background', async () => {
|
|
168
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 0 } } }]
|
|
169
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => res.end('<html>stale content</html>'))
|
|
170
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
171
|
+
await wrapped(mockReq('/page'), mockRes()) // prime
|
|
172
|
+
const res2 = mockRes()
|
|
173
|
+
await wrapped(mockReq('/page'), res2)
|
|
174
|
+
expect(res2.body()).toBe('<html>stale content</html>')
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it('triggers a background re-render when stale', async () => {
|
|
178
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 0 } } }]
|
|
179
|
+
let callCount = 0
|
|
180
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => {
|
|
181
|
+
callCount++
|
|
182
|
+
res.end(`<html>v${callCount}</html>`)
|
|
183
|
+
})
|
|
184
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
185
|
+
await wrapped(mockReq('/page'), mockRes()) // prime (callCount=1)
|
|
186
|
+
await wrapped(mockReq('/page'), mockRes()) // stale → triggers background render (callCount=2)
|
|
187
|
+
await new Promise((r) => setTimeout(r, 0)) // let background render settle
|
|
188
|
+
expect(callCount).toBe(2)
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('does not spawn a second background render while one is in flight', async () => {
|
|
192
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 0 } } }]
|
|
193
|
+
let callCount = 0
|
|
194
|
+
let resolveHung: (() => void) | undefined
|
|
195
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => {
|
|
196
|
+
callCount++
|
|
197
|
+
if (callCount === 1) {
|
|
198
|
+
res.end('<html>initial</html>')
|
|
199
|
+
} else {
|
|
200
|
+
// Simulate a slow background re-render that never ends in this test
|
|
201
|
+
new Promise<void>((r) => { resolveHung = r }).then(() => res.end('<html>refreshed</html>'))
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
205
|
+
await wrapped(mockReq('/page'), mockRes()) // prime
|
|
206
|
+
await wrapped(mockReq('/page'), mockRes()) // stale → background render in flight
|
|
207
|
+
// Third request while background render still in flight — should NOT spawn another
|
|
208
|
+
await wrapped(mockReq('/page'), mockRes())
|
|
209
|
+
expect(callCount).toBe(2) // still only 2 renders
|
|
210
|
+
resolveHung?.() // clean up
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it('resets revalidating flag when background render fails', async () => {
|
|
214
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 0 } } }]
|
|
215
|
+
let callCount = 0
|
|
216
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => {
|
|
217
|
+
callCount++
|
|
218
|
+
if (callCount === 1) {
|
|
219
|
+
res.end('<html>initial</html>')
|
|
220
|
+
} else {
|
|
221
|
+
throw new Error('render failed')
|
|
222
|
+
}
|
|
223
|
+
})
|
|
224
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
225
|
+
await wrapped(mockReq('/page'), mockRes()) // prime
|
|
226
|
+
await wrapped(mockReq('/page'), mockRes()) // stale → background render throws
|
|
227
|
+
await new Promise((r) => setTimeout(r, 0)) // let background render settle
|
|
228
|
+
|
|
229
|
+
// After failed background render, revalidating flag should be reset.
|
|
230
|
+
// The third request should trigger a new background render (callCount=3).
|
|
231
|
+
const res3 = mockRes()
|
|
232
|
+
await wrapped(mockReq('/page'), res3)
|
|
233
|
+
expect(res3.header('x-cache')).toBe('STALE')
|
|
234
|
+
expect(callCount).toBe(3) // a new render was triggered
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
it('resets revalidating flag after 30s timeout (hung render)', async () => {
|
|
238
|
+
vi.useFakeTimers()
|
|
239
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 0 } } }]
|
|
240
|
+
let callCount = 0
|
|
241
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => {
|
|
242
|
+
callCount++
|
|
243
|
+
if (callCount === 1) res.end('<html>initial</html>')
|
|
244
|
+
// callCount >= 2: hung — never calls res.end()
|
|
245
|
+
})
|
|
246
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
247
|
+
|
|
248
|
+
// Prime the cache (synchronous handler, resolves immediately)
|
|
249
|
+
await wrapped(mockReq('/page'), mockRes())
|
|
250
|
+
// Trigger stale + hung background render
|
|
251
|
+
await wrapped(mockReq('/page'), mockRes())
|
|
252
|
+
|
|
253
|
+
// Advance clock past 30s — revalidating flag should be reset by timeout
|
|
254
|
+
vi.advanceTimersByTime(31_000)
|
|
255
|
+
|
|
256
|
+
// After timeout reset, a new request should be able to start a fresh revalidation
|
|
257
|
+
const res3 = mockRes()
|
|
258
|
+
await wrapped(mockReq('/page'), res3)
|
|
259
|
+
expect(callCount).toBeGreaterThan(2) // a new render attempt was made
|
|
260
|
+
expect(res3.header('x-cache')).toBe('STALE')
|
|
261
|
+
})
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
// ─── Query string handling ─────────────────────────────────────────────────────
|
|
265
|
+
|
|
266
|
+
describe('createIsrHandler — query string handling', () => {
|
|
267
|
+
it('strips query string when looking up the cache key', async () => {
|
|
268
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
269
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => { res.end('<html>content</html>') })
|
|
270
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
271
|
+
// Prime with a query-string URL
|
|
272
|
+
await wrapped(mockReq('/page?foo=bar'), mockRes())
|
|
273
|
+
// Second request with a different query string — should still be a cache HIT
|
|
274
|
+
const res2 = mockRes()
|
|
275
|
+
await wrapped(mockReq('/page?baz=qux'), res2)
|
|
276
|
+
expect(res2.header('x-cache')).toBe('HIT')
|
|
277
|
+
expect(handler).toHaveBeenCalledTimes(1) // only one render; second served from cache
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
it('serves the cached HTML regardless of query string variation', async () => {
|
|
281
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
282
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => { res.end('<html>cached</html>') })
|
|
283
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
284
|
+
await wrapped(mockReq('/page?v=1'), mockRes())
|
|
285
|
+
const res2 = mockRes()
|
|
286
|
+
await wrapped(mockReq('/page?v=2'), res2)
|
|
287
|
+
expect(res2.body()).toBe('<html>cached</html>')
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('passes the stripped URL (no query string) to the handler during cache render', async () => {
|
|
291
|
+
// _renderForCache always uses the path-only URL for the fake request so the
|
|
292
|
+
// handler renders the canonical path, not a query-string-specific variant.
|
|
293
|
+
const routes = [{ path: '/page', meta: { ssg: { revalidate: 60 } } }]
|
|
294
|
+
let capturedUrl = ''
|
|
295
|
+
const handler = vi.fn((req: IncomingMessage, res: ServerResponse) => {
|
|
296
|
+
capturedUrl = req.url ?? ''
|
|
297
|
+
res.end('<html/>')
|
|
298
|
+
})
|
|
299
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
300
|
+
await wrapped(mockReq('/page?source=test'), mockRes())
|
|
301
|
+
expect(capturedUrl).toBe('/page')
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
// ─── render mode compatibility ────────────────────────────────────────────────
|
|
306
|
+
|
|
307
|
+
describe('createIsrHandler — render mode compatibility', () => {
|
|
308
|
+
it('caches a route with meta.render: server when revalidate is set', async () => {
|
|
309
|
+
// ISR applies to any route with meta.ssg.revalidate regardless of meta.render.
|
|
310
|
+
// render: 'server' controls SSG build-time behavior; ISR is a runtime cache layer.
|
|
311
|
+
const routes = [{ path: '/dashboard', meta: { render: 'server', ssg: { revalidate: 60 } } }]
|
|
312
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => { res.end('<html>dash</html>') })
|
|
313
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
314
|
+
await wrapped(mockReq('/dashboard'), mockRes()) // prime
|
|
315
|
+
const res2 = mockRes()
|
|
316
|
+
await wrapped(mockReq('/dashboard'), res2)
|
|
317
|
+
expect(res2.header('x-cache')).toBe('HIT')
|
|
318
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
it('does not cache a route with meta.render: server when revalidate is absent', async () => {
|
|
322
|
+
const routes = [{ path: '/dashboard', meta: { render: 'server' } }]
|
|
323
|
+
const handler = vi.fn((_: IncomingMessage, res: ServerResponse) => { res.end('<html>dash</html>') })
|
|
324
|
+
const wrapped = createIsrHandler(routes, handler)
|
|
325
|
+
await wrapped(mockReq('/dashboard'), mockRes())
|
|
326
|
+
const res2 = mockRes()
|
|
327
|
+
await wrapped(mockReq('/dashboard'), res2)
|
|
328
|
+
expect(res2.header('x-cache')).toBeUndefined()
|
|
329
|
+
expect(handler).toHaveBeenCalledTimes(2) // no cache — handler called each time
|
|
330
|
+
})
|
|
331
|
+
})
|
|
@@ -49,6 +49,25 @@ export function matchRoutePattern(pattern: string, urlPath: string): boolean {
|
|
|
49
49
|
return new RegExp(regexStr).test(norm(urlPath))
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Returns the per-route `render` strategy ('static' | 'server' | 'spa') for
|
|
54
|
+
* the route that best matches `urlPath`, or `null` when no route matches or
|
|
55
|
+
* none declares a render mode.
|
|
56
|
+
*/
|
|
57
|
+
export function findRenderMode(
|
|
58
|
+
routes: Array<{ path: string; meta?: Record<string, unknown> }>,
|
|
59
|
+
urlPath: string,
|
|
60
|
+
): 'static' | 'server' | 'spa' | null {
|
|
61
|
+
for (const route of routes) {
|
|
62
|
+
if (matchRoutePattern(route.path, urlPath)) {
|
|
63
|
+
const render = route.meta?.render
|
|
64
|
+
if (render === 'static' || render === 'server' || render === 'spa') return render
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return null
|
|
69
|
+
}
|
|
70
|
+
|
|
52
71
|
/**
|
|
53
72
|
* Looks up the `meta.ssg.revalidate` TTL (in seconds) for the route that best
|
|
54
73
|
* matches `urlPath`. Returns `null` when no route matches or none defines
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
type IsrCacheEntry,
|
|
8
8
|
type SsrHandlerFn,
|
|
9
9
|
findRevalidate,
|
|
10
|
+
findRenderMode,
|
|
10
11
|
renderForIsr,
|
|
11
12
|
serveFromIsrCache,
|
|
12
13
|
} from './preview-isr.js'
|
|
@@ -200,6 +201,51 @@ export function previewCommand(): Command {
|
|
|
200
201
|
if (served) return
|
|
201
202
|
}
|
|
202
203
|
|
|
204
|
+
// Per-route render strategy — checked before ISR.
|
|
205
|
+
const renderMode = findRenderMode(pageRoutes, urlPath)
|
|
206
|
+
|
|
207
|
+
// render: 'spa' — skip SSR, serve the client index.html shell.
|
|
208
|
+
if (renderMode === 'spa') {
|
|
209
|
+
const spaIndex = join(distDir, 'client/index.html')
|
|
210
|
+
if (existsSync(spaIndex)) {
|
|
211
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
|
212
|
+
res.setHeader('Cache-Control', 'no-cache')
|
|
213
|
+
createReadStream(spaIndex).pipe(res)
|
|
214
|
+
} else {
|
|
215
|
+
res.statusCode = 404
|
|
216
|
+
res.setHeader('Content-Type', 'text/plain')
|
|
217
|
+
res.end('Not Found')
|
|
218
|
+
}
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// render: 'static' — try pre-rendered HTML from dist/, fall through to SSR.
|
|
223
|
+
if (renderMode === 'static') {
|
|
224
|
+
const staticFile = urlPath === '/'
|
|
225
|
+
? join(distDir, 'index.html')
|
|
226
|
+
: join(distDir, urlPath.replace(/^\//, ''), 'index.html')
|
|
227
|
+
if (existsSync(staticFile)) {
|
|
228
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
|
229
|
+
res.setHeader('Cache-Control', 'no-cache')
|
|
230
|
+
createReadStream(staticFile).pipe(res)
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
// Fall through to SSR if no pre-rendered file exists.
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// render: 'server' — always SSR, bypass ISR cache.
|
|
237
|
+
if (renderMode === 'server') {
|
|
238
|
+
try {
|
|
239
|
+
await handler(req, res)
|
|
240
|
+
} catch (err) {
|
|
241
|
+
console.error('[cer-app] SSR handler error:', err)
|
|
242
|
+
res.statusCode = 500
|
|
243
|
+
res.setHeader('Content-Type', 'text/plain')
|
|
244
|
+
res.end('Internal Server Error')
|
|
245
|
+
}
|
|
246
|
+
return
|
|
247
|
+
}
|
|
248
|
+
|
|
203
249
|
// ISR: check whether this route has a revalidate TTL.
|
|
204
250
|
const revalidate = findRevalidate(pageRoutes, urlPath)
|
|
205
251
|
if (revalidate !== null) {
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
"preview": "cer-app preview"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@jasonshimmy/custom-elements-runtime": "^3.
|
|
11
|
+
"@jasonshimmy/custom-elements-runtime": "^3.4.0"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"vite": "^8.0.1",
|
|
15
|
-
"@jasonshimmy/vite-plugin-cer-app": "^0.
|
|
15
|
+
"@jasonshimmy/vite-plugin-cer-app": "^0.6.0",
|
|
16
16
|
"typescript": "^5.9.3"
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
"preview": "cer-app preview"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@jasonshimmy/custom-elements-runtime": "^3.
|
|
12
|
+
"@jasonshimmy/custom-elements-runtime": "^3.4.0"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"vite": "^8.0.1",
|
|
16
|
-
"@jasonshimmy/vite-plugin-cer-app": "^0.
|
|
16
|
+
"@jasonshimmy/vite-plugin-cer-app": "^0.6.0",
|
|
17
17
|
"typescript": "^5.9.3"
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
"preview": "cer-app preview --ssr"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@jasonshimmy/custom-elements-runtime": "^3.
|
|
11
|
+
"@jasonshimmy/custom-elements-runtime": "^3.4.0"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"vite": "^8.0.1",
|
|
15
|
-
"@jasonshimmy/vite-plugin-cer-app": "^0.
|
|
15
|
+
"@jasonshimmy/vite-plugin-cer-app": "^0.6.0",
|
|
16
16
|
"typescript": "^5.9.3"
|
|
17
17
|
}
|
|
18
18
|
}
|
package/src/plugin/build-ssg.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { writeFile, mkdir } from 'node:fs/promises'
|
|
1
|
+
import { writeFile, mkdir, readFile } from 'node:fs/promises'
|
|
2
2
|
import { existsSync } from 'node:fs'
|
|
3
3
|
import { join } from 'pathe'
|
|
4
4
|
import { createServer, type UserConfig } from 'vite'
|
|
@@ -45,6 +45,15 @@ async function collectSsgPaths(
|
|
|
45
45
|
const dynamicFiles: Array<{ file: string; entry: ReturnType<typeof buildRouteEntry> }> = []
|
|
46
46
|
|
|
47
47
|
for (const file of files) {
|
|
48
|
+
// Skip routes that declare render: 'server' or render: 'spa' — they are
|
|
49
|
+
// either always-SSR or client-only and must not be pre-rendered.
|
|
50
|
+
try {
|
|
51
|
+
const src = await readFile(file, 'utf-8')
|
|
52
|
+
const renderMatch = src.match(/render\s*:\s*['"]([^'"]+)['"]/)
|
|
53
|
+
const renderMode = renderMatch ? renderMatch[1] : null
|
|
54
|
+
if (renderMode === 'server' || renderMode === 'spa') continue
|
|
55
|
+
} catch { /* ignore read errors */ }
|
|
56
|
+
|
|
48
57
|
const entry = buildRouteEntry(file, config.pagesDir)
|
|
49
58
|
|
|
50
59
|
if (!entry.isDynamic && !entry.isCatchAll) {
|
|
@@ -73,6 +82,7 @@ async function collectSsgPaths(
|
|
|
73
82
|
const pageMod = await viteServer.ssrLoadModule(file)
|
|
74
83
|
const pageMeta = pageMod.meta ?? pageMod.pageMeta
|
|
75
84
|
|
|
85
|
+
if (pageMeta?.render === 'server' || pageMeta?.render === 'spa') continue
|
|
76
86
|
if (pageMeta?.ssg?.paths) {
|
|
77
87
|
const pathsResult = await pageMeta.ssg.paths()
|
|
78
88
|
for (const ctx of pathsResult) {
|
|
@@ -131,12 +141,14 @@ async function renderPath(
|
|
|
131
141
|
|
|
132
142
|
// Mock req/res for the Express-style handler.
|
|
133
143
|
// The handler internally merges with dist/client/index.html, so we just
|
|
134
|
-
// capture whatever it ends with.
|
|
144
|
+
// capture whatever it writes/ends with.
|
|
135
145
|
const mockReq = { url: path, headers: {} }
|
|
136
146
|
return new Promise<string>((resolve, reject) => {
|
|
147
|
+
const chunks: string[] = []
|
|
137
148
|
const mockRes = {
|
|
138
149
|
setHeader: () => {},
|
|
139
|
-
|
|
150
|
+
write: (chunk: string) => { chunks.push(chunk) },
|
|
151
|
+
end: (body?: string) => resolve(chunks.join('') + (body ?? '')),
|
|
140
152
|
}
|
|
141
153
|
;(handlerFn as (req: unknown, res: unknown) => Promise<void>)(mockReq, mockRes).catch(reject)
|
|
142
154
|
})
|
package/src/plugin/dev-server.ts
CHANGED
|
@@ -108,6 +108,24 @@ function parseQuery(url: string): Record<string, string> {
|
|
|
108
108
|
return result
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Tests whether a route path pattern matches a URL path.
|
|
113
|
+
* Mirrors the logic in preview-isr.ts#matchRoutePattern — kept local to
|
|
114
|
+
* avoid a plugin→CLI dependency.
|
|
115
|
+
*/
|
|
116
|
+
function _matchDevRoute(pattern: string, urlPath: string): boolean {
|
|
117
|
+
const norm = (s: string) => s.replace(/\/+$/, '') || '/'
|
|
118
|
+
if (norm(pattern) === norm(urlPath)) return true
|
|
119
|
+
const regexStr =
|
|
120
|
+
'^' +
|
|
121
|
+
norm(pattern)
|
|
122
|
+
.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
|
|
123
|
+
.replace(/:[^/]+\*/g, '.*')
|
|
124
|
+
.replace(/:[^/]+/g, '[^/]+') +
|
|
125
|
+
'$'
|
|
126
|
+
return new RegExp(regexStr).test(norm(urlPath))
|
|
127
|
+
}
|
|
128
|
+
|
|
111
129
|
/**
|
|
112
130
|
* Configures the Vite dev server with:
|
|
113
131
|
* 1. API route handlers from server/api/
|
|
@@ -218,6 +236,21 @@ export function configureCerDevServer(
|
|
|
218
236
|
(!url.includes('.') && !url.startsWith('/api/'))
|
|
219
237
|
|
|
220
238
|
if (acceptsHtml) {
|
|
239
|
+
// Check per-route render mode — skip SSR for 'spa' routes.
|
|
240
|
+
const urlPathOnly = url.split('?')[0]
|
|
241
|
+
try {
|
|
242
|
+
const routesMod = await server.ssrLoadModule('virtual:cer-routes')
|
|
243
|
+
const pageRoutes = Array.isArray(routesMod.default) ? routesMod.default as Array<{ path: string; meta?: Record<string, unknown> }> : []
|
|
244
|
+
for (const route of pageRoutes) {
|
|
245
|
+
if (_matchDevRoute(route.path, urlPathOnly)) {
|
|
246
|
+
if (route.meta?.render === 'spa') {
|
|
247
|
+
next()
|
|
248
|
+
return
|
|
249
|
+
}
|
|
250
|
+
break
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
} catch { /* module not ready — continue to SSR */ }
|
|
221
254
|
try {
|
|
222
255
|
// Load the SSR entry module
|
|
223
256
|
const ssrEntry = await server.ssrLoadModule(
|