Advancednote 04 of 0803 reasons · 03 notes
JavaScriptAdvanced
Keep modules side-effect light
Make imports predictable by moving startup work into explicit functions.
Code example
Copy, adapt, and ship.
Read the numbered lines, copy the complete snippet, then adjust the values for your project.
// analytics.jsexport function initAnalytics() { window.analytics?.load();} // app.jsimport { initAnalytics } from "./analytics"; initAnalytics();Why this matters
The production reason.
- 01
Imports should be boring. If importing a module starts timers, reads the DOM, or sends analytics, it becomes harder to test and split.
- 02
Keep modules focused on exported values and functions. Run startup work explicitly from the place that owns it.
- 03
This makes bundling, lazy loading, and server/client boundaries easier to reason about.
How it holds up outside a demo.
- JavaScriptAvoid top-level DOM reads in shared modules.
- Next.jsKeep browser-only startup code inside client components or effects.
- TestingSide-effect-light modules are easier to import in unit tests.
Explore other stacks
Jump into a different technology.
08 JavaScript notes on this shelf, and six more stacks beside it.
Use next/image for image optimization
Next.js image optimization starts with next/image sizing, lazy loading, modern formats, and predictable dimensions.
Open the noteKeep state close to the interaction
Local state avoids unnecessary parent renders and keeps component intent obvious.
Open the noteBuild a CSS gradient generator with code export
A CSS gradient generator should preview color stops, preserve accessible contrast, and export clean CSS code.
Open the noteFix WordPress H1 SEO issues
Fix WordPress SEO issues by keeping one clear H1, escaping template output, and compressing images before WordPress upload.
Open the noteReduce LCP with image optimization
Reduce LCP with image optimization by resizing, converting, and compressing images before they reach production.
Open the noteUse real buttons for actions
If it changes state, use button. If it navigates, use a link.
Open the note