@jasonshimmy/vite-plugin-cer-app 0.6.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.
Files changed (54) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/IMPLEMENTATION_PLAN.md +2 -2
  3. package/commits.txt +1 -1
  4. package/dist/cli/commands/preview-isr.d.ts +9 -0
  5. package/dist/cli/commands/preview-isr.d.ts.map +1 -1
  6. package/dist/cli/commands/preview-isr.js +16 -0
  7. package/dist/cli/commands/preview-isr.js.map +1 -1
  8. package/dist/cli/commands/preview.d.ts.map +1 -1
  9. package/dist/cli/commands/preview.js +44 -1
  10. package/dist/cli/commands/preview.js.map +1 -1
  11. package/dist/plugin/build-ssg.d.ts.map +1 -1
  12. package/dist/plugin/build-ssg.js +13 -1
  13. package/dist/plugin/build-ssg.js.map +1 -1
  14. package/dist/plugin/dev-server.d.ts.map +1 -1
  15. package/dist/plugin/dev-server.js +33 -0
  16. package/dist/plugin/dev-server.js.map +1 -1
  17. package/dist/plugin/virtual/routes.d.ts.map +1 -1
  18. package/dist/plugin/virtual/routes.js +24 -2
  19. package/dist/plugin/virtual/routes.js.map +1 -1
  20. package/dist/runtime/entry-server-template.d.ts +1 -1
  21. package/dist/runtime/entry-server-template.d.ts.map +1 -1
  22. package/dist/runtime/entry-server-template.js +21 -4
  23. package/dist/runtime/entry-server-template.js.map +1 -1
  24. package/dist/runtime/isr-handler.d.ts +40 -0
  25. package/dist/runtime/isr-handler.d.ts.map +1 -0
  26. package/dist/runtime/isr-handler.js +152 -0
  27. package/dist/runtime/isr-handler.js.map +1 -0
  28. package/dist/types/page.d.ts +14 -0
  29. package/dist/types/page.d.ts.map +1 -1
  30. package/docs/data-loading.md +69 -2
  31. package/docs/rendering-modes.md +63 -2
  32. package/docs/routing.md +33 -0
  33. package/e2e/cypress/e2e/error-boundary.cy.ts +43 -0
  34. package/e2e/cypress/e2e/isr-nested-runtime.cy.ts +6 -4
  35. package/e2e/cypress/e2e/per-route-render.cy.ts +70 -0
  36. package/e2e/kitchen-sink/app/error.ts +7 -2
  37. package/e2e/kitchen-sink/app/pages/loader-error-test.ts +13 -0
  38. package/e2e/kitchen-sink/app/pages/render-server-test.ts +12 -0
  39. package/e2e/kitchen-sink/app/pages/render-spa-test.ts +12 -0
  40. package/package.json +5 -1
  41. package/src/__tests__/cli/preview-isr.test.ts +44 -0
  42. package/src/__tests__/plugin/build-ssg.test.ts +126 -1
  43. package/src/__tests__/plugin/dev-server.test.ts +91 -0
  44. package/src/__tests__/plugin/entry-server-template.test.ts +53 -0
  45. package/src/__tests__/plugin/virtual/routes.test.ts +65 -0
  46. package/src/__tests__/runtime/isr-handler.test.ts +331 -0
  47. package/src/cli/commands/preview-isr.ts +19 -0
  48. package/src/cli/commands/preview.ts +46 -0
  49. package/src/plugin/build-ssg.ts +11 -1
  50. package/src/plugin/dev-server.ts +33 -0
  51. package/src/plugin/virtual/routes.ts +24 -2
  52. package/src/runtime/entry-server-template.ts +21 -4
  53. package/src/runtime/isr-handler.ts +183 -0
  54. package/src/types/page.ts +14 -0
