diff options
| author | Shu Ding <g@shud.in> | 2026-08-26 22:54:52 +0800 |
|---|---|---|
| committer | Shu Ding <g@shud.in> | 2026-08-26 22:54:52 +0800 |
| commit | 13f6d448f17473afc6869aa51093a5014b234f4a (patch) | |
| tree | e3a72201bf0f67b852a90fbd6084ba765f858dae | |
| parent | dd089a8c752c966dee8bf0f27cb625ba193ffd9e (diff) | |
new ruleshu/4PkiG
| -rw-r--r-- | packages/react-best-practices-build/test-cases.json | 16 | ||||
| -rw-r--r-- | skills/react-best-practices.zip | bin | 99338 -> 102991 bytes | |||
| -rw-r--r-- | skills/react-best-practices/AGENTS.md | 52 | ||||
| -rw-r--r-- | skills/react-best-practices/SKILL.md | 3 | ||||
| -rw-r--r-- | skills/react-best-practices/rules/client-revoke-blob-urls.md | 43 |
5 files changed, 107 insertions, 7 deletions
diff --git a/packages/react-best-practices-build/test-cases.json b/packages/react-best-practices-build/test-cases.json index 02ce981..a0a4bcc 100644 --- a/packages/react-best-practices-build/test-cases.json +++ b/packages/react-best-practices-build/test-cases.json @@ -297,6 +297,22 @@ }, { "ruleId": "", + "ruleTitle": "Revoke Blob URLs After Download", + "type": "bad", + "code": "function downloadBlob(blob: Blob, filename: string) {\n const url = URL.createObjectURL(blob)\n const link = document.createElement('a')\n link.href = url\n link.download = filename\n link.click()\n}", + "language": "typescript", + "description": "Blob URL is never released" + }, + { + "ruleId": "", + "ruleTitle": "Revoke Blob URLs After Download", + "type": "good", + "code": "function downloadBlob(blob: Blob, filename: string) {\n const url = URL.createObjectURL(blob)\n\n try {\n const link = document.createElement('a')\n link.href = url\n link.download = filename\n link.click()\n } finally {\n setTimeout(() => URL.revokeObjectURL(url), 0)\n }\n}", + "language": "typescript", + "description": "release the URL after the browser handles the click" + }, + { + "ruleId": "", "ruleTitle": "Use SWR for Automatic Deduplication", "type": "bad", "code": "function UserList() {\n const [users, setUsers] = useState([])\n useEffect(() => {\n fetch('/api/users')\n .then(r => r.json())\n .then(setUsers)\n }, [])\n}", diff --git a/skills/react-best-practices.zip b/skills/react-best-practices.zip Binary files differindex f6dfb7a..59c1052 100644 --- a/skills/react-best-practices.zip +++ b/skills/react-best-practices.zip diff --git a/skills/react-best-practices/AGENTS.md b/skills/react-best-practices/AGENTS.md index 4e340a5..308e549 100644 --- a/skills/react-best-practices/AGENTS.md +++ b/skills/react-best-practices/AGENTS.md @@ -47,9 +47,10 @@ Comprehensive performance optimization guide for React and Next.js applications, - 3.10 [Use after() for Non-Blocking Operations](#310-use-after-for-non-blocking-operations) 4. [Client-Side Data Fetching](#4-client-side-data-fetching) — **MEDIUM-HIGH** - 4.1 [Deduplicate Global Event Listeners](#41-deduplicate-global-event-listeners) - - 4.2 [Use Passive Event Listeners for Scrolling Performance](#42-use-passive-event-listeners-for-scrolling-performance) - - 4.3 [Use SWR for Automatic Deduplication](#43-use-swr-for-automatic-deduplication) - - 4.4 [Version and Minimize localStorage Data](#44-version-and-minimize-localstorage-data) + - 4.2 [Revoke Blob URLs After Download](#42-revoke-blob-urls-after-download) + - 4.3 [Use Passive Event Listeners for Scrolling Performance](#43-use-passive-event-listeners-for-scrolling-performance) + - 4.4 [Use SWR for Automatic Deduplication](#44-use-swr-for-automatic-deduplication) + - 4.5 [Version and Minimize localStorage Data](#45-version-and-minimize-localstorage-data) 5. [Re-render Optimization](#5-re-render-optimization) — **MEDIUM** - 5.1 [Calculate Derived State During Rendering](#51-calculate-derived-state-during-rendering) - 5.2 [Defer State Reads to Usage Point](#52-defer-state-reads-to-usage-point) @@ -1439,7 +1440,46 @@ function Profile() { } ``` -### 4.2 Use Passive Event Listeners for Scrolling Performance +### 4.2 Revoke Blob URLs After Download + +**Impact: MEDIUM (prevents Blob data from being retained in memory)** + +Every `URL.createObjectURL()` call keeps its backing `Blob` alive until the document unloads or `URL.revokeObjectURL()` is called. Revoke one-off download URLs after triggering the download, especially when users can download large or repeated files. + +**Incorrect: Blob URL is never released** + +```typescript +function downloadBlob(blob: Blob, filename: string) { + const url = URL.createObjectURL(blob) + const link = document.createElement('a') + link.href = url + link.download = filename + link.click() +} +``` + +**Correct: release the URL after the browser handles the click** + +```typescript +function downloadBlob(blob: Blob, filename: string) { + const url = URL.createObjectURL(blob) + + try { + const link = document.createElement('a') + link.href = url + link.download = filename + link.click() + } finally { + setTimeout(() => URL.revokeObjectURL(url), 0) + } +} +``` + +The deferred cleanup lets the browser consume the click before invalidating the URL. For Blob URLs used by images, previews, or other longer-lived consumers, revoke the previous URL when it is replaced or the consumer unmounts instead of immediately after creation. + +Reference: [https://developer.mozilla.org/docs/Web/API/URL/revokeObjectURL_static](https://developer.mozilla.org/docs/Web/API/URL/revokeObjectURL_static) + +### 4.3 Use Passive Event Listeners for Scrolling Performance **Impact: MEDIUM (eliminates scroll delay caused by event listeners)** @@ -1483,7 +1523,7 @@ useEffect(() => { **Don't use passive when:** implementing custom swipe gestures, custom zoom controls, or any listener that needs `preventDefault()`. -### 4.3 Use SWR for Automatic Deduplication +### 4.4 Use SWR for Automatic Deduplication **Impact: MEDIUM-HIGH (automatic deduplication)** @@ -1535,7 +1575,7 @@ function UpdateButton() { Reference: [https://swr.vercel.app](https://swr.vercel.app) -### 4.4 Version and Minimize localStorage Data +### 4.5 Version and Minimize localStorage Data **Impact: MEDIUM (prevents schema conflicts, reduces storage size)** diff --git a/skills/react-best-practices/SKILL.md b/skills/react-best-practices/SKILL.md index 237988d..abd7467 100644 --- a/skills/react-best-practices/SKILL.md +++ b/skills/react-best-practices/SKILL.md @@ -9,7 +9,7 @@ metadata: # Vercel React Best Practices -Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 70 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation. +Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 71 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation. ## When to Apply @@ -71,6 +71,7 @@ Reference these guidelines when: - `client-swr-dedup` - Use SWR for automatic request deduplication - `client-event-listeners` - Deduplicate global event listeners - `client-passive-event-listeners` - Use passive listeners for scroll +- `client-revoke-blob-urls` - Revoke one-off Blob URLs after downloads - `client-localstorage-schema` - Version and minimize localStorage data ### 5. Re-render Optimization (MEDIUM) diff --git a/skills/react-best-practices/rules/client-revoke-blob-urls.md b/skills/react-best-practices/rules/client-revoke-blob-urls.md new file mode 100644 index 0000000..7066c9f --- /dev/null +++ b/skills/react-best-practices/rules/client-revoke-blob-urls.md @@ -0,0 +1,43 @@ +--- +title: Revoke Blob URLs After Download +impact: MEDIUM +impactDescription: prevents Blob data from being retained in memory +tags: client, blob, object-url, download, memory +--- + +## Revoke Blob URLs After Download + +Every `URL.createObjectURL()` call keeps its backing `Blob` alive until the document unloads or `URL.revokeObjectURL()` is called. Revoke one-off download URLs after triggering the download, especially when users can download large or repeated files. + +**Incorrect (Blob URL is never released):** + +```typescript +function downloadBlob(blob: Blob, filename: string) { + const url = URL.createObjectURL(blob) + const link = document.createElement('a') + link.href = url + link.download = filename + link.click() +} +``` + +**Correct (release the URL after the browser handles the click):** + +```typescript +function downloadBlob(blob: Blob, filename: string) { + const url = URL.createObjectURL(blob) + + try { + const link = document.createElement('a') + link.href = url + link.download = filename + link.click() + } finally { + setTimeout(() => URL.revokeObjectURL(url), 0) + } +} +``` + +The deferred cleanup lets the browser consume the click before invalidating the URL. For Blob URLs used by images, previews, or other longer-lived consumers, revoke the previous URL when it is replaced or the consumer unmounts instead of immediately after creation. + +Reference: [MDN: URL.revokeObjectURL()](https://developer.mozilla.org/docs/Web/API/URL/revokeObjectURL_static) |
