bundle-conditional — Claude Code rule for conditional loading
The bundle-conditional rule recommends loading large modules only when their feature is activated. It applies to lazy-loaded data and browser-only code in SSR builds.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/bundle-conditional.mdWhat bundle-conditional does
bundle-conditional recommends loading large data or modules only when a feature needs them. Its React example imports animation frames after the player is enabled, shows a placeholder while they load, and disables the feature if the import fails.
The typeof window !== 'undefined' check keeps a browser-only module out of the server bundle during SSR, reducing bundle size and build time. Without that check, the server build could include code intended for the browser. In Claude Code, the rule file goes at .claude/rules/bundle-conditional.md.
What it helps with
- defer animation-frame loading until the player is enabled
- keep browser-only modules out of an SSR server bundle
- show a placeholder while module data loads
- turn off a feature when its import fails
Example prompts for the agent
- Apply
bundle-conditionalto the animation player’s frame loading. - Check whether this module can load only after its feature is enabled.
- Add an SSR check so browser-only code stays out of the server bundle.
How to install bundle-conditional
Manually: put the files in place
.claude/rules/bundle-conditional.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/bundle-conditional.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/bundle-conditional.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/bundle-conditional.mdManually: put the files in place
.clinerules/bundle-conditional.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about bundle-conditional
How do I install bundle-conditional in Claude Code?
Add the rule file at .claude/rules/bundle-conditional.md.
What does bundle-conditional do?
It recommends loading large modules or data when the related feature is activated. The example uses import() to fetch animation frames after the player is enabled.
Why does the rule check typeof window !== 'undefined'?
The check prevents a browser-only module from entering the server bundle during SSR. That reduces server bundle size and build time.
Source text
bundle-conditional.md · MIT
---
title: Conditional Module Loading
impact: HIGH
impactDescription: loads large data only when needed
tags: bundle, conditional-loading, lazy-loading
---
## Conditional Module Loading
Load large data or modules only when a feature is activated.
**Example (lazy-load animation frames):**
```tsx
function AnimationPlayer({ enabled, setEnabled }: { enabled: boolean; setEnabled: React.Dispatch<React.SetStateAction<boolean>> }) {
const [frames, setFrames] = useState<Frame[] | null>(null)
useEffect(() => {
if (enabled && !frames && typeof window !== 'undefined') {
import('./animation-frames.js')
.then(mod => setFrames(mod.frames))
.catch(() => setEnabled(false))
}
}, [enabled, frames, setEnabled])
if (!frames) return <Skeleton />
return <Canvas frames={frames} />
}
```
The `typeof window !== 'undefined'` check prevents bundling this module for SSR, optimizing server bundle size and build speed.
© tech-leads-club · MIT · tech-leads-club/agent-skills