Skip to content

Commit 2297a3d

Browse files
committed
refactor: move /incident-status to app router
Move /incident-status handler to App Router. Allows us to use Vercel Data Cache to cache the upstream fetch.
1 parent d067569 commit 2297a3d

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

apps/studio/pages/api/incident-status.ts renamed to apps/studio/app/api/incident-status/route.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IS_PLATFORM } from 'common'
2-
import { NextApiRequest, NextApiResponse } from 'next'
2+
import { NextResponse } from 'next/server'
33

44
import { InternalServerError } from '@/lib/api/apiHelpers'
55
import { getActiveIncidents, type IncidentCache } from '@/lib/api/incident-status'
@@ -42,22 +42,26 @@ async function fetchIncidentCache(incidentIds: Array<string>): Promise<Map<strin
4242
return cacheMap
4343
}
4444

45-
async function handler(req: NextApiRequest, res: NextApiResponse) {
46-
if (!IS_PLATFORM) {
47-
return res.status(404).end()
48-
}
45+
export async function OPTIONS() {
46+
if (!IS_PLATFORM) return new Response(null, { status: 404 })
47+
return new Response(null, {
48+
status: 204,
49+
headers: {
50+
Allow: 'GET, HEAD, OPTIONS',
51+
},
52+
})
53+
}
4954

50-
const { method } = req
55+
export async function HEAD() {
56+
if (!IS_PLATFORM) return new Response(null, { status: 404 })
57+
return new Response(null, {
58+
status: 200,
59+
headers: { 'Cache-Control': CACHE_CONTROL_SETTINGS },
60+
})
61+
}
5162

52-
if (method === 'HEAD') {
53-
res.setHeader('Cache-Control', CACHE_CONTROL_SETTINGS)
54-
return res.status(200).end()
55-
}
56-
57-
if (method !== 'GET') {
58-
res.setHeader('Allow', ['GET', 'HEAD'])
59-
return res.status(405).json({ error: `Method ${method} Not Allowed` })
60-
}
63+
export async function GET() {
64+
if (!IS_PLATFORM) return new Response(null, { status: 404 })
6165

6266
try {
6367
const allIncidents = await getActiveIncidents()
@@ -75,17 +79,18 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
7579
cache: cacheMap.get(incident.id) ?? null,
7680
}))
7781

78-
res.setHeader('Cache-Control', CACHE_CONTROL_SETTINGS)
79-
80-
return res.status(200).json(enrichedIncidents)
82+
return NextResponse.json(enrichedIncidents, {
83+
headers: { 'Cache-Control': CACHE_CONTROL_SETTINGS },
84+
})
8185
} catch (error) {
8286
let errorCode = 500
87+
const headers = new Headers()
8388

8489
if (error instanceof InternalServerError) {
8590
if (typeof error.details?.status === 'number') errorCode = error.details.status
8691
if (errorCode === 420) errorCode = 429
8792
if (errorCode === 429 && typeof error.details?.retryAfter === 'string') {
88-
res.setHeader('Retry-After', error.details.retryAfter)
93+
headers.set('Retry-After', error.details.retryAfter)
8994
}
9095
console.error('Failed to fetch active StatusPage incidents: %O', {
9196
message: error.message,
@@ -95,8 +100,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
95100
console.error('Unexpected error fetching active StatusPage incidents: %O', error)
96101
}
97102

98-
return res.status(errorCode).json({ error: 'Unable to fetch incidents at this time' })
103+
return NextResponse.json(
104+
{ error: 'Unable to fetch incidents at this time' },
105+
{ status: errorCode, headers }
106+
)
99107
}
100108
}
101-
102-
export default handler

apps/studio/lib/api/incident-status.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import z from 'zod'
2-
31
import { IS_PLATFORM } from 'common'
42
import { InternalServerError } from 'lib/api/apiHelpers'
3+
import z from 'zod'
54

65
export type IncidentCache = {
76
affected_regions: Array<string> | null
@@ -81,7 +80,7 @@ export async function getActiveIncidents(): Promise<IncidentInfo[]> {
8180
Accept: 'application/json',
8281
'Content-Type': 'application/json',
8382
},
84-
cache: 'no-store',
83+
next: { revalidate: 180 },
8584
signal: AbortSignal.timeout(30_000),
8685
})
8786
const responseText = await response.text()

0 commit comments

Comments
 (0)