Search

Explore Fieldwork

Writing / Field note

The browser that lost consent

A Safari cookie consent banner kept reopening right after visitors clicked Accept because a Next.js Server Action re-render emitted a fresh action reference.

Conceptual dark-glass editorial artwork of two curved concentric glass arcs where an inner acid-lime point stays latched as an outer arc glides past with an amber glint.
Conceptual illustration of saved browser consent state persisting across client re-renders.

A bug that reproduces only in one browser is often blamed on storage quirks. This one was deterministic React state overwritten by a fresh Server Action reference.

In version 0.7.2 of our Matomo analytics plugin for Next.js storefronts, visitors in Safari could click “Accept all”, save their consent cookie, and still watch the consent banner stay open or immediately reopen. Reloading the page proved the cookie had actually been written, yet live client state snapped back to undecided right after saving. Earlier checks in Chrome had passed, which made the Safari report easy to misread as a cookie timing problem. Why Chrome masked the same re-render was not established.

The root cause sat inside the Next.js App Router and React effect lifecycle of MatomoAnalyticsConsent. That Client Component received four Server Action functions as props (retrieveAnalyticsState, saveAnalyticsConsent, trackAnalyticsEvent, and savePurposeConsent) alongside the server-resolved initialState.

A single useEffect depended on retrieveAnalyticsState, trackAnalyticsEvent, cookieName, and initialState. When a Client Component invokes a Server Action that mutates cookies with (await cookies()).set(), Next.js re-renders the route and passes freshly serialized function references for those Server Action props. Because trackAnalyticsEvent sat in the same useEffect dependency array as one-time consent initialization, saving consent changed the prop reference, re-ran initialization, and called analyticsStore.resolve(initialState) with the pre-save initialState, overwriting the visitor's new choice in memory.

Separating connection updates from one-time resolution

The fix in commit b85abba (released as medusa-plugin-analytics-matomo v0.7.3) split connection wiring from one-time consent resolution using a useRef(false) guard:

TypeScript
const initialized = useRef(false)

useEffect(() => {
  analyticsStore.connect(trackAnalyticsEvent, { cookieName })
}, [trackAnalyticsEvent, cookieName])

useEffect(() => {
  if (initialized.current) return
  if (initialState) {
    initialized.current = true
    analyticsStore.resolve(initialState)
    return
  }
  // Without server-resolved state, ask the Server Action once.
  retrieveAnalyticsState().then(state => {
    initialized.current = true
    analyticsStore.resolve(state)
  })
}, [retrieveAnalyticsState, initialState])

Splitting connection binding from one-time consent resolution lets refreshed Server Action references update the sender without replaying stale initial state over a visitor's choice.

I added react-test-renderer regression tests simulating Next.js Server Action reference replacement across both “Accept all” and “Reject” flows, confirming that the decided state survives re-renders.

Verification and boundaries

Version 0.7.3 passed 453 unit tests and 10 integration contract tests, shipped to npm, and deployed through staging into production. Post-deploy checks in both Safari and Chrome confirmed that accept, reject, and custom preference choices persisted across navigation and hard reloads with clean browser consoles.

Two boundaries stay separate from this fix. Neither of these is part of the fix claim. First, an earlier Safari session had logged an unrelated t.appendChild exception whose cause was never established; this release does not claim to fix it. Second, an automated secret-detection job in the deployment pipeline failed to pull its CI helper image, leaving that security check incomplete during the rollout.

Share this article

Continue reading

Accessibility bugs are product bugs ↗

All writing ↗