@@ -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) {
@@ -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) {
@@ -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(
@@ -64,6 +64,23 @@ function extractTransition(source: string): string | boolean | null {
64
64
  return null
65
65
  }
66
66
 
67
+ /**
68
+ * Extracts the per-route `render` strategy from a page file's source.
69
+ * Returns 'static', 'server', 'spa', or null if absent.
70
+ *
71
+ * Matches patterns like:
72
+ * render: 'server'
73
+ * render: 'spa'
74
+ * render: 'static'
75
+ */
76
+ function extractRender(source: string): 'static' | 'server' | 'spa' | null {
77
+ const match = source.match(/render\s*:\s*['"]([^'"]+)['"]/)
78
+ if (!match) return null
79
+ const val = match[1]
80
+ if (val === 'static' || val === 'server' || val === 'spa') return val
81
+ return null
82
+ }
83
+
67
84
  /**
68
85
  * Resolves the layout chain for a page by walking its ancestor directories
69
86
  * inside pagesDir looking for `_layout.ts` files. Each `_layout.ts` must
@@ -162,6 +179,7 @@ export async function generateRoutesCode(pagesDir: string): Promise<string> {
162
179
  layoutChain: string[] | null
163
180
  revalidate: number | null
164
181
  transition: string | boolean | null
182
+ render: 'static' | 'server' | 'spa' | null
165
183
  }> = await Promise.all(
166
184
  sorted.map(async (entry) => {
167
185
  try {
@@ -174,9 +192,10 @@ export async function generateRoutesCode(pagesDir: string): Promise<string> {
174
192
  layoutChain,
175
193
  revalidate: extractRevalidate(src),
176
194
  transition: extractTransition(src),
195
+ render: extractRender(src),
177
196
  }
178
197
  } catch {
179
- return { middleware: [], layout: null, layoutChain: null, revalidate: null, transition: null }
198
+ return { middleware: [], layout: null, layoutChain: null, revalidate: null, transition: null, render: null }
180
199
  }
181
200
  }),
182
201
  )
@@ -185,7 +204,7 @@ export async function generateRoutesCode(pagesDir: string): Promise<string> {
185
204
 
186
205
  // Build routes array with lazy load() functions for code splitting.
187
206
  const routeItems = sorted.map((entry, i) => {
188
- const { middleware: mw, layout, layoutChain, revalidate, transition } = metaPerEntry[i]
207
+ const { middleware: mw, layout, layoutChain, revalidate, transition, render } = metaPerEntry[i]
189
208
  const filePath = JSON.stringify(entry.filePath)
190
209
  const tagName = JSON.stringify(entry.tagName)
191
210
  const routePath = JSON.stringify(entry.routePath)
@@ -209,6 +228,9 @@ export async function generateRoutesCode(pagesDir: string): Promise<string> {
209
228
  if (transition !== null) {
210
229
  metaFields.push(`transition: ${JSON.stringify(transition)}`)
211
230
  }
231
+ if (render !== null) {
232
+ metaFields.push(`render: ${JSON.stringify(render)}`)
233
+ }
212
234
  const metaStr = metaFields.length > 0 ? ` meta: { ${metaFields.join(', ')} },\n` : ''
213
235
 
214
236
  if (mw.length === 0) {
@@ -27,6 +27,8 @@ import { registerEntityMap, renderToStreamWithJITCSSDSD, DSD_POLYFILL_SCRIPT } f
27
27
  import entitiesJson from '@jasonshimmy/custom-elements-runtime/entities.json'
28
28
  import { initRouter } from '@jasonshimmy/custom-elements-runtime/router'
29
29
  import { beginHeadCollection, endHeadCollection, serializeHeadTags, initRuntimeConfig } from '@jasonshimmy/vite-plugin-cer-app/composables'
30
+ import { errorTag } from 'virtual:cer-error'
31
+ import { createIsrHandler } from '@jasonshimmy/vite-plugin-cer-app/isr'
30
32
 
31
33
  registerBuiltinComponents()
32
34
  initRuntimeConfig(runtimeConfig)
@@ -155,8 +157,18 @@ const _prepareRequest = async (req) => {
155
157
  head = \`<script>window.__CER_DATA__ = \${JSON.stringify(data)}</script>\`
156
158
  }
157
159
  }
158
- } catch {
159
- // Non-fatal: loader errors fall back to an empty page; client will refetch.
160
+ } catch (err) {
161
+ // Loader threw render the error page server-side if app/error.ts exists.
162
+ const status = (err && typeof err === 'object' && 'status' in err && typeof err.status === 'number')
163
+ ? err.status : 500
164
+ const message = (err instanceof Error) ? err.message : String(err)
165
+ if (!errorTag) {
166
+ console.error('[cer-app] Loader error (no app/error.ts defined):', err)
167
+ }
168
+ const errVnode = errorTag
169
+ ? { tag: errorTag, props: { attrs: { error: message, status: String(status) } }, children: [] }
170
+ : { tag: 'div', props: {}, children: [] }
171
+ return { vnode: errVnode, router, head: undefined, status }
160
172
  }
161
173
  }
162
174
 
@@ -172,12 +184,13 @@ const _prepareRequest = async (req) => {
172
184
  if (tag) vnode = { tag, props: {}, children: [vnode] }
173
185
  }
174
186
 
175
- return { vnode, router, head }
187
+ return { vnode, router, head, status: null }
176
188
  }
177
189
 
178
190
  export const handler = async (req, res) => {
179
191
  await _cerDataStore.run(null, async () => {
180
- const { vnode, router, head } = await _prepareRequest(req)
192
+ const { vnode, router, head, status } = await _prepareRequest(req)
193
+ if (status != null) res.statusCode = status
181
194
 
182
195
  // Begin collecting useHead() calls made during the synchronous render pass.
183
196
  // IMPORTANT: the stream's start() function runs synchronously on construction,
@@ -236,6 +249,10 @@ export const handler = async (req, res) => {
236
249
  })
237
250
  }
238
251
 
252
+ // ISR-wrapped handler for production integrations (Express, Hono, Fastify).
253
+ // Routes with meta.ssg.revalidate are served stale-while-revalidate.
254
+ export const isrHandler = createIsrHandler(routes, handler)
255
+
239
256
  export { apiRoutes, plugins, layouts, routes }
240
257
  export default handler
241
258
  `