Avoid crypto.randomUUID() TypeError by adding uuid helper #371
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The client currently calls
crypto.randomUUID()directly (notably ininitializeAnalyticsandToggle). In environments wherecrypto.randomUUIDis not available, this throws aTypeErrorand crashes the frontend, causing the app to show a fallback "Browser not supported / JavaScript disabled" banner.This PR introduces a small
uuid()helper and replaces direct calls tocrypto.randomUUID()withuuid(), preventing the crash and improving compatibility.Changes
client/src/lib/uuid.ts (new)
Adds uuid(): string which:
returns native crypto.randomUUID() if present
otherwise builds an RFC4122 v4 UUID using crypto.getRandomValues() if available
otherwise uses a last-resort Math.random() based UUID
client/src/lib/utils.ts (modified)
import { uuid } from '$lib/uuid';
replace uniqueId = crypto.randomUUID() with uniqueId = uuid();
client/src/lib/components/Toggle.svelte (modified)
import { uuid } from '$lib/uuid';
replace toggle id generation to use uuid()
Rationale
Avoids runtime crash on browsers/environments without crypto.randomUUID
Keeps behavior deterministic: still uses secure randomness when available
Minimal change surface and no new runtime dependency
Risk and backward compatibility
The only functional change is how the UUID is created when the native function is missing.
Analytics ID stored in localStorage continues to be a string UUID, so downstream code continues to work.
If maintainers prefer a global polyfill for crypto.randomUUID(), that can be done instead; this PR chooses an explicit helper to avoid mutating global state.