Client Hints
Client hints provide a solution for server-side rendering to respect user preferences without causing content layout shift. This system addresses limitations in server-side rendering where the browser doesn't provide enough information about user preferences.
Key Features
Supported Preferences
- Color scheme (light/dark mode)
- Data saver preferences
- Time zone
- User locale
- Motion preferences
- Cookie preferences
Benefits
- Eliminates flash of incorrect UI
- Provides consistent server-side rendering
- Maintains user preferences across sessions
- Zero content layout shift
How It Works
The system uses cookies to maintain user preferences:
-
First Visit:
- Quick check for existing preference cookies
- If not present, loads minimal document
- Sets cookies with user preferences
- Reloads page with correct preferences
-
Subsequent Visits:
- Server reads preferences from cookies
- Renders content correctly on first load
- No flash of incorrect content
- No content layout shift
-
Preference Changes:
- Monitors for preference changes
- Updates cookies automatically
- Triggers reload only if necessary
- Maintains consistency across sessions
Implementation
Server-Side Usage
import { getHints } from '#app/utils/client-hints'
export async function loader({ request }: LoaderArgs) { const hints = getHints(request) // Access user preferences const theme = hints.theme const timeZone = hints.timeZone // ... render with correct preferences }
Client-Side Usage
import { useHints } from '#app/utils/client-hints'
export function Component() { const hints = useHints() // Use preferences in component const theme = hints.theme const timeZone = hints.timeZone // ... render accordingly }
Cookie Management
Cookie Structure
- Secure, HTTP-only cookies
- Appropriately scoped to domain
- Contains serialized preference data
- Automatically managed by the system
Preference Updates
When user preferences change:
- System detects the change
- Updates relevant cookies
- Triggers minimal reload if necessary
- Maintains consistency
Fallback Behavior
For users with cookies disabled:
- System detects cookie limitations
- Falls back to default values
- Still functions, but may experience layout shift
- Degrades gracefully
Best Practices
-
Always use the provided utilities:
getHints()
for server-sideuseHints()
for client-side
-
Handle preference changes gracefully:
- Monitor for updates
- Update UI smoothly
- Avoid unnecessary reloads
-
Consider fallback values:
- Define sensible defaults
- Handle missing preferences
- Provide graceful degradation
-
Performance considerations:
- Minimize cookie size
- Optimize reload triggers
- Cache preference checks
Security Considerations
The client hints system includes several security measures:
-
Cookie Security:
- HTTP-only flags
- Secure flags when appropriate
- Same-site protection
- Appropriate cookie scoping
-
Data Validation:
- Input sanitization
- Type checking
- Value validation
-
Privacy Protection:
- Minimal data collection
- User control over preferences
- Clear preference management
Integration with Other Features
Client hints integrate seamlessly with:
-
Theme System
- Consistent dark/light mode
- Custom theme preferences
- System theme detection
-
Localization
- Time zone handling
- Locale preferences
- Number formatting
-
Performance Features
- Data saver modes
- Motion preferences
- Loading optimizations
For more details about security implications, check the security documentation.
Future Enhancements
The system is designed to be future-proof:
-
Web Platform Integration:
- Ready for native client hints
- Follows web standards
- Easy migration path
-
Preference Expansion:
- Extensible preference system
- Custom preference support
- Feature detection
-
Performance Optimization:
- Reduced reload requirements
- Better caching strategies
- Smoother transitions
For implementation details in your deployment, see the deployment documentation.