Best Practices
Follow these guidelines to build performant, reliable, and secure applications with DocCentral.
Performance
Optimize rendering, reduce re-renders, and improve user experience.
Learn more
Error Handling
Handle errors gracefully and provide helpful feedback to users.
Learn more
Security
Protect API keys, validate input, and secure document handling.
Learn more
Quick Tips
Performance
- Use selectors with
useDocumentStoreto prevent unnecessary re-renders - Lazy load the SDK only when document viewing is needed
- Implement virtual scrolling for documents with many pages
- Cache document metadata to reduce API calls
Error Handling
- Always wrap SDK operations in try-catch blocks
- Implement retry logic with exponential backoff
- Provide user-friendly error messages
- Log errors for debugging while keeping sensitive data out of logs
Security
- Never expose secret keys (
sk_*) in client-side code - Validate all user input before submission
- Use HTTPS in production
- Implement proper CORS configuration
Code Example: Production Setup
Recommended Setuptsx
import { DocCentralProvider, useSDK, useDocumentStore } from '@doccentral/react';
import { ErrorBoundary } from 'react-error-boundary';
// 1. Wrap SDK in error boundary
function App() {
return (
<ErrorBoundary fallback={<ErrorFallback />}>
<DocCentralProvider
apiKey={process.env.NEXT_PUBLIC_DOCCENTRAL_API_KEY!}
debug={process.env.NODE_ENV === 'development'}
timeout={30000}
onError={handleSDKError}
onReady={handleSDKReady}
>
<DocumentApp />
</DocCentralProvider>
</ErrorBoundary>
);
}
// 2. Use selectors for optimal rendering
function PageIndicator() {
// Only re-renders when these specific values change
const currentPage = useDocumentStore((s) => s.currentPage);
const totalPages = useDocumentStore((s) => s.totalPages);
return <span>{currentPage} / {totalPages}</span>;
}
// 3. Handle errors gracefully
function handleSDKError(error: string) {
// Log to monitoring service
console.error('[DocCentral Error]', error);
// Don't expose internal errors to users
if (error.includes('API key')) {
showToast('Configuration error. Please contact support.');
} else {
showToast('Something went wrong. Please try again.');
}
}Development vs Production
Enable
debug: true in development to see detailed SDK logs, but disable it in production to avoid performance overhead and potential information leakage.