Dev.to WebDev 🛠 Dev 👁 2 📖 5 min read

Our QR tool tops out at 2,331 characters, and past that it shows the error beside the previous code with the download buttons still lit

Our site has a free QR code generator at pub-trivia.app/tools/qr-code-generator. You paste a link, it draws a code, and you can download a PNG or an A4 sheet of four. It runs entirely in the browser with the qrcode packa

Our site has a free QR code generator at pub-trivia.app/tools/qr-code-generator. You paste a link, it draws a code, and you can download a PNG or an A4 sheet of four. It runs entirely in the browser with the qrcode package, so the link you type never leaves your machine.

I had measured how big the tool is. I had never asked how long a link it will take, what the code looks like at that length, or what the page does when it refuses. Those are three separate questions with three separate answers, and the third one is a bug.

The ceiling

The tool calls QRCode.toDataURL(url, { width: 512, margin: 2 }) and leaves error correction on the library default, level M. At level M the biggest code a QR can be is version 40, which holds 2,331 bytes. I found the limit by binary search on the real function: a URL of 2,331 ASCII characters encodes, and a URL of 2,332 throws.

Non-ASCII text hits it sooner, because the capacity is counted in bytes. A hundred copies of é after the domain is 120 characters but 220 bytes, and it needs a version 11 code, not the version 6 that 120 ASCII characters would.

Nobody should put 2,331 characters into a QR code. But the limit matters for what comes next.

What 512 pixels buys you

The tool always asks for a 512 by 512 PNG, whatever the link length. A longer link means more modules (the little squares) in the same space, so each one shrinks. The A4 sheet places the image at 78 mm square (two columns, two rows, with margins and a label line), so I can also compute the size of one printed module:

URL length Version Modules per side Pixels per module in the PNG Printed module on the A4 sheet
20 2 25 17.7 2.69 mm
60 4 33 13.8 2.11 mm
100 6 41 11.4 1.73 mm
200 10 57 8.4 1.28 mm
500 17 85 5.8 0.88 mm
1,200 29 133 3.7 0.57 mm
2,000 38 169 3.0 0.45 mm

(These include the two-module quiet zone. Versions come from the library's own create() call, and the module counts are the standard 17 plus 4 per version.)

A normal venue link, something like pub-trivia.app/play/ plus a short code, lands around version 3 and prints at two or more millimetres a module, which is comfortable at table-card size. A link with a long tracking tail of 500 characters drops under a millimetre. I did not scan these with a phone, so I will not tell you where scanning stops. The numbers just show how quickly the margin shrinks, and the first thing to fix for a worse code is a shorter link, not a higher print resolution.

What happens at 2,332

Here is the part I did not expect. I opened the live tool in a real browser and drove it three ways.

  1. A too-long link with nothing generated yet. The page shows "Could not generate a code for that address." and no image. Correct.
  2. A good link, then a too-long link. The page shows the same error message, and the old code is still on screen, along with the PNG and A4 sheet of four buttons. I compared the image before and after: the same 11,226-character data URL, ending in the same characters.
  3. A good link, then a malformed one (just https://). The page shows "That does not look like a web address." and the image and both download buttons disappear.

So the tool has two failure paths and they do different things. The "not a web address" branch clears the stored code. The "could not generate" branch, in a catch block, sets the error and leaves the previous code where it was.

Why it matters: the person has typed a new link, pressed the button, and sees the old one's code plus an error message. If they skim past the message, they download the stale code and print four copies of it. A QR code looks the same whatever it says, so nothing about the printed sheet would warn them. They would find out when a customer scans it.

You can reproduce this on the live page in under a minute:

  1. Open the generator.
  2. Type example.com/one and press Generate code. A code appears.
  3. Replace the link with a very long one, for instance example.com/ followed by 2,400 letters a.
  4. Press Generate code again. Look at what is on screen.

A one-line fix would do it: clear the stored code in the same catch that sets the error, which is exactly what the other branch already does. Setting the code to null whenever the input changes would be a better one, because then a stale code could never sit beside a changed field at all.

The tool also has no maximum length

The link input declares no maxLength, and the page does not tell you the limit. That is a reasonable choice for a field whose normal use is 30 characters, but combined with the bug above it means the only way to learn the ceiling is to hit it. The poster maker next door does it differently: it caps its optional QR link at 300 characters before encoding, so it never reaches the library's limit. Two tools on one site, two policies for the same input.

What I would take from this

  1. Test the failure path with state, not just from cold. Case 1 above behaves perfectly. The bug only exists when something succeeded first, and a test that starts from an empty page will never see it.
  2. Error handling is a set of branches, and each branch must reset the same things. Here there were two, and they disagreed about whether to clear the result.
  3. Output that looks identical whatever it says needs the strongest guard. A QR code cannot be proofread by looking at it.
  4. Compute the physical size, not just the pixel size. "512 by 512" says nothing about print. Millimetres per module is the figure a person with a printer actually needs.

The tool is free, needs no account and sends nothing anywhere. If you are running quiz nights and want the product the codes were made for, there is a free tier with no card at pub-trivia.app.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.