@hitch42/applet 0.1.0-beta.9 → 0.1.1-beta.10
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.
- package/dist/_internal/client.js +120 -11
- package/dist/_internal/log-context.d.ts +34 -0
- package/dist/_internal/log-context.js +2 -0
- package/dist/_internal/server.js +112 -57
- package/dist/applet.js +12 -12
- package/dist/build-Bh_S5BUv.js +2 -0
- package/dist/{build-CAb26kMY.js → build-CEfFoIZZ.js} +1 -1
- package/dist/{ci-5qWl7lT3.js → ci-BKw14X2h.js} +14 -13
- package/dist/client.d.ts +2 -2
- package/dist/{contract-rP_eV9PK.d.ts → contract-CsUpTBe0.d.ts} +1 -1
- package/dist/{deploy-D_OFJ6-Q.js → deploy-wGYjwXrx.js} +139 -88
- package/dist/{generate-CBPnHoqX.js → generate-BEIYs17x.js} +54 -22
- package/dist/generate-Dox-OYt6.js +2 -0
- package/dist/headers-DkNr9MLJ.js +17 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/{init-tfzwuYnk.js → init-6oGPM46R.js} +10 -15
- package/dist/{load-config-BYSxDVAl.js → load-config-xEdTXs0y.js} +2 -2
- package/dist/log-context-zu8U9Sjm.js +73 -0
- package/dist/{login-C7RKmmTe.js → login-CUx3TmFQ.js} +1 -1
- package/dist/{logs-DgjgAxj2.js → logs-DSjuEN0Q.js} +20 -22
- package/dist/{manifest-DD_SGkS6.d.ts → manifest-C6_F4bL8.d.ts} +5 -1
- package/dist/{manifest-Bp87Uz0a.js → manifest-CJSL9GuC.js} +20 -4
- package/dist/{project-D6XHllmC.js → project-BRZGd6xq.js} +197 -16
- package/dist/query-cache-CXmAyLv3.js +82 -0
- package/dist/react.d.ts +2 -1
- package/dist/react.js +59 -39
- package/dist/{schedules-dhU1yyFr.js → schedules-C8_Svr4h.js} +1 -1
- package/dist/server.d.ts +8 -3
- package/dist/server.js +29 -8
- package/dist/telephony.d.ts +24 -4
- package/dist/telephony.js +135 -16
- package/dist/{variables-Bwnr7oxf.js → variables-DT5RyHdH.js} +13 -13
- package/dist/vite.d.ts +3 -60
- package/dist/vite.js +50 -15
- package/dist/{workflow-engine-MogejNfF.js → workflow-engine-BOTzURKM.js} +1 -7
- package/dist/{workspace-XVtM12ZC.js → workspace-D0dnnlxL.js} +33 -38
- package/package.json +13 -6
- package/scaffold/.agents/skills/hitch-applets/SKILL.md +29 -3
- package/scaffold/.agents/skills/hitch-applets/references/manifest.md +8 -0
- package/scaffold/.agents/skills/hitch-applets/references/markdown.md +2 -0
- package/scaffold/.agents/skills/hitch-applets/references/records.md +2 -0
- package/scaffold/.agents/skills/hitch-data/SKILL.md +2 -1
- package/scaffold/.agents/skills/hitch-data/references/attributes.md +7 -1
- package/scaffold/.agents/skills/hitch-data/references/limits.md +13 -13
- package/scaffold/.agents/skills/hitch-theory/SKILL.md +3 -2
- package/scaffold/.agents/skills/react-best-practices/SKILL.md +127 -0
- package/scaffold/.agents/skills/react-best-practices/rules/advanced-effect-event-deps.md +50 -0
- package/scaffold/.agents/skills/react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/scaffold/.agents/skills/react-best-practices/rules/advanced-init-once.md +42 -0
- package/scaffold/.agents/skills/react-best-practices/rules/advanced-use-latest.md +39 -0
- package/scaffold/.agents/skills/react-best-practices/rules/async-cheap-condition-before-await.md +37 -0
- package/scaffold/.agents/skills/react-best-practices/rules/async-defer-await.md +82 -0
- package/scaffold/.agents/skills/react-best-practices/rules/async-dependencies.md +48 -0
- package/scaffold/.agents/skills/react-best-practices/rules/async-parallel.md +24 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-analyzable-paths.md +42 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-barrel-imports.md +39 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-conditional.md +35 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-defer-third-party.md +46 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-dynamic-imports.md +38 -0
- package/scaffold/.agents/skills/react-best-practices/rules/bundle-preload.md +40 -0
- package/scaffold/.agents/skills/react-best-practices/rules/client-event-listeners.md +78 -0
- package/scaffold/.agents/skills/react-best-practices/rules/client-localstorage-schema.md +74 -0
- package/scaffold/.agents/skills/react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-batch-dom-css.md +110 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-cache-function-results.md +58 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-cache-property-access.md +28 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-cache-storage.md +56 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-combine-iterations.md +32 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-early-exit.md +50 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-flatmap-filter.md +51 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-index-maps.md +37 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-length-check-first.md +50 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-min-max-loop.md +82 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-request-idle-callback.md +104 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/scaffold/.agents/skills/react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-activity.md +26 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-animate-svg-wrapper.md +38 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-conditional-render.md +32 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-hoist-jsx.md +36 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-resource-hints.md +81 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-script-defer-async.md +36 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rendering-usetransition-loading.md +43 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-dependencies.md +45 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-derived-state-no-effect.md +40 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-derived-state.md +29 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-functional-setstate.md +77 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-lazy-state-init.md +56 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-memo-with-default-value.md +36 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-memo.md +44 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-move-effect-to-event.md +45 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-no-inline-components.md +75 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-simple-expression-in-memo.md +35 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-split-combined-hooks.md +64 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-transitions.md +40 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-use-deferred-value.md +59 -0
- package/scaffold/.agents/skills/react-best-practices/rules/rerender-use-ref-transient-values.md +73 -0
- package/scaffold/.agents/skills/tanstack-query/SKILL.md +136 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/automatic-invalidation-after-mutations.md +124 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/broadcast-realtime-and-multi-tab-synchronization.md +150 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/compose-query-with-tanstack-router-and-start.md +248 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/enforce-query-best-practices-with-eslint.md +167 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/persist-offline-and-restore-caches.md +181 -0
- package/scaffold/.agents/skills/tanstack-query/references/compositions/query-data-and-forms.md +156 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/build-query-abstractions.md +130 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/cancel-queries-and-consume-abort-signals.md +176 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/concurrent-optimistic-updates.md +135 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/coordinate-query-execution.md +188 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/design-query-keys-and-options.md +182 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/fetch-and-observe-queries.md +193 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/implement-optimistic-updates-and-cache-writes.md +168 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/paginate-and-build-infinite-queries.md +188 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/seed-placeholder-select-and-transform-data.md +188 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/selectors-and-derived-state.md +145 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/tune-defaults-freshness-retries-and-refetching.md +172 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/understand-query-internals-and-observers.md +104 -0
- package/scaffold/.agents/skills/tanstack-query/references/core/write-mutations-and-invalidate-related-queries.md +183 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/debug-with-devtools.md +169 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/handle-status-and-errors.md +119 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/shape-data-and-render-efficiently.md +184 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/use-framework-adapter-reactivity.md +185 -0
- package/scaffold/.agents/skills/tanstack-query/references/framework/use-suspense-and-error-boundaries.md +208 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/migrate-major-versions-and-codemods.md +169 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/prefetch-and-remove-request-waterfalls.md +228 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/setup-query-client-and-providers.md +161 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/ssr-hydration-and-streaming.md +209 -0
- package/scaffold/.agents/skills/tanstack-query/references/lifecycle/test-query-code.md +160 -0
- package/scaffold/.agents/skills/tanstack-router/SKILL.md +134 -0
- package/scaffold/.agents/skills/tanstack-router/references/compositions/router-query.md +412 -0
- package/scaffold/.agents/skills/tanstack-router/references/react-router.md +495 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/auth-and-guards.md +495 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/code-splitting.md +318 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/data-loading.md +490 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/index.md +158 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/navigation.md +433 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/not-found-and-errors.md +436 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/path-params.md +383 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/search-params.md +343 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/ssr.md +462 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-core/type-safety.md +493 -0
- package/scaffold/.agents/skills/tanstack-router/references/router-plugin.md +250 -0
- package/scaffold/.agents/skills/tanstack-router/references/virtual-file-routes.md +215 -0
- package/scaffold/.agents/skills/web-design-guidelines/SKILL.md +184 -0
- package/scaffold/.github/workflows/hitch.yml +1 -1
- package/scaffold/AGENTS.md +2 -2
- package/scaffold/applet/src/client/components/ui/alert.tsx +1 -1
- package/scaffold/applet/src/client/components/ui/button.tsx +1 -1
- package/scaffold/applet/src/client/components/ui/checkbox.tsx +1 -1
- package/scaffold/applet/src/client/components/ui/dialog.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/dropdown-menu.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/input-group.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/select.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/sheet.tsx +2 -2
- package/scaffold/applet/src/client/components/ui/spinner.tsx +2 -1
- package/scaffold/applet/src/client/components/ui/tabs.tsx +1 -1
- package/scaffold/applet/src/client/lib/utils.ts +1 -6
- package/scaffold/applet/src/client/styles/globals.css +2 -0
- package/dist/build-BfbTbt_r.js +0 -2
- package/dist/generate-BgepN4-b.js +0 -2
|
@@ -24,8 +24,9 @@ An organization's data lives in Hitch objects. Each object has attributes. Each
|
|
|
24
24
|
## Attributes
|
|
25
25
|
|
|
26
26
|
- An attribute is schema, not a free JSON field. Choose a type. Hitch validates the value against the type and its configuration.
|
|
27
|
-
- Types: text, rich_text, integer, decimal, currency, checkbox, date, datetime, time, domain, url, email, phone, select, reference, user, attachment.
|
|
27
|
+
- Types: text, rich_text, integer, decimal, currency, checkbox, date, datetime, time, domain, url, email, phone, select, reference, user, organization, attachment.
|
|
28
28
|
- A value is a non-empty value or `null`. No type accepts an empty string.
|
|
29
|
+
- An organisation attribute points at a connected organisation; null means internal only; at most one exists per object.
|
|
29
30
|
- Cardinality is configuration. Text, select, reference, user, and attachment can be multi-value.
|
|
30
31
|
- A required attribute rejects `null` and rejects an empty array.
|
|
31
32
|
- Store a fixed set of values as a select, not as text. Store a link to another record as a reference, not as text or as an id in a text field. Text cannot be a group dimension, and text matching needs the `indexed` flag.
|
|
@@ -13,7 +13,7 @@ Hitch validates and normalizes every attribute value on write. Read this before
|
|
|
13
13
|
- An `immutable` attribute rejects a change once it holds a non-null value. A null immutable attribute can still be set once.
|
|
14
14
|
- A `unique` attribute rejects a duplicate value with HTTP 409. Unique is never allowed on a multi-value attribute.
|
|
15
15
|
- Every owner attribute key is present on read. An empty attribute reads as `null`. An inverse reference face has no key.
|
|
16
|
-
- Every attribute accepts the `$eq` and `$not_empty` filters. Every attribute except checkbox accepts `$in`. Other operators and `sort` need the `indexed` flag. Read `limits.md`.
|
|
16
|
+
- Every attribute accepts the `$eq` and `$not_empty` filters. Every attribute except checkbox and organization accepts `$in`. Other operators and `sort` need the `indexed` flag. Read `limits.md`.
|
|
17
17
|
|
|
18
18
|
## Text and rich text
|
|
19
19
|
|
|
@@ -91,6 +91,12 @@ Hitch validates and normalizes every attribute value on write. Read this before
|
|
|
91
91
|
- Read: the UUID. With `include`, the expansion is `{ id, firstName, lastName, email }`.
|
|
92
92
|
- Filters: identity operators only. Sort: never.
|
|
93
93
|
|
|
94
|
+
## Organisation
|
|
95
|
+
|
|
96
|
+
- An organisation attribute points at a connected organisation; null means internal only; at most one exists per object.
|
|
97
|
+
- Write an accepted peer organization UUID. The value stays visible after the connection ends.
|
|
98
|
+
- With `include`, the expansion is `{ id, name }`. Filters are `$eq` and `$not_empty` only.
|
|
99
|
+
|
|
94
100
|
## Attachment
|
|
95
101
|
|
|
96
102
|
- Write the file id from a completed upload. An unknown id fails with `file not found`. A file with no uploaded bytes fails with `file has not arrived`. Read `../../hitch-applets/references/files.md`.
|
|
@@ -22,19 +22,19 @@ A page is the unit of a read. A screen shows one page and asks for the next one.
|
|
|
22
22
|
|
|
23
23
|
An aggregate answers a question about many records with a small result. The caps keep the result small enough to render. A question that needs more cells is two questions.
|
|
24
24
|
|
|
25
|
-
| Limit | Value | Past the limit
|
|
26
|
-
| --------------------------------------- | ------------------------------------------------------------ |
|
|
27
|
-
| Aggregates per call | 20 | HTTP 400
|
|
28
|
-
| Group dimensions | 2 | HTTP 400
|
|
29
|
-
| Multi-value group dimensions | 1 | HTTP 400
|
|
30
|
-
| Distinct keys per dimension, default | 50 |
|
|
31
|
-
| Distinct keys per dimension, max | 200 |
|
|
32
|
-
| Cells, the product of dimension limits | 2500 | HTTP 400
|
|
33
|
-
| Reference steps in an aggregate path | 1 | HTTP 400
|
|
34
|
-
| Sum, average, min, max on a multi-value | Not allowed | HTTP 400
|
|
35
|
-
| Group dimension type | checkbox, select, reference, user, bucketed date or datetime | HTTP 400
|
|
36
|
-
| Aggregate alias `as` | 64 characters | HTTP 400
|
|
37
|
-
| Sum, average, min, max on a non-numeric | Not allowed | HTTP 400
|
|
25
|
+
| Limit | Value | Past the limit |
|
|
26
|
+
| --------------------------------------- | ------------------------------------------------------------ | --------------------------------------- |
|
|
27
|
+
| Aggregates per call | 20 | HTTP 400 |
|
|
28
|
+
| Group dimensions | 2 | HTTP 400 |
|
|
29
|
+
| Multi-value group dimensions | 1 | HTTP 400 |
|
|
30
|
+
| Distinct keys per dimension, default | 50 | Extra groups omitted, `truncated: true` |
|
|
31
|
+
| Distinct keys per dimension, max | 200 | HTTP 400 |
|
|
32
|
+
| Cells, the product of dimension limits | 2500 | HTTP 400 |
|
|
33
|
+
| Reference steps in an aggregate path | 1 | HTTP 400 |
|
|
34
|
+
| Sum, average, min, max on a multi-value | Not allowed | HTTP 400 |
|
|
35
|
+
| Group dimension type | checkbox, select, reference, user, bucketed date or datetime | HTTP 400 |
|
|
36
|
+
| Aggregate alias `as` | 64 characters | HTTP 400 |
|
|
37
|
+
| Sum, average, min, max on a non-numeric | Not allowed | HTTP 400 |
|
|
38
38
|
|
|
39
39
|
## Writes
|
|
40
40
|
|
|
@@ -45,7 +45,7 @@ Applets can be used standalone via their `.app` URL or within the Hitch shell. S
|
|
|
45
45
|
|
|
46
46
|
The organization's Hitch data is the single source of truth. An applet reads and writes that data. It does not keep its own copy.
|
|
47
47
|
|
|
48
|
-
The data layer stands on its own so that every way of using it is a client of the same thing. An applet, the `hitch` command line, an MCP server for an AI agent, an API key in a script, and an import all speak to the same objects, the same schema, and the same permissions. None of them is a bridge bolted on later.
|
|
48
|
+
The data layer stands on its own so that every way of using it is a client of the same thing. An applet, the `hitch` command line, an MCP server for an AI agent, an API key in a script, and an import all speak to the same objects, the same schema, and the same permissions. None of them is a bridge bolted on later. The CLI and applet doors are generated from the organization's own schema. The MCP door has a fixed set of record tools and reads each object's live schema at runtime with `get_object_schema`. Each one acts as the person or applet behind it, so it sees exactly what that caller may see. The result is that a person in a browser, an agent in a chat window, and a script in a terminal all work on the same records under the same rules, and each gets the interface that suits them. Software stops being one product with one screen and becomes your organizational data with many doors.
|
|
49
49
|
|
|
50
50
|
The boundary gives you:
|
|
51
51
|
|
|
@@ -68,7 +68,7 @@ Consequences:
|
|
|
68
68
|
|
|
69
69
|
An attribute is schema, not a free JSON field. Hitch validates each value against its type and configuration, normalizes it, stores it in the record, and indexes it when asked. A value that should be a select or a reference, but is stored as free text, cannot be grouped or filtered well later.
|
|
70
70
|
|
|
71
|
-
- Types: text, rich text, integer, decimal, currency, checkbox, date, datetime, time, domain, url, email, phone, select, reference, user, attachment.
|
|
71
|
+
- Types: text, rich text, integer, decimal, currency, checkbox, date, datetime, time, domain, url, email, phone, select, reference, user, organisation, attachment.
|
|
72
72
|
- Cardinality is configuration, not a type. Text, select, reference, user, and attachment can be multi-value. A multi-value is an ordered array.
|
|
73
73
|
- An empty attribute reads as null. A required multi-value attribute rejects an empty array.
|
|
74
74
|
- Uniqueness is a real constraint, per attribute, per organization.
|
|
@@ -83,6 +83,7 @@ References model relationships:
|
|
|
83
83
|
Special cases:
|
|
84
84
|
|
|
85
85
|
- A user attribute points at a membership in the same organization. Removal of the member sweeps those values.
|
|
86
|
+
- An organisation attribute points at a connected organisation; null means internal only; at most one exists per object.
|
|
86
87
|
- An attachment attribute holds file IDs. Files live outside records, in their own storage. Create the file, upload it, then store the ID on the record.
|
|
87
88
|
|
|
88
89
|
## 6. Changesets and releases move together
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: react-best-practices
|
|
3
|
+
description: Client React performance guidelines. Use when writing, reviewing, or refactoring React components, client data fetching, bundles, rendering, or browser performance.
|
|
4
|
+
license: MIT
|
|
5
|
+
metadata:
|
|
6
|
+
version: "1.0.0"
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# React Best Practices
|
|
10
|
+
|
|
11
|
+
Performance guidance for client React applications. The rules focus on browser behavior, client data fetching, bundles, rendering, and re-renders.
|
|
12
|
+
|
|
13
|
+
## When to Apply
|
|
14
|
+
|
|
15
|
+
Reference these guidelines when:
|
|
16
|
+
|
|
17
|
+
- Writing new React components
|
|
18
|
+
- Implementing client data fetching
|
|
19
|
+
- Reviewing code for performance issues
|
|
20
|
+
- Refactoring existing React code
|
|
21
|
+
- Optimizing bundle size or load times
|
|
22
|
+
|
|
23
|
+
## Rule Categories by Priority
|
|
24
|
+
|
|
25
|
+
| Priority | Category | Impact | Prefix |
|
|
26
|
+
| -------- | ------------------------- | ----------- | ------------ |
|
|
27
|
+
| 1 | Eliminating Waterfalls | CRITICAL | `async-` |
|
|
28
|
+
| 2 | Bundle Size Optimization | CRITICAL | `bundle-` |
|
|
29
|
+
| 3 | Client-Side Data Fetching | MEDIUM-HIGH | `client-` |
|
|
30
|
+
| 4 | Re-render Optimization | MEDIUM | `rerender-` |
|
|
31
|
+
| 5 | Rendering Performance | MEDIUM | `rendering-` |
|
|
32
|
+
| 6 | JavaScript Performance | LOW-MEDIUM | `js-` |
|
|
33
|
+
| 7 | Advanced Patterns | LOW | `advanced-` |
|
|
34
|
+
|
|
35
|
+
## Quick Reference
|
|
36
|
+
|
|
37
|
+
### 1. Eliminating Waterfalls (CRITICAL)
|
|
38
|
+
|
|
39
|
+
- `async-cheap-condition-before-await` - Check cheap sync conditions before awaiting flags or remote values
|
|
40
|
+
- `async-defer-await` - Move await into branches where actually used
|
|
41
|
+
- `async-parallel` - Use Promise.all() for independent operations
|
|
42
|
+
- `async-dependencies` - Use better-all for partial dependencies
|
|
43
|
+
|
|
44
|
+
### 2. Bundle Size Optimization (CRITICAL)
|
|
45
|
+
|
|
46
|
+
- `bundle-barrel-imports` - Import directly, avoid barrel files
|
|
47
|
+
- `bundle-analyzable-paths` - Prefer statically analyzable imports
|
|
48
|
+
- `bundle-dynamic-imports` - Use React.lazy for heavy components
|
|
49
|
+
- `bundle-defer-third-party` - Split non-critical libraries from the initial bundle
|
|
50
|
+
- `bundle-conditional` - Load modules only when feature is activated
|
|
51
|
+
- `bundle-preload` - Preload on hover/focus for perceived speed
|
|
52
|
+
|
|
53
|
+
### 3. Client-Side Data Fetching (MEDIUM-HIGH)
|
|
54
|
+
|
|
55
|
+
- `client-event-listeners` - Deduplicate global event listeners
|
|
56
|
+
- `client-passive-event-listeners` - Use passive listeners for scroll
|
|
57
|
+
- `client-localstorage-schema` - Version and minimize localStorage data
|
|
58
|
+
|
|
59
|
+
### 4. Re-render Optimization (MEDIUM)
|
|
60
|
+
|
|
61
|
+
- `rerender-defer-reads` - Don't subscribe to state only used in callbacks
|
|
62
|
+
- `rerender-memo` - Extract expensive work into memoized components
|
|
63
|
+
- `rerender-memo-with-default-value` - Hoist default non-primitive props
|
|
64
|
+
- `rerender-dependencies` - Use primitive dependencies in effects
|
|
65
|
+
- `rerender-derived-state` - Subscribe to derived booleans, not raw values
|
|
66
|
+
- `rerender-derived-state-no-effect` - Derive state during render, not effects
|
|
67
|
+
- `rerender-functional-setstate` - Use functional setState for stable callbacks
|
|
68
|
+
- `rerender-lazy-state-init` - Pass function to useState for expensive values
|
|
69
|
+
- `rerender-simple-expression-in-memo` - Avoid memo for simple primitives
|
|
70
|
+
- `rerender-split-combined-hooks` - Split hooks with independent dependencies
|
|
71
|
+
- `rerender-move-effect-to-event` - Put interaction logic in event handlers
|
|
72
|
+
- `rerender-transitions` - Use startTransition for non-urgent updates
|
|
73
|
+
- `rerender-use-deferred-value` - Defer expensive renders to keep input responsive
|
|
74
|
+
- `rerender-use-ref-transient-values` - Use refs for transient frequent values
|
|
75
|
+
- `rerender-no-inline-components` - Don't define components inside components
|
|
76
|
+
|
|
77
|
+
### 5. Rendering Performance (MEDIUM)
|
|
78
|
+
|
|
79
|
+
- `rendering-animate-svg-wrapper` - Animate div wrapper, not SVG element
|
|
80
|
+
- `rendering-content-visibility` - Use content-visibility for long lists
|
|
81
|
+
- `rendering-hoist-jsx` - Extract static JSX outside components
|
|
82
|
+
- `rendering-svg-precision` - Reduce SVG coordinate precision
|
|
83
|
+
- `rendering-activity` - Use Activity component for show/hide
|
|
84
|
+
- `rendering-conditional-render` - Use ternary, not && for conditionals
|
|
85
|
+
- `rendering-usetransition-loading` - Track async action pending state with useTransition
|
|
86
|
+
- `rendering-resource-hints` - Use React DOM resource hints for preloading
|
|
87
|
+
- `rendering-script-defer-async` - Use defer or async on script tags
|
|
88
|
+
|
|
89
|
+
### 6. JavaScript Performance (LOW-MEDIUM)
|
|
90
|
+
|
|
91
|
+
- `js-batch-dom-css` - Group CSS changes via classes or cssText
|
|
92
|
+
- `js-index-maps` - Build Map for repeated lookups
|
|
93
|
+
- `js-cache-property-access` - Cache object properties in loops
|
|
94
|
+
- `js-cache-function-results` - Cache function results in module-level Map
|
|
95
|
+
- `js-cache-storage` - Cache localStorage/sessionStorage reads
|
|
96
|
+
- `js-combine-iterations` - Combine multiple filter/map into one loop
|
|
97
|
+
- `js-length-check-first` - Check array length before expensive comparison
|
|
98
|
+
- `js-early-exit` - Return early from functions
|
|
99
|
+
- `js-hoist-regexp` - Hoist RegExp creation outside loops
|
|
100
|
+
- `js-min-max-loop` - Use loop for min/max instead of sort
|
|
101
|
+
- `js-set-map-lookups` - Use Set/Map for O(1) lookups
|
|
102
|
+
- `js-tosorted-immutable` - Use toSorted() for immutability
|
|
103
|
+
- `js-flatmap-filter` - Use flatMap to map and filter in one pass
|
|
104
|
+
- `js-request-idle-callback` - Defer non-critical work to browser idle time
|
|
105
|
+
|
|
106
|
+
### 7. Advanced Patterns (LOW)
|
|
107
|
+
|
|
108
|
+
- `advanced-effect-event-deps` - Don't put `useEffectEvent` results in effect deps
|
|
109
|
+
- `advanced-event-handler-refs` - Store event handlers in refs
|
|
110
|
+
- `advanced-init-once` - Initialize app once per app load
|
|
111
|
+
- `advanced-use-latest` - useLatest for stable callback refs
|
|
112
|
+
|
|
113
|
+
## How to Use
|
|
114
|
+
|
|
115
|
+
Read individual rule files for detailed explanations and code examples:
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
rules/async-parallel.md
|
|
119
|
+
rules/bundle-barrel-imports.md
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Each rule file contains:
|
|
123
|
+
|
|
124
|
+
- Brief explanation of why it matters
|
|
125
|
+
- Incorrect code example with explanation
|
|
126
|
+
- Correct code example with explanation
|
|
127
|
+
- Additional context and references
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Do Not Put Effect Events in Dependency Arrays
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: avoids unnecessary effect re-runs and lint errors
|
|
5
|
+
tags: advanced, hooks, useEffectEvent, dependencies, effects
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Do Not Put Effect Events in Dependency Arrays
|
|
9
|
+
|
|
10
|
+
Effect Event functions do not have a stable identity. Their identity intentionally changes on every render. Do not include the function returned by `useEffectEvent` in a `useEffect` dependency array. Keep the actual reactive values as dependencies and call the Effect Event from inside the effect body or subscriptions created by that effect.
|
|
11
|
+
|
|
12
|
+
**Incorrect (Effect Event added as a dependency):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { useEffect, useEffectEvent } from "react";
|
|
16
|
+
|
|
17
|
+
function ChatRoom({ roomId, onConnected }: { roomId: string; onConnected: () => void }) {
|
|
18
|
+
const handleConnected = useEffectEvent(onConnected);
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const connection = createConnection(roomId);
|
|
22
|
+
connection.on("connected", handleConnected);
|
|
23
|
+
connection.connect();
|
|
24
|
+
|
|
25
|
+
return () => connection.disconnect();
|
|
26
|
+
}, [roomId, handleConnected]);
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Including the Effect Event in dependencies makes the effect re-run every render and triggers the React Hooks lint rule.
|
|
31
|
+
|
|
32
|
+
**Correct (depend on reactive values, not the Effect Event):**
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import { useEffect, useEffectEvent } from "react";
|
|
36
|
+
|
|
37
|
+
function ChatRoom({ roomId, onConnected }: { roomId: string; onConnected: () => void }) {
|
|
38
|
+
const handleConnected = useEffectEvent(onConnected);
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const connection = createConnection(roomId);
|
|
42
|
+
connection.on("connected", handleConnected);
|
|
43
|
+
connection.connect();
|
|
44
|
+
|
|
45
|
+
return () => connection.disconnect();
|
|
46
|
+
}, [roomId]);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Reference: [React useEffectEvent: Effect Event in deps](https://react.dev/reference/react/useEffectEvent#effect-event-in-deps)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Store Event Handlers in Refs
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: stable subscriptions
|
|
5
|
+
tags: advanced, hooks, refs, event-handlers, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Store Event Handlers in Refs
|
|
9
|
+
|
|
10
|
+
Store callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.
|
|
11
|
+
|
|
12
|
+
**Incorrect (re-subscribes on every render):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
window.addEventListener(event, handler);
|
|
18
|
+
return () => window.removeEventListener(event, handler);
|
|
19
|
+
}, [event, handler]);
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Correct (stable subscription):**
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
27
|
+
const handlerRef = useRef(handler);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
handlerRef.current = handler;
|
|
30
|
+
}, [handler]);
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const listener = (e) => handlerRef.current(e);
|
|
34
|
+
window.addEventListener(event, listener);
|
|
35
|
+
return () => window.removeEventListener(event, listener);
|
|
36
|
+
}, [event]);
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Alternative: use `useEffectEvent` if you're on latest React:**
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { useEffectEvent } from "react";
|
|
44
|
+
|
|
45
|
+
function useWindowEvent(event: string, handler: (e) => void) {
|
|
46
|
+
const onEvent = useEffectEvent(handler);
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
window.addEventListener(event, onEvent);
|
|
50
|
+
return () => window.removeEventListener(event, onEvent);
|
|
51
|
+
}, [event]);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`useEffectEvent` reads the latest handler without re-subscribing. The Effect Event function does not have a stable identity.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Initialize App Once, Not Per Mount
|
|
3
|
+
impact: LOW-MEDIUM
|
|
4
|
+
impactDescription: avoids duplicate init in development
|
|
5
|
+
tags: initialization, useEffect, app-startup, side-effects
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Initialize App Once, Not Per Mount
|
|
9
|
+
|
|
10
|
+
Do not put app-wide initialization that must run once per app load inside `useEffect([])` of a component. Components can remount and effects will re-run. Use a module-level guard or top-level init in the entry module instead.
|
|
11
|
+
|
|
12
|
+
**Incorrect (runs twice in dev, re-runs on remount):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function Comp() {
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
loadFromStorage();
|
|
18
|
+
checkAuthToken();
|
|
19
|
+
}, []);
|
|
20
|
+
|
|
21
|
+
// ...
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (once per app load):**
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
let didInit = false;
|
|
29
|
+
|
|
30
|
+
function Comp() {
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (didInit) return;
|
|
33
|
+
didInit = true;
|
|
34
|
+
loadFromStorage();
|
|
35
|
+
checkAuthToken();
|
|
36
|
+
}, []);
|
|
37
|
+
|
|
38
|
+
// ...
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Reference: [Initializing the application](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: useEffectEvent for Stable Callback Refs
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: prevents effect re-runs
|
|
5
|
+
tags: advanced, hooks, useEffectEvent, refs, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## useEffectEvent for Stable Callback Refs
|
|
9
|
+
|
|
10
|
+
Access latest values in callbacks without adding them to dependency arrays. Prevents effect re-runs while avoiding stale closures.
|
|
11
|
+
|
|
12
|
+
**Incorrect (effect re-runs on every callback change):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
|
16
|
+
const [query, setQuery] = useState("");
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const timeout = setTimeout(() => onSearch(query), 300);
|
|
20
|
+
return () => clearTimeout(timeout);
|
|
21
|
+
}, [query, onSearch]);
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (using React's useEffectEvent):**
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { useEffectEvent } from "react";
|
|
29
|
+
|
|
30
|
+
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
|
31
|
+
const [query, setQuery] = useState("");
|
|
32
|
+
const onSearchEvent = useEffectEvent(onSearch);
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
const timeout = setTimeout(() => onSearchEvent(query), 300);
|
|
36
|
+
return () => clearTimeout(timeout);
|
|
37
|
+
}, [query]);
|
|
38
|
+
}
|
|
39
|
+
```
|
package/scaffold/.agents/skills/react-best-practices/rules/async-cheap-condition-before-await.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Check Cheap Conditions Before Async Flags
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: avoids unnecessary async work when a synchronous guard already fails
|
|
5
|
+
tags: async, await, feature-flags, short-circuit, conditional
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Check Cheap Conditions Before Async Flags
|
|
9
|
+
|
|
10
|
+
When a branch uses `await` for a flag or remote value and also requires a **cheap synchronous** condition (local props, request metadata, already-loaded state), evaluate the cheap condition **first**. Otherwise you pay for the async call even when the compound condition can never be true.
|
|
11
|
+
|
|
12
|
+
This is a specialization of [Defer Await Until Needed](./async-defer-await.md) for `flag && cheapCondition` style checks.
|
|
13
|
+
|
|
14
|
+
**Incorrect:**
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
const someFlag = await getFlag();
|
|
18
|
+
|
|
19
|
+
if (someFlag && someCondition) {
|
|
20
|
+
// ...
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Correct:**
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
if (someCondition) {
|
|
28
|
+
const someFlag = await getFlag();
|
|
29
|
+
if (someFlag) {
|
|
30
|
+
// ...
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
This matters when `getFlag` hits the network, a feature-flag service, or `React.cache` / DB work: skipping it when `someCondition` is false removes that cost on the cold path.
|
|
36
|
+
|
|
37
|
+
Keep the original order if `someCondition` is expensive, depends on the flag, or you must run side effects in a fixed order.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Defer Await Until Needed
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: avoids blocking unused code paths
|
|
5
|
+
tags: async, await, conditional, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Defer Await Until Needed
|
|
9
|
+
|
|
10
|
+
Move `await` operations into the branches where they're actually used to avoid blocking code paths that don't need them.
|
|
11
|
+
|
|
12
|
+
**Incorrect (blocks both branches):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
async function handleRequest(userId: string, skipProcessing: boolean) {
|
|
16
|
+
const userData = await fetchUserData(userId);
|
|
17
|
+
|
|
18
|
+
if (skipProcessing) {
|
|
19
|
+
// Returns immediately but still waited for userData
|
|
20
|
+
return { skipped: true };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Only this branch uses userData
|
|
24
|
+
return processUserData(userData);
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Correct (only blocks when needed):**
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
async function handleRequest(userId: string, skipProcessing: boolean) {
|
|
32
|
+
if (skipProcessing) {
|
|
33
|
+
// Returns immediately without waiting
|
|
34
|
+
return { skipped: true };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Fetch only when needed
|
|
38
|
+
const userData = await fetchUserData(userId);
|
|
39
|
+
return processUserData(userData);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Another example (early return optimization):**
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
// Incorrect: always fetches permissions
|
|
47
|
+
async function updateResource(resourceId: string, userId: string) {
|
|
48
|
+
const permissions = await fetchPermissions(userId);
|
|
49
|
+
const resource = await getResource(resourceId);
|
|
50
|
+
|
|
51
|
+
if (!resource) {
|
|
52
|
+
return { error: "Not found" };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!permissions.canEdit) {
|
|
56
|
+
return { error: "Forbidden" };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return await updateResourceData(resource, permissions);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Correct: fetches only when needed
|
|
63
|
+
async function updateResource(resourceId: string, userId: string) {
|
|
64
|
+
const resource = await getResource(resourceId);
|
|
65
|
+
|
|
66
|
+
if (!resource) {
|
|
67
|
+
return { error: "Not found" };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const permissions = await fetchPermissions(userId);
|
|
71
|
+
|
|
72
|
+
if (!permissions.canEdit) {
|
|
73
|
+
return { error: "Forbidden" };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return await updateResourceData(resource, permissions);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This optimization is especially valuable when the skipped branch is frequently taken, or when the deferred operation is expensive.
|
|
81
|
+
|
|
82
|
+
For `await getFlag()` combined with a cheap synchronous guard (`flag && someCondition`), see [Check Cheap Conditions Before Async Flags](./async-cheap-condition-before-await.md).
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Dependency-Based Parallelization
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: 2-10× improvement
|
|
5
|
+
tags: async, parallelization, dependencies, better-all
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Dependency-Based Parallelization
|
|
9
|
+
|
|
10
|
+
For operations with partial dependencies, use `better-all` to maximize parallelism. It automatically starts each task at the earliest possible moment.
|
|
11
|
+
|
|
12
|
+
**Incorrect (profile waits for config unnecessarily):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
const [user, config] = await Promise.all([fetchUser(), fetchConfig()]);
|
|
16
|
+
const profile = await fetchProfile(user.id);
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Correct (config and profile run in parallel):**
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { all } from "better-all";
|
|
23
|
+
|
|
24
|
+
const { user, config, profile } = await all({
|
|
25
|
+
async user() {
|
|
26
|
+
return fetchUser();
|
|
27
|
+
},
|
|
28
|
+
async config() {
|
|
29
|
+
return fetchConfig();
|
|
30
|
+
},
|
|
31
|
+
async profile() {
|
|
32
|
+
return fetchProfile((await this.$.user).id);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**Alternative without extra dependencies:**
|
|
38
|
+
|
|
39
|
+
We can also create all the promises first, and do `Promise.all()` at the end.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const userPromise = fetchUser();
|
|
43
|
+
const profilePromise = userPromise.then((user) => fetchProfile(user.id));
|
|
44
|
+
|
|
45
|
+
const [user, config, profile] = await Promise.all([userPromise, fetchConfig(), profilePromise]);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Reference: [https://github.com/shuding/better-all](https://github.com/shuding/better-all)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Promise.all() for Independent Operations
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: 2-10× improvement
|
|
5
|
+
tags: async, parallelization, promises, waterfalls
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Promise.all() for Independent Operations
|
|
9
|
+
|
|
10
|
+
When async operations have no interdependencies, execute them concurrently using `Promise.all()`.
|
|
11
|
+
|
|
12
|
+
**Incorrect (sequential execution, 3 round trips):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
const user = await fetchUser();
|
|
16
|
+
const posts = await fetchPosts();
|
|
17
|
+
const comments = await fetchComments();
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Correct (parallel execution, 1 round trip):**
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
const [user, posts, comments] = await Promise.all([fetchUser(), fetchPosts(), fetchComments()]);
|
|
24
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Prefer Statically Analyzable Imports
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: avoids accidental broad bundles and file traces
|
|
5
|
+
tags: bundle, vite, webpack, rollup, esbuild, imports
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Prefer Statically Analyzable Imports
|
|
9
|
+
|
|
10
|
+
Build tools work best when import paths are obvious at build time. If you hide the real path inside a variable, the tool can include too many files or fail to split the bundle correctly.
|
|
11
|
+
|
|
12
|
+
Prefer explicit maps or literal paths so the set of reachable modules stays narrow and predictable.
|
|
13
|
+
|
|
14
|
+
When analysis becomes too broad, the cost is real:
|
|
15
|
+
|
|
16
|
+
- Larger browser bundles
|
|
17
|
+
- Slower builds
|
|
18
|
+
- More memory use
|
|
19
|
+
|
|
20
|
+
**Incorrect (the bundler cannot tell what may be imported):**
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
const PAGE_MODULES = {
|
|
24
|
+
home: "./pages/home",
|
|
25
|
+
settings: "./pages/settings",
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
28
|
+
const Page = await import(PAGE_MODULES[pageName]);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**Correct (use an explicit map of allowed modules):**
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const PAGE_MODULES = {
|
|
35
|
+
home: () => import("./pages/home"),
|
|
36
|
+
settings: () => import("./pages/settings"),
|
|
37
|
+
} as const;
|
|
38
|
+
|
|
39
|
+
const Page = await PAGE_MODULES[pageName]();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Reference: [Vite features](https://vite.dev/guide/features.html), [esbuild API](https://esbuild.github.io/api/), [Rollup dynamic import vars](https://www.npmjs.com/package/@rollup/plugin-dynamic-import-vars), [Webpack dependency management](https://webpack.js.org/guides/dependency-management/)
|