cold-start: decouple webhook registration from server hook#41
Conversation
Webhook handlers now register lazily when the webhook route is hit, keeping Octokit and regression imports out of every cold boot.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR decouples GitHub webhook handler registration from the SvelteKit server hook, deferring it until the first request hits
Confidence Score: 4/5Safe to merge — the lazy registration pattern is correct and the singleton App instance ensures handlers stay bound across warm requests. The core logic is sound: getGithubApp() returns the same App instance per container lifetime, so the boolean flag accurately reflects whether handlers have been wired up. The one thing worth noting is that ensureGithubWebhookHandlersRegistered runs before signature verification, meaning any unauthenticated hit to the webhook endpoint on a fresh container will trigger handler registration — harmless today but an ordering inversion that could be surprising later. The ordering of ensureGithubWebhookHandlersRegistered relative to signature verification in src/lib/server/github/webhooks.ts is worth a second look. Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant Hooks as hooks.server.ts
participant Route as webhook route
participant Webhooks as webhooks.ts
participant AppModule as app.ts
Note over Hooks: Cold start (before PR)
Hooks->>Webhooks: registerGithubWebhookHandlers()
Webhooks->>AppModule: getGithubApp()
AppModule-->>Webhooks: App singleton
Webhooks-->>Hooks: handlers registered eagerly
Note over Hooks: Cold start (after PR)
Hooks-->>Client: no webhook import at boot
Client->>Route: POST /api/github/webhook
Route->>Webhooks: handleGithubWebhook(request)
Webhooks->>AppModule: getGithubApp()
AppModule-->>Webhooks: App singleton
Webhooks->>Webhooks: ensureGithubWebhookHandlersRegistered flag false registers handlers
Webhooks->>Webhooks: verify signature
Webhooks->>Webhooks: webhooks.receive(event)
Webhooks-->>Client: 200 ok
Client->>Route: POST /api/github/webhook (warm)
Route->>Webhooks: handleGithubWebhook(request)
Webhooks->>Webhooks: ensureGithubWebhookHandlersRegistered flag true skips
Webhooks->>Webhooks: verify and receive
Webhooks-->>Client: 200 ok
Reviews (1): Last reviewed commit: "Stop registering GitHub webhook handlers..." | Re-trigger Greptile |
| return new Response('GitHub App is not configured', { status: 503 }); | ||
| } | ||
|
|
||
| ensureGithubWebhookHandlersRegistered(githubApp); |
There was a problem hiding this comment.
Handler registration before signature verification
ensureGithubWebhookHandlersRegistered is called before the signature check, so any unauthenticated request to this endpoint on a fresh container will trigger handler registration. Functionally this is harmless — no events are dispatched until after webhooks.receive() — but the ordering is semantically inverted. Moving the call to just after signature verification aligns intent with execution.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Stack Context
This stack reduces Vercel cold-start time by shrinking the shared SvelteKit server boot path and isolating heavy agent/GitHub dependencies.
Why?
Every serverless invocation was loading GitHub webhook handler registration from
hooks.server.ts, which pulled Octokit and regression scheduling into the global request path. Webhook handlers only need to exist for the webhook route, so registration is deferred until that route handles a request.Test plan
/sign-inafter a cold period and confirm faster first response/api/github/webhookand confirm handlers still fire for PR events