DEV Community

URL as a database: custom word decks without login

/custom lets users enter 10-30 words and generate a link. Someone else opens the link and plays the same set. No login.

So the question is: where do those 30 words live? The lightest option is URL fragment encoding:

// encode
const payload = JSON.stringify(words);
const encoded = compressToEncodedURIComponent(payload); // lz-string
const url = `${location.origin}/custom#${encoded}`;

// decode (after hydration)
const raw = location.hash.slice(1);
if (raw) {
  const words = JSON.parse(decompressFromEncodedURIComponent(raw));
  validate(words); // required: 10-30 items, per-word length cap, strip control chars
}

Why the fragment

I use the fragment, not the query string, because fragments never leave the browser - the word set stays invisible to the server.

lz-string's compressToEncodedURIComponent outputs URL-safe characters, no extra escaping needed.

The cost: long links

The real cost of this approach: 30 words compressed is still a few hundred characters. The link is long. It wraps in chat.

My first attempt was plain Base64:

btoa(unescape(encodeURIComponent(JSON.stringify(words))))

btoa doesn't accept non-Latin1 characters, so custom words with emoji or non-English characters throw. lz-string's compression has limited gains on short text, but at least it solves the charset problem.

Where "short link" is a stretch I called it a "share link," and the word "short" is generous. If I wanted a genuinely short link, I'd need a KV store with short IDs - cost: server-side state, expiry handling, abuse handling.

That's also why the /custom page has a report button: as soon as a shared link can put content in front of other people, a governance surface exists, even if the main flow is stateless.

The trap: validate input

The trap: decoding must validate input, or the client becomes an XSS surface. validate isn't optional:

function validate(words) {
  if (!Array.isArray(words)) throw new Error('bad');
  if (words.length < 10 || words.length > 30) throw new Error('count');
  return words.map(w => String(w).slice(0, 60).replace(/[\u0000-\u001f]/g, ''));
}

How to verify

Generate a deck at sketchwordgen.com/custom, copy the link, look at what's after the #. Base64/compressed string = approach A; short ID = approach B. That experiment is worth more than anything I say.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.