Our $19 report has no database row. The whole purchase lives in the URL fragment.
check.tattoo scores a tattoo idea before you get it. The score is free. The full report is a one-time $19 payment for one design: the reasoning behind each score, what to change, and a brief for the artist. There are no
check.tattoo scores a tattoo idea before you get it. The score is free. The full report is a one-time $19 payment for one design: the reasoning behind each score, what to change, and a brief for the artist.
There are no accounts, and that created a problem.
The problem: a purchase that lives in localStorage
After payment, two things are written to the browser: the entitlement ("this design is unlocked") and the report itself. Both live in localStorage.
localStorage is not durable. Safari's tracking prevention can evict script-written storage after about a week without a return visit. So a buyer could pay, come back ten days later on an iPhone, and find nothing.
The usual fix is a restore code and a table: store the purchase server-side, hand the buyer a code, and look it up later. We considered it and rejected it for a reason specific to this product.
A lookup would restore the right to the report, not the report. The report is generated by a model running at temperature 0.4. If you re-grant the unlock and re-run the check, you get a different report, with different wording and maybe different numbers. Handing back different numbers isn't restoring a purchase.
So the restore link carries the whole report.
The design: put the purchase in the fragment
After paying, the buyer gets a link like this:
https://check.tattoo/restore#r=<base64url of the whole report>
Everything after # is the fragment, and browsers never send the fragment to a server. So this design stores nothing: no table of purchases, no codes anyone could guess, no personal data, nothing to delete later.
Opening the link re-installs the exact report that was paid for, for that one design.
The trade-off is explicit in the code: anyone holding the link gets that report. That's the same property a downloaded PDF has. It covers one design and can't unlock anything else.
Encoding: three details that bite
The payload is JSON, base64url-encoded. Three things go wrong if you write this the obvious way:
export const encodeFragment = (payload: unknown): string => {
const bytes = new TextEncoder().encode(JSON.stringify(payload));
let latin1 = "";
for (const b of bytes) latin1 += String.fromCharCode(b);
return btoa(latin1).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
};
-
btoais latin1-only and throws on anything above U+00FF. Model output routinely contains an em dash, so you have to go throughTextEncoderfirst. -
+,/and=are not safe in a fragment, hence base64url with the padding stripped. - Decoding parses text from a URL anyone can edit, so it must never throw:
export const decodeFragment = (hash: string, key: string): unknown => {
try {
const m = new RegExp(`[#&]${key}=([A-Za-z0-9_-]+)`).exec(hash);
if (!m) return undefined;
const b64 = m[1].replace(/-/g, "+").replace(/_/g, "/");
const latin1 = atob(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
const bytes = new Uint8Array(latin1.length);
for (let i = 0; i < latin1.length; i += 1) bytes[i] = latin1.charCodeAt(i);
return JSON.parse(new TextDecoder().decode(bytes));
} catch {
return undefined;
}
};
Keeping the URL short enough
The report includes six dimension summaries, two or three recommendations and an artist brief. To keep the URL manageable, the payload is positional: an array, not an object. Keys would add hundreds of characters.
[version, designKey, orderId, overall, headline,
[[score, verdict, summary] Γ 6],
[[priority, title, detail] Γ n],
artistBrief, contextLine]
Every field has a length cap (headline 300, each summary 1,200, the brief 4,000, at most five recommendations). According to a note in the code, five real reports measured between 2,829 and 3,443 characters. That's long for a URL, but it's the whole purchase.
A version number comes first, so the layout can change later without old links decoding into garbage.
Validate, don't trust
Decoding checks the shape and rejects anything off. The version must match, scores must be integers from 0 to 100, and there must be exactly six dimensions. Strings are cut to their caps again on the way in. A shortened excerpt:
if (v !== RECOVERY_VERSION) return null;
if (typeof designKey !== "string" || !designKey) return null;
if (!isScore(overall) || typeof headline !== "string") return null;
if (!Array.isArray(dims) || dims.length !== DIMENSION_IDS.length) return null;
The catch: "browsers never send the fragment" isn't the whole story
The browser doesn't send the fragment, but scripts on the page can read it. Our host injects a page-view measurement script into every page, and that script posts window.location.href, fragment included, to its own endpoint shortly after load. We can't change that script.
So the fragment has to leave the address bar before any deferred script runs. A synchronous inline script at the top of <head> does it:
<script>
(function () {
function strip() {
try {
var l = window.location;
if (/^\/restore\/?$/i.test(l.pathname) && /[#&]r=/.test(l.hash)) {
window.__restoreFragment = l.hash;
try { sessionStorage.setItem("checktattoo.restoreFragment.v1", l.hash); } catch (e) {}
history.replaceState(history.state, "", l.pathname + l.search);
}
} catch (e) {}
}
strip();
window.addEventListener("hashchange", strip, true);
window.addEventListener("popstate", strip, true);
})();
</script>
Three details:
-
It runs before anything deferred, so by the time the measurement script reads
location.href, the address is plain/restore. -
The payload goes to
sessionStorage, which is scoped to the tab and cleared when the tab closes. A reload still works, and nothing is left behind. -
hashchangeandpopstatelisteners, in the capture phase. If someone pastes a link into a tab already on/restore, that's a same-document navigation and the inline script doesn't run again. The listeners catch that case before any listener the host adds later.
Restoring is a button, not a side effect
Opening a recovery link doesn't restore anything by itself. Restoring writes to localStorage and replaces whatever report is already there. Someone who opens a friend's link out of curiosity shouldn't lose their own report without being asked. So /restore shows what it found and waits for a click. If a different paid report is already in that slot, it's archived rather than overwritten.
When this pattern fits
- The thing being restored is deterministic data you already have, not something you'd regenerate.
- It's scoped. One link, one item; sharing a link gives away only that item.
- You'd rather hold no personal data than secure some.
- You control what runs before third-party scripts, or you check what they read.
It doesn't fit if a purchase needs to be revocable, or if the payload is too large or too sensitive to hand to the user.
Our site has a page about the restore link for buyers. The tool itself is free to try at check.tattoo.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.