@jgamaraalv/ts-dev-kit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/.claude-plugin/marketplace.json +24 -0
  2. package/.claude-plugin/plugin.json +24 -0
  3. package/CHANGELOG.md +24 -0
  4. package/LICENSE +21 -0
  5. package/README.md +128 -0
  6. package/agents/accessibility-pro.md +139 -0
  7. package/agents/api-builder.md +110 -0
  8. package/agents/code-reviewer.md +190 -0
  9. package/agents/database-expert.md +138 -0
  10. package/agents/debugger.md +241 -0
  11. package/agents/docker-expert.md +51 -0
  12. package/agents/multi-agent-coordinator.md +378 -0
  13. package/agents/nextjs-expert.md +136 -0
  14. package/agents/performance-engineer.md +138 -0
  15. package/agents/playwright-expert.md +126 -0
  16. package/agents/react-specialist.md +97 -0
  17. package/agents/security-scanner.md +105 -0
  18. package/agents/test-generator.md +221 -0
  19. package/agents/typescript-pro.md +253 -0
  20. package/agents/ux-optimizer.md +93 -0
  21. package/docs/rules/orchestration.md.template +126 -0
  22. package/package.json +28 -0
  23. package/skills/bullmq/SKILL.md +225 -0
  24. package/skills/bullmq/references/flows-and-schedulers.md +186 -0
  25. package/skills/bullmq/references/job-types-and-options.md +163 -0
  26. package/skills/bullmq/references/patterns.md +273 -0
  27. package/skills/bullmq/references/production.md +308 -0
  28. package/skills/composition-patterns/SKILL.md +58 -0
  29. package/skills/composition-patterns/references/architecture-avoid-boolean-props.md +87 -0
  30. package/skills/composition-patterns/references/architecture-compound-components.md +107 -0
  31. package/skills/composition-patterns/references/patterns-children-over-render-props.md +77 -0
  32. package/skills/composition-patterns/references/patterns-explicit-variants.md +87 -0
  33. package/skills/composition-patterns/references/react19-no-forwardref.md +37 -0
  34. package/skills/composition-patterns/references/state-context-interface.md +194 -0
  35. package/skills/composition-patterns/references/state-decouple-implementation.md +96 -0
  36. package/skills/composition-patterns/references/state-lift-state.md +126 -0
  37. package/skills/conventional-commits/SKILL.md +148 -0
  38. package/skills/docker/SKILL.md +55 -0
  39. package/skills/docker/references/compose-configs.md +95 -0
  40. package/skills/docker/references/monorepo-dockerfile.md +111 -0
  41. package/skills/drizzle-pg/SKILL.md +202 -0
  42. package/skills/drizzle-pg/references/advanced.md +299 -0
  43. package/skills/drizzle-pg/references/migrations.md +214 -0
  44. package/skills/drizzle-pg/references/queries.md +321 -0
  45. package/skills/drizzle-pg/references/relations.md +272 -0
  46. package/skills/drizzle-pg/references/schema-pg.md +256 -0
  47. package/skills/drizzle-pg/references/sql-operator.md +215 -0
  48. package/skills/fastify-best-practices/SKILL.md +143 -0
  49. package/skills/fastify-best-practices/references/hooks-and-lifecycle.md +122 -0
  50. package/skills/fastify-best-practices/references/plugins-and-encapsulation.md +137 -0
  51. package/skills/fastify-best-practices/references/request-reply-errors.md +189 -0
  52. package/skills/fastify-best-practices/references/routes-and-handlers.md +134 -0
  53. package/skills/fastify-best-practices/references/server-and-options.md +127 -0
  54. package/skills/fastify-best-practices/references/typescript-and-logging.md +223 -0
  55. package/skills/fastify-best-practices/references/validation-and-serialization.md +190 -0
  56. package/skills/ioredis/SKILL.md +51 -0
  57. package/skills/ioredis/references/advanced-patterns.md +312 -0
  58. package/skills/ioredis/references/cluster-sentinel.md +280 -0
  59. package/skills/ioredis/references/connection-options.md +187 -0
  60. package/skills/ioredis/references/core-api.md +179 -0
  61. package/skills/nextjs-best-practices/SKILL.md +194 -0
  62. package/skills/nextjs-best-practices/references/async-patterns.md +84 -0
  63. package/skills/nextjs-best-practices/references/bundling.md +192 -0
  64. package/skills/nextjs-best-practices/references/data-patterns.md +310 -0
  65. package/skills/nextjs-best-practices/references/debug-tricks.md +127 -0
  66. package/skills/nextjs-best-practices/references/directives.md +74 -0
  67. package/skills/nextjs-best-practices/references/error-handling.md +237 -0
  68. package/skills/nextjs-best-practices/references/file-conventions.md +152 -0
  69. package/skills/nextjs-best-practices/references/font.md +175 -0
  70. package/skills/nextjs-best-practices/references/functions.md +116 -0
  71. package/skills/nextjs-best-practices/references/hydration-error.md +86 -0
  72. package/skills/nextjs-best-practices/references/image.md +184 -0
  73. package/skills/nextjs-best-practices/references/metadata.md +305 -0
  74. package/skills/nextjs-best-practices/references/parallel-routes.md +299 -0
  75. package/skills/nextjs-best-practices/references/route-handlers.md +154 -0
  76. package/skills/nextjs-best-practices/references/rsc-boundaries.md +168 -0
  77. package/skills/nextjs-best-practices/references/runtime-selection.md +40 -0
  78. package/skills/nextjs-best-practices/references/scripts.md +148 -0
  79. package/skills/nextjs-best-practices/references/self-hosting.md +210 -0
  80. package/skills/nextjs-best-practices/references/suspense-boundaries.md +67 -0
  81. package/skills/owasp-security-review/SKILL.md +98 -0
  82. package/skills/owasp-security-review/references/a01-broken-access-control.md +78 -0
  83. package/skills/owasp-security-review/references/a02-security-misconfiguration.md +81 -0
  84. package/skills/owasp-security-review/references/a03-supply-chain-failures.md +65 -0
  85. package/skills/owasp-security-review/references/a04-cryptographic-failures.md +82 -0
  86. package/skills/owasp-security-review/references/a05-injection.md +106 -0
  87. package/skills/owasp-security-review/references/a06-insecure-design.md +76 -0
  88. package/skills/owasp-security-review/references/a07-authentication-failures.md +83 -0
  89. package/skills/owasp-security-review/references/a08-integrity-failures.md +72 -0
  90. package/skills/owasp-security-review/references/a09-logging-alerting-failures.md +76 -0
  91. package/skills/owasp-security-review/references/a10-exceptional-conditions.md +131 -0
  92. package/skills/postgresql/SKILL.md +50 -0
  93. package/skills/postgresql/references/ddl-schema.md +300 -0
  94. package/skills/postgresql/references/indexes.md +257 -0
  95. package/skills/postgresql/references/jsonb.md +261 -0
  96. package/skills/postgresql/references/performance.md +291 -0
  97. package/skills/postgresql/references/psql-cli.md +153 -0
  98. package/skills/postgresql/references/queries.md +287 -0
  99. package/skills/postgresql/references/transactions.md +280 -0
  100. package/skills/react-best-practices/SKILL.md +110 -0
  101. package/skills/react-best-practices/references/advanced-patterns.md +91 -0
  102. package/skills/react-best-practices/references/async-patterns.md +233 -0
  103. package/skills/react-best-practices/references/bundle-optimization.md +201 -0
  104. package/skills/react-best-practices/references/client-patterns.md +178 -0
  105. package/skills/react-best-practices/references/js-performance.md +210 -0
  106. package/skills/react-best-practices/references/rendering-performance.md +209 -0
  107. package/skills/react-best-practices/references/rerender-optimization.md +316 -0
  108. package/skills/react-best-practices/references/server-performance.md +274 -0
  109. package/skills/service-worker/SKILL.md +195 -0
  110. package/skills/service-worker/references/api-reference.md +114 -0
  111. package/skills/service-worker/references/caching-strategies.md +202 -0
  112. package/skills/service-worker/references/push-and-sync.md +261 -0
  113. package/skills/typescript-conventions/SKILL.md +51 -0
  114. package/skills/ui-ux-guidelines/SKILL.md +105 -0
  115. package/skills/ui-ux-guidelines/references/accessibility-and-interaction.md +74 -0
  116. package/skills/ui-ux-guidelines/references/forms-content-checklist.md +126 -0
  117. package/skills/ui-ux-guidelines/references/layout-typography-animation.md +95 -0
