|
| 1 | +# Production Build & Performance Strategy Groundwork |
| 2 | + |
| 3 | +**Related Issue:** #5531 |
| 4 | +**Purpose:** Investigation and documentation only (no architectural changes) |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 1. Introduction & Understanding |
| 9 | + |
| 10 | +This document provides a foundational analysis of the Music Blocks application's current asset loading strategy. The analysis is based on: |
| 11 | + |
| 12 | +- README.md |
| 13 | +- CONTRIBUTING.md |
| 14 | +- DOCS_MODULE_ARCHITECTURE.md |
| 15 | + |
| 16 | +The goal is to support future performance optimization efforts, especially for: |
| 17 | + |
| 18 | +- Production environments |
| 19 | +- Offline-first behavior |
| 20 | +- Service worker reliability |
| 21 | +- Long-term maintainability |
| 22 | + |
| 23 | +This work strictly follows the constraints outlined in Issue #5531: |
| 24 | + |
| 25 | +- Investigation and documentation only |
| 26 | +- No architectural migrations |
| 27 | +- No replacement of RequireJS / AMD |
| 28 | +- Focus on low-risk incremental improvements |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## 2. Current State: Production Asset Delivery & Runtime |
| 33 | + |
| 34 | +### 2.1 Build and Runtime Model |
| 35 | + |
| 36 | +Music Blocks currently operates without a distinct production build process. |
| 37 | + |
| 38 | +Development and production environments load assets in essentially the same way. |
| 39 | + |
| 40 | +Key observations: |
| 41 | + |
| 42 | +- No bundling step |
| 43 | +- No production minification pipeline |
| 44 | +- Assets served individually |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### 2.2 Application Loading Flow |
| 49 | + |
| 50 | +The application appears to follow a multi-stage bootstrap process: |
| 51 | + |
| 52 | +1. `index.html` loads global scripts and styles. |
| 53 | + - Includes dependencies such as p5.js and jQuery. |
| 54 | + |
| 55 | +2. `js/loader.js` initializes RequireJS configuration. |
| 56 | + |
| 57 | +3. RequireJS resolves AMD modules: |
| 58 | + - Core modules load first (e.g., `activity/activity`) |
| 59 | + - Dependency graph loads asynchronously via individual file requests. |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +### 2.3 Asset Delivery Characteristics |
| 64 | + |
| 65 | +- JavaScript modules are served as many independent files. |
| 66 | +- Files appear largely unminified. |
| 67 | +- High number of network requests during initial load. |
| 68 | + |
| 69 | +Dependency analysis suggests: |
| 70 | + |
| 71 | +- Some modules (e.g., `musicutils.js`) have very high dependency counts. |
| 72 | +- Runtime graph complexity is significant. |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### 2.4 Existing Build Process |
| 77 | + |
| 78 | +Current npm scripts primarily support development workflows: |
| 79 | + |
| 80 | +- `npm run dev` → Local HTTP server |
| 81 | +- `npm run lint` / `npm test` → Quality checks |
| 82 | + |
| 83 | +There is no dedicated web production build pipeline. |
| 84 | + |
| 85 | +The architecture can effectively be described as: |
| 86 | + |
| 87 | +> **"Buildless runtime delivery for web assets."** |
| 88 | +
|
| 89 | +--- |
| 90 | + |
| 91 | +### 2.5 Service Worker Behavior |
| 92 | + |
| 93 | +The service worker (`sw.js`) supports offline functionality. |
| 94 | + |
| 95 | +Implications of current asset model: |
| 96 | + |
| 97 | +- Must cache many individual files. |
| 98 | +- Cache list appears manually maintained. |
| 99 | +- Risk of maintenance errors when files change. |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## 3. Performance & Offline Implications |
| 104 | + |
| 105 | +### 3.1 Network Performance |
| 106 | + |
| 107 | +Challenges include: |
| 108 | + |
| 109 | +- High request overhead due to many individual files. |
| 110 | +- Increased latency during initial load. |
| 111 | +- Larger transfer sizes from unminified assets. |
| 112 | + |
| 113 | +Even with HTTP/2 multiplexing, many requests introduce measurable overhead. |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +### 3.2 Offline Reliability |
| 118 | + |
| 119 | +Potential issues: |
| 120 | + |
| 121 | +- Manual cache manifest increases fragility. |
| 122 | +- Missing entries may break offline startup. |
| 123 | +- Numerous small assets reduce caching efficiency. |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +### 3.3 AMD Constraints |
| 128 | + |
| 129 | +Because RequireJS (AMD) manages module loading: |
| 130 | + |
| 131 | +- Simple concatenation-based bundling will fail. |
| 132 | +- Dependency resolution order must be preserved. |
| 133 | +- Many modern bundlers require architectural changes. |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +## 4. Low-Risk Optimization Strategies |
| 138 | + |
| 139 | +The following are exploratory options aligned with current constraints. |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +### Strategy A — Per-File Minification |
| 144 | + |
| 145 | +#### Approach |
| 146 | + |
| 147 | +- Iterate through `.js` files. |
| 148 | +- Minify using a tool such as `terser`. |
| 149 | +- Output to a `dist/` directory preserving structure. |
| 150 | + |
| 151 | +#### Compatibility |
| 152 | + |
| 153 | +- Fully compatible with AMD modules. |
| 154 | + |
| 155 | +#### Benefits |
| 156 | + |
| 157 | +- Reduced download size. |
| 158 | +- Minimal architectural impact. |
| 159 | + |
| 160 | +#### Downsides |
| 161 | + |
| 162 | +- Does not reduce request count. |
| 163 | + |
| 164 | +#### Risk Level |
| 165 | + |
| 166 | +Low. |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +### Strategy B — Selective Bundling Using r.js (RequireJS Optimizer) |
| 171 | + |
| 172 | +#### Approach |
| 173 | + |
| 174 | +Use RequireJS optimizer to bundle specific stable modules. |
| 175 | + |
| 176 | +Example candidate: |
| 177 | + |
| 178 | +- `js/utils/musicutils.js` and its dependency tree. |
| 179 | + |
| 180 | +#### Compatibility |
| 181 | + |
| 182 | +- Native AMD optimization tool. |
| 183 | + |
| 184 | +#### Benefits |
| 185 | + |
| 186 | +- Reduced number of requests. |
| 187 | +- Faster initial loading. |
| 188 | + |
| 189 | +#### Downsides |
| 190 | + |
| 191 | +- Requires new build configuration. |
| 192 | +- Service worker manifest must be updated accordingly. |
| 193 | + |
| 194 | +#### Risk Level |
| 195 | + |
| 196 | +Medium. |
| 197 | + |
| 198 | +Automation of cache manifest updates would be strongly recommended. |
| 199 | + |
| 200 | +--- |
| 201 | + |
| 202 | +## 5. Summary and Potential Path Forward |
| 203 | + |
| 204 | +Current model introduces performance and maintenance challenges: |
| 205 | + |
| 206 | +- Large number of network requests |
| 207 | +- Lack of minification |
| 208 | +- Fragile offline caching workflow |
| 209 | + |
| 210 | +Potential incremental path: |
| 211 | + |
| 212 | +1. Implement per-file minification (low risk). |
| 213 | +2. Investigate selective AMD-aware bundling via r.js. |
| 214 | +3. Document constraints to inform future v4 architectural planning. |
| 215 | + |
| 216 | +Long-term modernization (e.g., Vite/Webpack) should be evaluated only within future architectural redesign discussions. |
| 217 | + |
| 218 | +--- |
| 219 | + |
| 220 | +## 6. Metrics and Open Questions |
| 221 | + |
| 222 | +### Suggested Baseline Metrics |
| 223 | + |
| 224 | +These can be gathered via browser DevTools: |
| 225 | + |
| 226 | +- Total request count on initial load |
| 227 | +- Total JS download size |
| 228 | +- DOMContentLoaded time |
| 229 | +- Full page load time |
| 230 | + |
| 231 | +--- |
| 232 | + |
| 233 | +### Open Questions |
| 234 | + |
| 235 | +- Is the service worker cache list manually maintained? |
| 236 | +- Would introducing r.js as a devDependency be acceptable? |
| 237 | +- Should this analysis live as a standalone performance documentation file? |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +## Status |
| 242 | + |
| 243 | +Draft groundwork document intended to support discussion and future planning. |
0 commit comments