Skip to content
Book a call

The captcha that silently never rendered

· 4-minute read · Engineering · by Direct Softworks

A public environment variable is compiled into the bundle, not read at boot. Set it on the server and the build has already decided, which is how a form gate disappears without a single error.

The contact form stopped producing enquiries. Not zero traffic, not an error rate. Enquiries just stopped arriving, and the form looked entirely normal.

What had happened: the captcha widget was not rendering, submissions carried no token, and the server rejected every one of them for a perfectly good reason. Nothing in the browser complained, because a widget that never mounts does not throw. The form submitted. The server said no. The user saw a generic failure and left.

The mechanism

In Next.js, and in most bundlers with the same convention, a variable prefixed NEXT_PUBLIC_ is inlined into the JavaScript at build time. It is not read from the environment when the process starts. The build replaces every occurrence of process.env.NEXT_PUBLIC_X with a literal string.

Which means: if the variable is absent during next build, the literal that gets compiled in is undefined, or an empty string, forever. Setting it correctly on the production server changes nothing at all. The decision was made in CI, minutes earlier, by a build that could not see it.

That is the whole bug, and it is invisible from the server side. You can SSH in, print the environment, see the correct value, and be looking at a machine that is serving a bundle which does not contain it.

Why it fails silently rather than loudly

Third-party widget SDKs are defensive. Given an empty site key, most render nothing rather than throwing, on the reasonable theory that a missing widget is better than a broken page.

So the failure is: no console error, no network error, no server log, no monitoring signal. The page is 200. The form is present. The button works. Only the conversion rate knows, and it knows quietly, over a week.

How to tell whether a client value actually shipped

Do not check the server environment. Check the bundle:

curl -s https://your-site/ | grep -o 'main-[a-z0-9]*\.js'
curl -s https://your-site/_next/static/chunks/<that file> | grep -c 'YOUR_EXPECTED_VALUE'

If the count is zero, the value is not in the build, whatever the server says. That two-line check is the only trustworthy answer, and it takes twenty seconds.

The same test works as a deploy gate: after each release, assert that the key appears in the shipped JavaScript. That turns a silent, week-long revenue leak into a failed deployment.

Three ways to stop it happening

Fail the build. If a required public variable is missing, throw during the build rather than compiling in an empty string. Ten lines in the config, and it converts a silent production failure into a loud CI failure, which is the trade you want every time.

Put the value in the repository. This sounds wrong and for public values it is correct. A captcha site key is rendered into the HTML of every page for every visitor. It is not a credential, it is a constant. Committing it means any build anywhere is correct with nothing to configure, and the environment variable can still override it for a staging build with a different key. The matching secret key stays server-side, where it is read at runtime and where none of this applies.

That is what we ended up doing, and the reasoning generalises: if a value is going to be visible to every visitor anyway, treating it as a secret buys nothing and costs a class of deployment bug.

Make it render an error instead of nothing. If the key is missing at runtime, show a visible broken state rather than an empty div. A widget that fails loudly gets fixed the day it breaks.

The wider category

This is one instance of a family: configuration that is resolved earlier than people assume.

Build-time environment variables. Values baked into a container image. Anything computed at module load rather than per request. In each case there is a moment when the decision is made, and if your mental model puts that moment later than it really is, you will change the input and observe no effect, repeatedly, and start doubting the deployment rather than the model.

The diagnostic question is always the same, and it is worth asking early rather than after an afternoon: when was this value decided, and by which process? If the answer is "during a build in CI", nothing you do on the server will matter, and the fastest route to the truth is to look at the artefact rather than the machine.

It is a cousin of the 503 whose symptoms all pointed the wrong way: the evidence in front of you is real, and it is answering a different question than the one you are asking.

More notes