Code Splitting
Split flag components across routes to load only what’s needed per page.
Optimize your bundle size by choosing the right approach for your use case.
View detailed usage with code examples →
View detailed usage with code examples →
View detailed usage with code examples →
| Method | Bundle Size | Tree-shaking | Loading | Use Case |
|---|---|---|---|---|
| Named imports (3 flags) | ~2.8 KB | ✅ Excellent | Instant | Known flags at build time |
| Named imports (10 flags) | ~9.5 KB | ✅ Excellent | Instant | Common flags header |
| CircleFlag | ~2 KB | ✅ Perfect | Async CDN | Dynamic flags, minimal bundle |
| DynamicFlag | ~600 KB | ❌ None | Instant | CDN-restricted, all flags needed |
Code Splitting
Split flag components across routes to load only what’s needed per page.
Lazy Loading
Use React’s lazy() to defer loading flags until they’re actually rendered.
Static Analysis
Run bundle analysis regularly to catch unexpected size increases.
Use CircleFlag
For dynamic flags, prefer CircleFlag over DynamicFlag to avoid bundling unused flags.
Combine named imports for known flags with CircleFlag for dynamic ones:
import { FlagUs } from '@sankyu/react-circle-flags'import { CircleFlag } from '@sankyu/react-circle-flags'
export function Header({ userCountry }: { userCountry?: string }) { return ( <div className="flex gap-2"> {/* Static flags: bundled (~950 bytes) */} <FlagUs width={32} height={32} />
{/* Dynamic flag: loaded from CDN (~0 KB) */} {userCountry && <CircleFlag countryCode={userCountry} width={32} height={32} />} </div> )}Total bundle impact: ~2.9 KB (FlagUs + CircleFlag component)
Use these tools to verify your bundle size:
# Webpack Bundle Analyzernpm install --save-dev webpack-bundle-analyzer
# Vite Bundle Analyzernpm install --save-dev rollup-plugin-visualizer
# Next.js Bundle Analyzernpm install --save-dev @next/bundle-analyzer