Agent Wranglers

WEB-213Recommended

Store each preference where it is needed

Keep a visitor's preferences in local storage when only the browser needs them. Use a cookie only when the server must know the preference before it answers.

How to do it

Local storage stays on the device and is never sent with requests. A theme or a dismissed notice belongs there, read by a tiny script in the head before the first paint. A cookie travels with every request, so the server can send the right language or currency at once. Give such a cookie Secure, SameSite=Lax and no longer a life than the preference needs. Under EU rules, storage the visitor asked for is usually exempt from consent, but say it in the privacy policy. Never set a cookie only for your own scripts to read. Wrap storage in try, because private windows can block it. Ask first for any other storage, as Store nothing extra without a yes says.

Examples

QuestionLocal storageCookie
Sent to the server?NeverWith every request
Server can use it for the first response?NoYes
Read before first paint?Yes, from an inline scriptYes, by the server
SizeAbout 5 MB per siteAbout 4 KB each
Good forTheme, layout, dismissed noticesLanguage, currency, region

HTML example

<!-- Local storage: applied before the first paint, never leaves the device -->
<script>try { const t = localStorage.getItem("theme"); if (t) document.documentElement.dataset.theme = t } catch (e) {}</script>

HTTP example

Set-Cookie: lang=sv; Path=/; Max-Age=31536000; Secure; SameSite=Lax

How to check

Set the preference and reload. It shows at once with no flash, and requests carry no cookie unless the server reads it.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/privacy/preference-storage">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.