Device-ID-based log tracing
getDeviceId() returns the device's unique identifier synchronously. Sending the device ID with server logs lets you correlate events from the same device across multiple sessions.
Register the device ID in the log context on app init
import { getDeviceId } from '@apps-in-toss/web-framework';
import { useEffect } from 'react';
// Read once at module scope and cache — the value doesn't change during a session.
const deviceId = getDeviceId();
function App() {
useEffect(() => {
// Notify the server of app start, including the device ID.
fetch('/api/sessions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ deviceId }),
});
}, []);
return <main>{/* app content */}</main>;
}
The device ID is stable for the lifetime of the app, so reading it once at module scope is enough.
Use the device ID as a per-device storage key prefix
import { getDeviceId } from '@apps-in-toss/web-framework';
const DEVICE_ID = getDeviceId();
function storageKey(name: string): string {
return `${DEVICE_ID}:${name}`;
}
// Example: separate localStorage slots per device
function saveDevicePreference(theme: 'light' | 'dark'): void {
localStorage.setItem(storageKey('theme'), theme);
}
function loadDevicePreference(): 'light' | 'dark' | null {
return localStorage.getItem(storageKey('theme')) as 'light' | 'dark' | null;
}
When one user owns multiple devices, prefixing storage keys with the device ID keeps per-device settings independent.
Device ID can change
The device ID may change after a factory reset, OS upgrade, or app reinstall. Use a server-side user ID alongside the device ID for durable user identification.
Related APIs
getDeviceId— returns the device's unique identifier.getGroupId— include the group ID in server context alongside the device ID.env.getDeploymentId— include the deployment ID in error context alongside the device ID.