I built a small personality quiz, a 7 heavenly virtues test that scores you on seven traits from 0 to 100. People wanted to send their result to a friend. The site is static files on a CDN, with no database and no accounts, so the result had to live somewhere the link itself could carry it.
The answer was the query string. The first version of a result link looked like this:
https://7virtuestest.org/?r=72558190436067#test Those fourteen digits are the seven scores, two digits each: 72, 55, 81, 90, 43, 60, 67.
Encoding
function encode(scores: number[]) { return scores.map((s) => String(Math.min(99, s)).padStart(2, '0')).join(''); } Two details matter here.
padStart(2, '0') keeps every score exactly two characters wide, so 7 becomes 07. Without it the string has no fixed layout and can't be split back apart.
Math.min(99, s) is a deliberate loss. A perfect score of 100 would need three digits and break the fixed width. I could have used three digits for every score, but that makes every link 21 digits long to support a case that almost never happens. A shared 100 shows up as 99. Nobody has complained.
Decoding, defensively
function decode(r: string | null): number[] | null { if (!r || !/^\d{14}$/.test(r)) return null; return Array.from({ length: 7 }, (_, i) => Number(r.slice(i * 2, i * 2 + 2))); } Anything in a URL is user input. People truncate links when they paste them, chat apps append tracking parameters, and some people will edit the digits by hand to see what happens. The regex accepts exactly fourteen digits and nothing else. Anything that fails it returns null, and the page simply starts a fresh quiz instead of drawing a broken chart.
Hand-edited digits still pass, of course. That is fine: the worst case is someone showing their friend a result they didn't earn, which is their business.
Writing the URL without reloading
When someone finishes the quiz, the page updates the address bar in place:
if (!shared) history.replaceState(null, '', `?r=${encode(scores)}#test`); replaceState rather than pushState, because a finished quiz isn't a new page. If it pushed a history entry, the back button would go from "your result" to "your result without the digits", which feels broken.
The !shared check matters too. If you arrive from a friend's link, the URL already holds their result, and the page shouldn't rewrite it.
Restarting the quiz clears the digits again:
history.replaceState(null, '', location.pathname + '#test'); Reading it on load
const shared = decode(new URLSearchParams(location.search).get('r')); if (shared) finish(shared, true); else renderQuestion(); The same finish() function draws both your own result and a shared one. The second argument only changes small things, such as the restart button reading "Take the test yourself" instead of "Take the test again", which turns out to be a good invitation for the friend who opened the link.
What this approach does not give you
- No counts. I can't tell how many results were shared, only how many pages were viewed.
- No privacy beyond obscurity. Anyone with the link sees the scores. For a quiz about patience and humility that's acceptable; for anything sensitive it wouldn't be.
- No way to change the format later without breaking old links. If I ever add an eighth trait, old fourteen-digit links still need to decode, so the regex would have to accept both lengths.
Version two: hex, and the result in the path
That last point came back sooner than I expected, for a different reason: link previews. Chat apps and social sites fetch a shared URL and read its og:image without running JavaScript, and a static site serves the same HTML whatever the query string says. Every shared result showed the same generic card.
So result links now look like this:
https://7virtuestest.org/result/patience/?s=64483c2b51370a The strongest virtue moved into the path. There are only seven of them, so the build generates seven static result pages (per language), each with its own title and preview image. The full scores stay in the query string, and JavaScript draws the chart from them.
The scores themselves switched to hex. Two hex digits hold 0 to 255, so 100 finally fits without the Math.min(99, …) compromise:
export function encodeScores(scores: number[]): string { return scores .map((s) => Math.max(0, Math.min(100, Math.round(s))).toString(16).padStart(2, '0')) .join(''); } export function decodeScores(s: string | null): number[] | null { if (!s || !/^[0-9a-f]{14}$/i.test(s)) return null; const scores = Array.from({ length: 7 }, (_, i) => parseInt(s.slice(i * 2, i * 2 + 2), 16)); return scores.every((n) => n <= 100) ? scores : null; } Still fourteen characters, still validated before use, with one extra check: hex can spell 255, so anything over 100 is rejected.
Two things carried over from version one:
- Old links still work. The home page still reads
?r=, decodes the fourteen digits, and redirects to the new result page. Links people pasted into group chats months ago keep opening. - The path is a hint, the scores are the truth. If someone edits the digits so that a different virtue comes out on top, the page recomputes the strongest virtue and
location.replaces itself onto the right path, so the preview and the reading always agree.
The "your result" versus "their result" wording also moved: instead of a shared flag, the quiz stores the encoded scores in sessionStorage just before it navigates, and the result page checks whether they match.
For a static site, fourteen characters still replace an entire backend. The trade was worth it, twice.