@@ -0,0 +1,91 @@
1
+ # Advanced Patterns
2
+
3
+ Specialized React patterns for edge cases.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Store Event Handlers in Refs](#store-event-handlers-in-refs)
8
+ - [Initialize App Once Per Load](#initialize-app-once-per-load)
9
+
10
+ ---
11
+
12
+ ## Store Event Handlers in Refs
13
+
14
+ Store callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.
15
+
16
+ **Incorrect (re-subscribes on every render):**
17
+
18
+ ```tsx
19
+ function useWindowEvent(event: string, handler: (e) => void) {
20
+ useEffect(() => {
21
+ window.addEventListener(event, handler);
22
+ return () => window.removeEventListener(event, handler);
23
+ }, [event, handler]);
24
+ }
25
+ ```
26
+
27
+ **Correct (stable subscription):**
28
+
29
+ ```tsx
30
+ function useWindowEvent(event: string, handler: (e) => void) {
31
+ const handlerRef = useRef(handler);
32
+ useEffect(() => {
33
+ handlerRef.current = handler;
34
+ }, [handler]);
35
+
36
+ useEffect(() => {
37
+ const listener = (e) => handlerRef.current(e);
38
+ window.addEventListener(event, listener);
39
+ return () => window.removeEventListener(event, listener);
40
+ }, [event]);
41
+ }
42
+ ```
43
+
44
+ **Alternative: use `useEffectEvent` (React 19+):**
45
+
46
+ ```tsx
47
+ import { useEffectEvent } from "react";
48
+
49
+ function useWindowEvent(event: string, handler: (e) => void) {
50
+ const onEvent = useEffectEvent(handler);
51
+
52
+ useEffect(() => {
53
+ window.addEventListener(event, onEvent);
54
+ return () => window.removeEventListener(event, onEvent);
55
+ }, [event]);
56
+ }
57
+ ```
58
+
59
+ ---
60
+
61
+ ## Initialize App Once Per Load
62
+
63
+ Do not put app-wide initialization that must run once per app load inside `useEffect([])`. Components can remount and effects will re-run. Use a module-level guard.
64
+
65
+ **Incorrect (runs twice in dev, re-runs on remount):**
66
+
67
+ ```tsx
68
+ function Comp() {
69
+ useEffect(() => {
70
+ loadFromStorage();
71
+ checkAuthToken();
72
+ }, []);
73
+ }
74
+ ```
75
+
76
+ **Correct (once per app load):**
77
+
78
+ ```tsx
79
+ let didInit = false;
80
+
81
+ function Comp() {
82
+ useEffect(() => {
83
+ if (didInit) return;
84
+ didInit = true;
85
+ loadFromStorage();
86
+ checkAuthToken();
87
+ }, []);
88
+ }
89
+ ```
90
+
91
+ Reference: [Initializing the application](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)
@@ -0,0 +1,233 @@
1
+ # Async Patterns
2
+
3
+ Eliminate request waterfalls -- the #1 performance killer in React/Next.js apps.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Prevent Waterfall Chains in API Routes](#prevent-waterfall-chains-in-api-routes)
8
+ - [Defer Await Until Needed](#defer-await-until-needed)
9
+ - [Dependency-Based Parallelization](#dependency-based-parallelization)
10
+ - [Strategic Suspense Boundaries](#strategic-suspense-boundaries)
11
+
12
+ ---
13
+
14
+ ## Prevent Waterfall Chains in API Routes
15
+
16
+ In API routes and Server Actions, start independent operations immediately, even if you don't await them yet.
17
+
18
+ **Incorrect (config waits for auth, data waits for both):**
19
+
20
+ ```typescript
21
+ export async function GET(request: Request) {
22
+ const session = await auth();
23
+ const config = await fetchConfig();
24
+ const data = await fetchData(session.user.id);
25
+ return Response.json({ data, config });
26
+ }
27
+ ```
28
+
29
+ **Correct (auth and config start immediately):**
30
+
31
+ ```typescript
32
+ export async function GET(request: Request) {
33
+ const sessionPromise = auth();
34
+ const configPromise = fetchConfig();
35
+ const session = await sessionPromise;
36
+ const [config, data] = await Promise.all([configPromise, fetchData(session.user.id)]);
37
+ return Response.json({ data, config });
38
+ }
39
+ ```
40
+
41
+ ---
42
+
43
+ ## Defer Await Until Needed
44
+
45
+ Move `await` operations into the branches where they're actually used to avoid blocking code paths that don't need them.
46
+
47
+ **Incorrect (blocks both branches):**
48
+
49
+ ```typescript
50
+ async function handleRequest(userId: string, skipProcessing: boolean) {
51
+ const userData = await fetchUserData(userId);
52
+
53
+ if (skipProcessing) {
54
+ return { skipped: true };
55
+ }
56
+
57
+ return processUserData(userData);
58
+ }
59
+ ```
60
+
61
+ **Correct (only blocks when needed):**
62
+
63
+ ```typescript
64
+ async function handleRequest(userId: string, skipProcessing: boolean) {
65
+ if (skipProcessing) {
66
+ return { skipped: true };
67
+ }
68
+
69
+ const userData = await fetchUserData(userId);
70
+ return processUserData(userData);
71
+ }
72
+ ```
73
+
74
+ **Another example (early return optimization):**
75
+
76
+ ```typescript
77
+ // Incorrect: always fetches permissions
78
+ async function updateResource(resourceId: string, userId: string) {
79
+ const permissions = await fetchPermissions(userId);
80
+ const resource = await getResource(resourceId);
81
+
82
+ if (!resource) {
83
+ return { error: "Not found" };
84
+ }
85
+
86
+ if (!permissions.canEdit) {
87
+ return { error: "Forbidden" };
88
+ }
89
+
90
+ return await updateResourceData(resource, permissions);
91
+ }
92
+
93
+ // Correct: fetches only when needed
94
+ async function updateResource(resourceId: string, userId: string) {
95
+ const resource = await getResource(resourceId);
96
+
97
+ if (!resource) {
98
+ return { error: "Not found" };
99
+ }
100
+
101
+ const permissions = await fetchPermissions(userId);
102
+
103
+ if (!permissions.canEdit) {
104
+ return { error: "Forbidden" };
105
+ }
106
+
107
+ return await updateResourceData(resource, permissions);
108
+ }
109
+ ```
110
+
111
+ This optimization is especially valuable when the skipped branch is frequently taken, or when the deferred operation is expensive.
112
+
113
+ ---
114
+
115
+ ## Dependency-Based Parallelization
116
+
117
+ For operations with partial dependencies, use vanilla `Promise.all()` with `.then()` chaining to maximize parallelism. Start all promises upfront and let dependent operations chain off their prerequisites.
118
+
119
+ **Incorrect (profile waits for config unnecessarily):**
120
+
121
+ ```typescript
122
+ const [user, config] = await Promise.all([fetchUser(), fetchConfig()]);
123
+ const profile = await fetchProfile(user.id);
124
+ ```
125
+
126
+ **Correct (config and profile run in parallel):**
127
+
128
+ ```typescript
129
+ const userPromise = fetchUser();
130
+ const profilePromise = userPromise.then((user) => fetchProfile(user.id));
131
+
132
+ const [user, config, profile] = await Promise.all([userPromise, fetchConfig(), profilePromise]);
133
+ ```
134
+
135
+ **Optional: `better-all` library for complex dependency graphs:**
136
+
137
+ ```typescript
138
+ import { all } from "better-all";
139
+
140
+ const { user, config, profile } = await all({
141
+ async user() {
142
+ return fetchUser();
143
+ },
144
+ async config() {
145
+ return fetchConfig();
146
+ },
147
+ async profile() {
148
+ return fetchProfile((await this.$.user).id);
149
+ },
150
+ });
151
+ ```
152
+
153
+ Reference: [better-all](https://github.com/shuding/better-all)
154
+
155
+ ---
156
+
157
+ ## Strategic Suspense Boundaries
158
+
159
+ Instead of awaiting data in async components before returning JSX, use Suspense boundaries to show the wrapper UI faster while data loads.
160
+
161
+ **Incorrect (wrapper blocked by data fetching):**
162
+
163
+ ```tsx
164
+ async function Page() {
165
+ const data = await fetchData(); // Blocks entire page
166
+
167
+ return (
168
+ <div>
169
+ <div>Sidebar</div>
170
+ <div>Header</div>
171
+ <div>
172
+ <DataDisplay data={data} />
173
+ </div>
174
+ <div>Footer</div>
175
+ </div>
176
+ );
177
+ }
178
+ ```
179
+
180
+ **Correct (wrapper shows immediately, data streams in):**
181
+
182
+ ```tsx
183
+ function Page() {
184
+ return (
185
+ <div>
186
+ <div>Sidebar</div>
187
+ <div>Header</div>
188
+ <div>
189
+ <Suspense fallback={<Skeleton />}>
190
+ <DataDisplay />
191
+ </Suspense>
192
+ </div>
193
+ <div>Footer</div>
194
+ </div>
195
+ );
196
+ }
197
+
198
+ async function DataDisplay() {
199
+ const data = await fetchData(); // Only blocks this component
200
+ return <div>{data.content}</div>;
201
+ }
202
+ ```
203
+
204
+ **Alternative (share promise across components with `use()`):**
205
+
206
+ ```tsx
207
+ function Page() {
208
+ const dataPromise = fetchData();
209
+
210
+ return (
211
+ <div>
212
+ <div>Sidebar</div>
213
+ <Suspense fallback={<Skeleton />}>
214
+ <DataDisplay dataPromise={dataPromise} />
215
+ <DataSummary dataPromise={dataPromise} />
216
+ </Suspense>
217
+ <div>Footer</div>
218
+ </div>
219
+ );
220
+ }
221
+
222
+ function DataDisplay({ dataPromise }: { dataPromise: Promise<Data> }) {
223
+ const data = use(dataPromise);
224
+ return <div>{data.content}</div>;
225
+ }
226
+ ```
227
+
228
+ **When NOT to use this pattern:**
229
+
230
+ - Critical data needed for layout decisions (affects positioning)
231
+ - SEO-critical content above the fold
232
+ - Small, fast queries where suspense overhead isn't worth it
233
+ - When you want to avoid layout shift (loading to content jump)
@@ -0,0 +1,201 @@
1
+ # Bundle Size Optimization
2
+
3
+ Reduce JavaScript shipped to clients -- directly impacts load time and interactivity.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Avoid Barrel File Imports](#avoid-barrel-file-imports)
8
+ - [Conditional Module Loading](#conditional-module-loading)
9
+ - [Defer Non-Critical Third-Party Libraries](#defer-non-critical-third-party-libraries)
10
+ - [Dynamic Imports for Heavy Components](#dynamic-imports-for-heavy-components)
11
+ - [Preload Based on User Intent](#preload-based-on-user-intent)
12
+
13
+ ---
14
+
15
+ ## Avoid Barrel File Imports
16
+
17
+ Import directly from source files instead of barrel files to avoid loading thousands of unused modules. Barrel files re-export multiple modules (e.g., `index.js` that does `export * from './module'`).
18
+
19
+ Popular libraries can have **up to 10,000 re-exports**. For many React packages, **importing takes 200-800ms**, affecting dev speed and production cold starts. Tree-shaking doesn't help when a library is marked as external.
20
+
21
+ **Incorrect (imports entire library):**
22
+
23
+ ```tsx
24
+ import { Check, X, Menu } from "lucide-react";
25
+ // Loads 1,583 modules, takes ~2.8s extra in dev
26
+
27
+ import { Button, TextField } from "@mui/material";
28
+ // Loads 2,225 modules, takes ~4.2s extra in dev
29
+ ```
30
+
31
+ **Correct (imports only what you need):**
32
+
33
+ ```tsx
34
+ import Check from "lucide-react/dist/esm/icons/check";
35
+ import X from "lucide-react/dist/esm/icons/x";
36
+ import Menu from "lucide-react/dist/esm/icons/menu";
37
+
38
+ import Button from "@mui/material/Button";
39
+ import TextField from "@mui/material/TextField";
40
+ ```
41
+
42
+ **Alternative (Next.js 13.5+):**
43
+
44
+ ```js
45
+ // next.config.js
46
+ module.exports = {
47
+ experimental: {
48
+ optimizePackageImports: ["lucide-react", "@mui/material"],
49
+ },
50
+ };
51
+
52
+ // Then keep ergonomic barrel imports -- transformed at build time
53
+ import { Check, X, Menu } from "lucide-react";
54
+ ```
55
+
56
+ Commonly affected: `lucide-react`, `@mui/material`, `@mui/icons-material`, `@tabler/icons-react`, `react-icons`, `@radix-ui/react-*`, `lodash`, `date-fns`.
57
+
58
+ Reference: [How we optimized package imports in Next.js](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js)
59
+
60
+ ---
61
+
62
+ ## Conditional Module Loading
63
+
64
+ Load large data or modules only when a feature is activated.
65
+
66
+ ```tsx
67
+ function AnimationPlayer({
68
+ enabled,
69
+ setEnabled,
70
+ }: {
71
+ enabled: boolean;
72
+ setEnabled: React.Dispatch<React.SetStateAction<boolean>>;
73
+ }) {
74
+ const [frames, setFrames] = useState<Frame[] | null>(null);
75
+
76
+ useEffect(() => {
77
+ if (enabled && !frames && typeof window !== "undefined") {
78
+ import("./animation-frames.js")
79
+ .then((mod) => setFrames(mod.frames))
80
+ .catch(() => setEnabled(false));
81
+ }
82
+ }, [enabled, frames, setEnabled]);
83
+
84
+ if (!frames) return <Skeleton />;
85
+ return <Canvas frames={frames} />;
86
+ }
87
+ ```
88
+
89
+ The `typeof window !== 'undefined'` check prevents bundling this module for SSR, optimizing server bundle size.
90
+
91
+ ---
92
+
93
+ ## Defer Non-Critical Third-Party Libraries
94
+
95
+ Analytics, logging, and error tracking don't block user interaction. Load them after hydration.
96
+
97
+ **Incorrect (blocks initial bundle):**
98
+
99
+ ```tsx
100
+ import { Analytics } from "@vercel/analytics/react";
101
+
102
+ export default function RootLayout({ children }) {
103
+ return (
104
+ <html>
105
+ <body>
106
+ {children}
107
+ <Analytics />
108
+ </body>
109
+ </html>
110
+ );
111
+ }
112
+ ```
113
+
114
+ **Correct (loads after hydration):**
115
+
116
+ ```tsx
117
+ import dynamic from "next/dynamic";
118
+
119
+ const Analytics = dynamic(() => import("@vercel/analytics/react").then((m) => m.Analytics), {
120
+ ssr: false,
121
+ });
122
+
123
+ export default function RootLayout({ children }) {
124
+ return (
125
+ <html>
126
+ <body>
127
+ {children}
128
+ <Analytics />
129
+ </body>
130
+ </html>
131
+ );
132
+ }
133
+ ```
134
+
135
+ ---
136
+
137
+ ## Dynamic Imports for Heavy Components
138
+
139
+ Use `next/dynamic` to lazy-load large components not needed on initial render.
140
+
141
+ **Incorrect (Monaco bundles with main chunk ~300KB):**
142
+
143
+ ```tsx
144
+ import { MonacoEditor } from "./monaco-editor";
145
+
146
+ function CodePanel({ code }: { code: string }) {
147
+ return <MonacoEditor value={code} />;
148
+ }
149
+ ```
150
+
151
+ **Correct (Monaco loads on demand):**
152
+
153
+ ```tsx
154
+ import dynamic from "next/dynamic";
155
+
156
+ const MonacoEditor = dynamic(() => import("./monaco-editor").then((m) => m.MonacoEditor), {
157
+ ssr: false,
158
+ });
159
+
160
+ function CodePanel({ code }: { code: string }) {
161
+ return <MonacoEditor value={code} />;
162
+ }
163
+ ```
164
+
165
+ ---
166
+
167
+ ## Preload Based on User Intent
168
+
169
+ Preload heavy bundles before they're needed to reduce perceived latency.
170
+
171
+ **Preload on hover/focus:**
172
+
173
+ ```tsx
174
+ function EditorButton({ onClick }: { onClick: () => void }) {
175
+ const preload = () => {
176
+ if (typeof window !== "undefined") {
177
+ void import("./monaco-editor");
178
+ }
179
+ };
180
+
181
+ return (
182
+ <button onMouseEnter={preload} onFocus={preload} onClick={onClick}>
183
+ Open Editor
184
+ </button>
185
+ );
186
+ }
187
+ ```
188
+
189
+ **Preload when feature flag is enabled:**
190
+
191
+ ```tsx
192
+ function FlagsProvider({ children, flags }: Props) {
193
+ useEffect(() => {
194
+ if (flags.editorEnabled && typeof window !== "undefined") {
195
+ void import("./monaco-editor").then((mod) => mod.init());
196
+ }
197
+ }, [flags.editorEnabled]);
198
+
199
+ return <FlagsContext.Provider value={flags}>{children}</FlagsContext.Provider>;
200
+ }
201
+ ```
@@ -0,0 +1,178 @@
1
+ # Client-Side Patterns
2
+
3
+ Optimize client-side data fetching, event handling, and storage.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Deduplicate Global Event Listeners](#deduplicate-global-event-listeners)
8
+ - [Version and Minimize localStorage Data](#version-and-minimize-localstorage-data)
9
+ - [Use Passive Event Listeners](#use-passive-event-listeners)
10
+ - [Use SWR for Automatic Deduplication](#use-swr-for-automatic-deduplication)
11
+
12
+ ---
13
+
14
+ ## Deduplicate Global Event Listeners
15
+
16
+ Use `useSWRSubscription()` to share global event listeners across component instances.
17
+
18
+ **Incorrect (N instances = N listeners):**
19
+
20
+ ```tsx
21
+ function useKeyboardShortcut(key: string, callback: () => void) {
22
+ useEffect(() => {
23
+ const handler = (e: KeyboardEvent) => {
24
+ if (e.metaKey && e.key === key) callback();
25
+ };
26
+ window.addEventListener("keydown", handler);
27
+ return () => window.removeEventListener("keydown", handler);
28
+ }, [key, callback]);
29
+ }
30
+ ```
31
+
32
+ **Correct (N instances = 1 listener):**
33
+
34
+ ```tsx
35
+ import useSWRSubscription from "swr/subscription";
36
+
37
+ const keyCallbacks = new Map<string, Set<() => void>>();
38
+
39
+ function useKeyboardShortcut(key: string, callback: () => void) {
40
+ useEffect(() => {
41
+ if (!keyCallbacks.has(key)) keyCallbacks.set(key, new Set());
42
+ keyCallbacks.get(key)!.add(callback);
43
+
44
+ return () => {
45
+ const set = keyCallbacks.get(key);
46
+ if (set) {
47
+ set.delete(callback);
48
+ if (set.size === 0) keyCallbacks.delete(key);
49
+ }
50
+ };
51
+ }, [key, callback]);
52
+
53
+ useSWRSubscription("global-keydown", () => {
54
+ const handler = (e: KeyboardEvent) => {
55
+ if (e.metaKey && keyCallbacks.has(e.key)) {
56
+ keyCallbacks.get(e.key)!.forEach((cb) => cb());
57
+ }
58
+ };
59
+ window.addEventListener("keydown", handler);
60
+ return () => window.removeEventListener("keydown", handler);
61
+ });
62
+ }
63
+ ```
64
+
65
+ ---
66
+
67
+ ## Version and Minimize localStorage Data
68
+
69
+ Add version prefix to keys and store only needed fields. Prevents schema conflicts and accidental storage of sensitive data.
70
+
71
+ **Incorrect:**
72
+
73
+ ```typescript
74
+ localStorage.setItem("userConfig", JSON.stringify(fullUserObject));
75
+ ```
76
+
77
+ **Correct:**
78
+
79
+ ```typescript
80
+ const VERSION = "v2";
81
+
82
+ function saveConfig(config: { theme: string; language: string }) {
83
+ try {
84
+ localStorage.setItem(`userConfig:${VERSION}`, JSON.stringify(config));
85
+ } catch {
86
+ // Throws in incognito/private browsing, quota exceeded, or disabled
87
+ }
88
+ }
89
+
90
+ function loadConfig() {
91
+ try {
92
+ const data = localStorage.getItem(`userConfig:${VERSION}`);
93
+ return data ? JSON.parse(data) : null;
94
+ } catch {
95
+ return null;
96
+ }
97
+ }
98
+ ```
99
+
100
+ **Always wrap in try-catch:** `getItem()` and `setItem()` throw in incognito/private browsing (Safari, Firefox), when quota exceeded, or when disabled.
101
+
102
+ Store minimal fields from server responses -- only what the UI needs.
103
+
104
+ ---
105
+
106
+ ## Use Passive Event Listeners
107
+
108
+ Add `{ passive: true }` to touch and wheel event listeners to enable immediate scrolling. Browsers normally wait for listeners to finish to check if `preventDefault()` is called, causing scroll delay.
109
+
110
+ **Incorrect:**
111
+
112
+ ```typescript
113
+ useEffect(() => {
114
+ document.addEventListener("touchstart", handleTouch);
115
+ document.addEventListener("wheel", handleWheel);
116
+ return () => {
117
+ document.removeEventListener("touchstart", handleTouch);
118
+ document.removeEventListener("wheel", handleWheel);
119
+ };
120
+ }, []);
121
+ ```
122
+
123
+ **Correct:**
124
+
125
+ ```typescript
126
+ useEffect(() => {
127
+ document.addEventListener("touchstart", handleTouch, { passive: true });
128
+ document.addEventListener("wheel", handleWheel, { passive: true });
129
+ return () => {
130
+ document.removeEventListener("touchstart", handleTouch);
131
+ document.removeEventListener("wheel", handleWheel);
132
+ };
133
+ }, []);
134
+ ```
135
+
136
+ Use passive when: tracking/analytics, logging, any listener that doesn't call `preventDefault()`. Don't use when implementing custom swipe gestures or custom zoom controls.
137
+
138
+ ---
139
+
140
+ ## Use SWR for Automatic Deduplication
141
+
142
+ SWR enables request deduplication, caching, and revalidation across component instances.
143
+
144
+ **Incorrect (no deduplication, each instance fetches):**
145
+
146
+ ```tsx
147
+ function UserList() {
148
+ const [users, setUsers] = useState([]);
149
+ useEffect(() => {
150
+ fetch("/api/users")
151
+ .then((r) => r.json())
152
+ .then(setUsers);
153
+ }, []);
154
+ }
155
+ ```
156
+
157
+ **Correct (multiple instances share one request):**
158
+
159
+ ```tsx
160
+ import useSWR from "swr";
161
+
162
+ function UserList() {
163
+ const { data: users } = useSWR("/api/users", fetcher);
164
+ }
165
+ ```
166
+
167
+ **For mutations:**
168
+
169
+ ```tsx
170
+ import { useSWRMutation } from "swr/mutation";
171
+
172
+ function UpdateButton() {
173
+ const { trigger } = useSWRMutation("/api/user", updateUser);
174
+ return <button onClick={() => trigger()}>Update</button>;
175
+ }
176
+ ```
177
+
178
+ Reference: [SWR](https://swr.vercel.app)