35 lines
1.4 KiB
Markdown
35 lines
1.4 KiB
Markdown
# ShopProQuote JS Pitfalls
|
|
|
|
## `const` redeclaration in large single-file scripts
|
|
|
|
`dashboard.js` is a large file (~4000 lines) with many functions sharing the same closure scope. When a `const` variable is declared twice in the same block, it throws a fatal parse error that prevents the ENTIRE script from loading. The browser won't execute any of it.
|
|
|
|
### Real bug — Daily Briefing stuck on "Loading..."
|
|
|
|
In `dashboard.js`, `const briefingText` was declared twice in the same `try` block:
|
|
|
|
```js
|
|
// First declaration (line 3926) — extracting from API response
|
|
const briefingText = dsResult.choices?.[0]?.message?.content?.trim() || '';
|
|
|
|
// ... (HTML rendering) ...
|
|
|
|
// Second declaration (line 3944) — caching
|
|
const briefingText = contentDiv.textContent || '';
|
|
```
|
|
|
|
This parse error stopped `dashboard.js` from loading entirely. The fallback inline script in `index.html` kept polling for `window.generateDailyBriefing` (which was never defined), showing "Loading briefing..." forever.
|
|
|
|
### Fix
|
|
|
|
Rename the second variable unambiguously:
|
|
```js
|
|
const finalBriefingText = contentDiv.textContent || '';
|
|
```
|
|
|
|
### Prevention
|
|
|
|
- When editing a function in a large file, check for variable name collisions in the same function scope.
|
|
- `let` declarations in different blocks can share names safely, but `const` in the same block cannot.
|
|
- There is no build step or linter — these errors only surface at runtime. Test in browser console.
|