Skip to content
All articles

Four Ways My Local API Shim Disagreed With Production

6 min read

This site runs its API endpoints as Vercel functions in production. Locally there is no Vercel: a small Vite plugin intercepts /api/* requests and calls the same handler files, imitating what the platform would hand them.

That plugin is about a hundred lines, most of them comments. It has produced four separate bugs where something worked in one environment and not the other — and in every case the dangerous direction was the same: it passed locally and failed, or quietly behaved differently, in production.

1. It tested the wrong contract entirely

The first contact-form handler was written in the modern style: take a Request, return a Response. The local shim was written to match, so it called the handler, took the returned Response and sent it. Locally, the form worked perfectly.

In production, every submission hung. In the way this project is deployed, a default-exported function is invoked Node-style, as (req, res), and is expected to write to res. A returned Response is ignored. The handler did its work, returned a value nobody read, and the request waited until it timed out.

The shim had faithfully tested the handler against a contract the platform does not use. Fixing it meant rewriting the handler to the (req, res) shape — and, more importantly, rewriting the shim to call it that way too, so local runs exercise what production will actually do.

2. req.query was always empty

The platform parses the query string for you and hands it over as req.query. The shim, written quickly, passed query: {}.

Any handler branch that read a query parameter therefore took its default path in development and the real path in production. Nothing failed; the code you were testing locally was simply not the code that ran live. The fix is to parse the actual query string in the shim.

3. It only knew about one endpoint

The shim matched the exact path /api/contact. When a second endpoint was added — /api/articles — it worked in production immediately, because the platform routes every file in the api directory, and 404'd locally. That one at least fails loudly. The fix is to route /api/<name> to api/<name>.ts when the file exists, the same rule the platform uses.

4. Repeated query parameters went the opposite way

This is the subtle one, and it was hiding inside the fix for the second bug. The shim parsed the query like this:

const query = Object.fromEntries(new URLSearchParams(search))

For ?tag=a&tag=b, Object.fromEntries keeps the last value: { tag: 'b' }. The platform gives an array: { tag: ['a', 'b'] }. The handler took the first element of an array, or the value if it was a string. So in production ?tag=a&tag=b meant a, and in development it meant b.

I found it only because a test request with a repeated parameter came back with the wrong article. The fix mirrors the platform exactly:

const params = new URLSearchParams(search)
const query = {}
for (const key of new Set(params.keys())) {
  const values = params.getAll(key)
  query[key] = values.length > 1 ? values : values[0] ?? ''
}

What the four have in common

Each one was a place where the shim was written from memory of how the platform behaves, rather than checked against it. And each one failed in the direction that hides the problem: development looked fine.

A local stand-in for a platform is only worth having if it behaves like the platform at the edges, because that is where bugs live. Three habits would have caught all four:

  • Write the handler signature from the platform's documentation, then make the shim call it that way — never the other way round.
  • Pass real inputs, not placeholders. An empty object where parsed data should be is a branch of your code you have silently stopped testing.
  • Deploy one request with odd input early. A repeated parameter, a missing one, a second endpoint. Compare the response with local. It takes five minutes and it is the only test that actually compares the two environments.

The shim is still about a hundred lines. They now agree with production, which is the only thing it was ever for.