mirror of
https://github.com/lobehub/lobe-chat.git
synced 2026-06-17 13:06:21 +00:00
Compare commits
125 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 80bce41dea | |||
| bfe387c027 | |||
| 6034e5fe85 | |||
| b0180f89f3 | |||
| d8c0c264b9 | |||
| 6548fc7572 | |||
| c0c99a7ede | |||
| 6df77315b9 | |||
| 831a9b34f9 | |||
| ad32a61704 | |||
| 3a78f82618 | |||
| 379eb6b320 | |||
| 42ad2a064b | |||
| 24051339a4 | |||
| de6009da7e | |||
| 7efd8b8e98 | |||
| 6ce064656c | |||
| 0d3017b7fe | |||
| 3baf78043d | |||
| f6988e7032 | |||
| 21a0f5c255 | |||
| 4e060be8e4 | |||
| b13bb8a839 | |||
| 093c24f119 | |||
| bcf8628087 | |||
| 0160fbde83 | |||
| 12b1d56e33 | |||
| 8e3d3dbb1b | |||
| 55abddc532 | |||
| bae270e7da | |||
| 22283a43de | |||
| 5664b84ba8 | |||
| 404c5776be | |||
| 26ce317313 | |||
| 3110e2c356 | |||
| 3a955e600f | |||
| 90a5935670 | |||
| d450dd9742 | |||
| e1666a57e4 | |||
| 3a7862f7f5 | |||
| 9ac84e6ac8 | |||
| 4e8b3e8fa8 | |||
| c8694b5c7d | |||
| 9de773ba7d | |||
| 8443904600 | |||
| c628b8aade | |||
| 06d41e5153 | |||
| c0f9875195 | |||
| a8b042f406 | |||
| af234ac25c | |||
| 95a7011437 | |||
| 98cf57bc2c | |||
| 32c0623770 | |||
| f223a12e8f | |||
| ad51305f19 | |||
| 70e1d995c5 | |||
| 44549b9856 | |||
| 13e95b98c2 | |||
| 01550e0b13 | |||
| fb86dc0282 | |||
| c9d9dff635 | |||
| cf5320e27f | |||
| d8121d3322 | |||
| 47fd5e6875 | |||
| eebdd09a5f | |||
| be6b026c7b | |||
| ba30f46bc6 | |||
| 5858cd16ba | |||
| 49a284b418 | |||
| b1c3b83e42 | |||
| 1c29bca963 | |||
| e61ae85f0c | |||
| 5c645f09fd | |||
| ddbe661642 | |||
| 5074fbef2c | |||
| 2fffe8b6ee | |||
| 600cb85001 | |||
| dbb928508f | |||
| bcbdc88742 | |||
| 420fe283f2 | |||
| 9c91dd99fe | |||
| af446d9e3d | |||
| ffb79fa753 | |||
| 358cc0c301 | |||
| 27caed8faf | |||
| 49374daab2 | |||
| dc7f7d212b | |||
| 3d555ced12 | |||
| 046d792997 | |||
| 0d30e5f1f7 | |||
| fd2e3a305d | |||
| 9f30b929d9 | |||
| ff2dfe4215 | |||
| 04465c8105 | |||
| ff63a44f07 | |||
| d78e0f3417 | |||
| 34bdcd4f4c | |||
| 315dec2f7c | |||
| e751e2f89c | |||
| 05a08734ba | |||
| 777b561d68 | |||
| e21231206a | |||
| 8ece553555 | |||
| de51c7de55 | |||
| 97d2477e8c | |||
| 0ffe6c4af5 | |||
| a3dedd5b04 | |||
| 4aa9464310 | |||
| 6fff60d2f2 | |||
| 560f5de026 | |||
| 6ed280e0cc | |||
| abf57c59a0 | |||
| c097c69f49 | |||
| b481253dd5 | |||
| 1018679cc8 | |||
| 38725da72f | |||
| b062f8556f | |||
| 435eede231 | |||
| dff304ca95 | |||
| 484814ab3a | |||
| 4668b85238 | |||
| c2cd768839 | |||
| d92550cbd2 | |||
| 2f032d44d1 | |||
| 7f004c5baf |
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,125 @@
|
||||
---
|
||||
name: vercel-react-best-practices
|
||||
description: React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.
|
||||
license: MIT
|
||||
metadata:
|
||||
author: vercel
|
||||
version: "1.0.0"
|
||||
---
|
||||
|
||||
# Vercel React Best Practices
|
||||
|
||||
Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
|
||||
|
||||
## When to Apply
|
||||
|
||||
Reference these guidelines when:
|
||||
- Writing new React components or Next.js pages
|
||||
- Implementing data fetching (client or server-side)
|
||||
- Reviewing code for performance issues
|
||||
- Refactoring existing React/Next.js code
|
||||
- Optimizing bundle size or load times
|
||||
|
||||
## Rule Categories by Priority
|
||||
|
||||
| Priority | Category | Impact | Prefix |
|
||||
|----------|----------|--------|--------|
|
||||
| 1 | Eliminating Waterfalls | CRITICAL | `async-` |
|
||||
| 2 | Bundle Size Optimization | CRITICAL | `bundle-` |
|
||||
| 3 | Server-Side Performance | HIGH | `server-` |
|
||||
| 4 | Client-Side Data Fetching | MEDIUM-HIGH | `client-` |
|
||||
| 5 | Re-render Optimization | MEDIUM | `rerender-` |
|
||||
| 6 | Rendering Performance | MEDIUM | `rendering-` |
|
||||
| 7 | JavaScript Performance | LOW-MEDIUM | `js-` |
|
||||
| 8 | Advanced Patterns | LOW | `advanced-` |
|
||||
|
||||
## Quick Reference
|
||||
|
||||
### 1. Eliminating Waterfalls (CRITICAL)
|
||||
|
||||
- `async-defer-await` - Move await into branches where actually used
|
||||
- `async-parallel` - Use Promise.all() for independent operations
|
||||
- `async-dependencies` - Use better-all for partial dependencies
|
||||
- `async-api-routes` - Start promises early, await late in API routes
|
||||
- `async-suspense-boundaries` - Use Suspense to stream content
|
||||
|
||||
### 2. Bundle Size Optimization (CRITICAL)
|
||||
|
||||
- `bundle-barrel-imports` - Import directly, avoid barrel files
|
||||
- `bundle-dynamic-imports` - Use next/dynamic for heavy components
|
||||
- `bundle-defer-third-party` - Load analytics/logging after hydration
|
||||
- `bundle-conditional` - Load modules only when feature is activated
|
||||
- `bundle-preload` - Preload on hover/focus for perceived speed
|
||||
|
||||
### 3. Server-Side Performance (HIGH)
|
||||
|
||||
- `server-cache-react` - Use React.cache() for per-request deduplication
|
||||
- `server-cache-lru` - Use LRU cache for cross-request caching
|
||||
- `server-serialization` - Minimize data passed to client components
|
||||
- `server-parallel-fetching` - Restructure components to parallelize fetches
|
||||
- `server-after-nonblocking` - Use after() for non-blocking operations
|
||||
|
||||
### 4. Client-Side Data Fetching (MEDIUM-HIGH)
|
||||
|
||||
- `client-swr-dedup` - Use SWR for automatic request deduplication
|
||||
- `client-event-listeners` - Deduplicate global event listeners
|
||||
|
||||
### 5. Re-render Optimization (MEDIUM)
|
||||
|
||||
- `rerender-defer-reads` - Don't subscribe to state only used in callbacks
|
||||
- `rerender-memo` - Extract expensive work into memoized components
|
||||
- `rerender-dependencies` - Use primitive dependencies in effects
|
||||
- `rerender-derived-state` - Subscribe to derived booleans, not raw values
|
||||
- `rerender-functional-setstate` - Use functional setState for stable callbacks
|
||||
- `rerender-lazy-state-init` - Pass function to useState for expensive values
|
||||
- `rerender-transitions` - Use startTransition for non-urgent updates
|
||||
|
||||
### 6. Rendering Performance (MEDIUM)
|
||||
|
||||
- `rendering-animate-svg-wrapper` - Animate div wrapper, not SVG element
|
||||
- `rendering-content-visibility` - Use content-visibility for long lists
|
||||
- `rendering-hoist-jsx` - Extract static JSX outside components
|
||||
- `rendering-svg-precision` - Reduce SVG coordinate precision
|
||||
- `rendering-hydration-no-flicker` - Use inline script for client-only data
|
||||
- `rendering-activity` - Use Activity component for show/hide
|
||||
- `rendering-conditional-render` - Use ternary, not && for conditionals
|
||||
|
||||
### 7. JavaScript Performance (LOW-MEDIUM)
|
||||
|
||||
- `js-batch-dom-css` - Group CSS changes via classes or cssText
|
||||
- `js-index-maps` - Build Map for repeated lookups
|
||||
- `js-cache-property-access` - Cache object properties in loops
|
||||
- `js-cache-function-results` - Cache function results in module-level Map
|
||||
- `js-cache-storage` - Cache localStorage/sessionStorage reads
|
||||
- `js-combine-iterations` - Combine multiple filter/map into one loop
|
||||
- `js-length-check-first` - Check array length before expensive comparison
|
||||
- `js-early-exit` - Return early from functions
|
||||
- `js-hoist-regexp` - Hoist RegExp creation outside loops
|
||||
- `js-min-max-loop` - Use loop for min/max instead of sort
|
||||
- `js-set-map-lookups` - Use Set/Map for O(1) lookups
|
||||
- `js-tosorted-immutable` - Use toSorted() for immutability
|
||||
|
||||
### 8. Advanced Patterns (LOW)
|
||||
|
||||
- `advanced-event-handler-refs` - Store event handlers in refs
|
||||
- `advanced-use-latest` - useLatest for stable callback refs
|
||||
|
||||
## How to Use
|
||||
|
||||
Read individual rule files for detailed explanations and code examples:
|
||||
|
||||
```
|
||||
rules/async-parallel.md
|
||||
rules/bundle-barrel-imports.md
|
||||
rules/_sections.md
|
||||
```
|
||||
|
||||
Each rule file contains:
|
||||
- Brief explanation of why it matters
|
||||
- Incorrect code example with explanation
|
||||
- Correct code example with explanation
|
||||
- Additional context and references
|
||||
|
||||
## Full Compiled Document
|
||||
|
||||
For the complete guide with all rules expanded: `AGENTS.md`
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
title: Store Event Handlers in Refs
|
||||
impact: LOW
|
||||
impactDescription: stable subscriptions
|
||||
tags: advanced, hooks, refs, event-handlers, optimization
|
||||
---
|
||||
|
||||
## Store Event Handlers in Refs
|
||||
|
||||
Store callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.
|
||||
|
||||
**Incorrect (re-subscribes on every render):**
|
||||
|
||||
```tsx
|
||||
function useWindowEvent(event: string, handler: (e) => void) {
|
||||
useEffect(() => {
|
||||
window.addEventListener(event, handler)
|
||||
return () => window.removeEventListener(event, handler)
|
||||
}, [event, handler])
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (stable subscription):**
|
||||
|
||||
```tsx
|
||||
function useWindowEvent(event: string, handler: (e) => void) {
|
||||
const handlerRef = useRef(handler)
|
||||
useEffect(() => {
|
||||
handlerRef.current = handler
|
||||
}, [handler])
|
||||
|
||||
useEffect(() => {
|
||||
const listener = (e) => handlerRef.current(e)
|
||||
window.addEventListener(event, listener)
|
||||
return () => window.removeEventListener(event, listener)
|
||||
}, [event])
|
||||
}
|
||||
```
|
||||
|
||||
**Alternative: use `useEffectEvent` if you're on latest React:**
|
||||
|
||||
```tsx
|
||||
import { useEffectEvent } from 'react'
|
||||
|
||||
function useWindowEvent(event: string, handler: (e) => void) {
|
||||
const onEvent = useEffectEvent(handler)
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener(event, onEvent)
|
||||
return () => window.removeEventListener(event, onEvent)
|
||||
}, [event])
|
||||
}
|
||||
```
|
||||
|
||||
`useEffectEvent` provides a cleaner API for the same pattern: it creates a stable function reference that always calls the latest version of the handler.
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: useLatest for Stable Callback Refs
|
||||
impact: LOW
|
||||
impactDescription: prevents effect re-runs
|
||||
tags: advanced, hooks, useLatest, refs, optimization
|
||||
---
|
||||
|
||||
## useLatest for Stable Callback Refs
|
||||
|
||||
Access latest values in callbacks without adding them to dependency arrays. Prevents effect re-runs while avoiding stale closures.
|
||||
|
||||
**Implementation:**
|
||||
|
||||
```typescript
|
||||
function useLatest<T>(value: T) {
|
||||
const ref = useRef(value)
|
||||
useLayoutEffect(() => {
|
||||
ref.current = value
|
||||
}, [value])
|
||||
return ref
|
||||
}
|
||||
```
|
||||
|
||||
**Incorrect (effect re-runs on every callback change):**
|
||||
|
||||
```tsx
|
||||
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
||||
const [query, setQuery] = useState('')
|
||||
|
||||
useEffect(() => {
|
||||
const timeout = setTimeout(() => onSearch(query), 300)
|
||||
return () => clearTimeout(timeout)
|
||||
}, [query, onSearch])
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (stable effect, fresh callback):**
|
||||
|
||||
```tsx
|
||||
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
||||
const [query, setQuery] = useState('')
|
||||
const onSearchRef = useLatest(onSearch)
|
||||
|
||||
useEffect(() => {
|
||||
const timeout = setTimeout(() => onSearchRef.current(query), 300)
|
||||
return () => clearTimeout(timeout)
|
||||
}, [query])
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: Prevent Waterfall Chains in API Routes
|
||||
impact: CRITICAL
|
||||
impactDescription: 2-10× improvement
|
||||
tags: api-routes, server-actions, waterfalls, parallelization
|
||||
---
|
||||
|
||||
## Prevent Waterfall Chains in API Routes
|
||||
|
||||
In API routes and Server Actions, start independent operations immediately, even if you don't await them yet.
|
||||
|
||||
**Incorrect (config waits for auth, data waits for both):**
|
||||
|
||||
```typescript
|
||||
export async function GET(request: Request) {
|
||||
const session = await auth()
|
||||
const config = await fetchConfig()
|
||||
const data = await fetchData(session.user.id)
|
||||
return Response.json({ data, config })
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (auth and config start immediately):**
|
||||
|
||||
```typescript
|
||||
export async function GET(request: Request) {
|
||||
const sessionPromise = auth()
|
||||
const configPromise = fetchConfig()
|
||||
const session = await sessionPromise
|
||||
const [config, data] = await Promise.all([
|
||||
configPromise,
|
||||
fetchData(session.user.id)
|
||||
])
|
||||
return Response.json({ data, config })
|
||||
}
|
||||
```
|
||||
|
||||
For operations with more complex dependency chains, use `better-all` to automatically maximize parallelism (see Dependency-Based Parallelization).
|
||||
@@ -0,0 +1,80 @@
|
||||
---
|
||||
title: Defer Await Until Needed
|
||||
impact: HIGH
|
||||
impactDescription: avoids blocking unused code paths
|
||||
tags: async, await, conditional, optimization
|
||||
---
|
||||
|
||||
## Defer Await Until Needed
|
||||
|
||||
Move `await` operations into the branches where they're actually used to avoid blocking code paths that don't need them.
|
||||
|
||||
**Incorrect (blocks both branches):**
|
||||
|
||||
```typescript
|
||||
async function handleRequest(userId: string, skipProcessing: boolean) {
|
||||
const userData = await fetchUserData(userId)
|
||||
|
||||
if (skipProcessing) {
|
||||
// Returns immediately but still waited for userData
|
||||
return { skipped: true }
|
||||
}
|
||||
|
||||
// Only this branch uses userData
|
||||
return processUserData(userData)
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (only blocks when needed):**
|
||||
|
||||
```typescript
|
||||
async function handleRequest(userId: string, skipProcessing: boolean) {
|
||||
if (skipProcessing) {
|
||||
// Returns immediately without waiting
|
||||
return { skipped: true }
|
||||
}
|
||||
|
||||
// Fetch only when needed
|
||||
const userData = await fetchUserData(userId)
|
||||
return processUserData(userData)
|
||||
}
|
||||
```
|
||||
|
||||
**Another example (early return optimization):**
|
||||
|
||||
```typescript
|
||||
// Incorrect: always fetches permissions
|
||||
async function updateResource(resourceId: string, userId: string) {
|
||||
const permissions = await fetchPermissions(userId)
|
||||
const resource = await getResource(resourceId)
|
||||
|
||||
if (!resource) {
|
||||
return { error: 'Not found' }
|
||||
}
|
||||
|
||||
if (!permissions.canEdit) {
|
||||
return { error: 'Forbidden' }
|
||||
}
|
||||
|
||||
return await updateResourceData(resource, permissions)
|
||||
}
|
||||
|
||||
// Correct: fetches only when needed
|
||||
async function updateResource(resourceId: string, userId: string) {
|
||||
const resource = await getResource(resourceId)
|
||||
|
||||
if (!resource) {
|
||||
return { error: 'Not found' }
|
||||
}
|
||||
|
||||
const permissions = await fetchPermissions(userId)
|
||||
|
||||
if (!permissions.canEdit) {
|
||||
return { error: 'Forbidden' }
|
||||
}
|
||||
|
||||
return await updateResourceData(resource, permissions)
|
||||
}
|
||||
```
|
||||
|
||||
This optimization is especially valuable when the skipped branch is frequently taken, or when the deferred operation is expensive.
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: Dependency-Based Parallelization
|
||||
impact: CRITICAL
|
||||
impactDescription: 2-10× improvement
|
||||
tags: async, parallelization, dependencies, better-all
|
||||
---
|
||||
|
||||
## Dependency-Based Parallelization
|
||||
|
||||
For operations with partial dependencies, use `better-all` to maximize parallelism. It automatically starts each task at the earliest possible moment.
|
||||
|
||||
**Incorrect (profile waits for config unnecessarily):**
|
||||
|
||||
```typescript
|
||||
const [user, config] = await Promise.all([
|
||||
fetchUser(),
|
||||
fetchConfig()
|
||||
])
|
||||
const profile = await fetchProfile(user.id)
|
||||
```
|
||||
|
||||
**Correct (config and profile run in parallel):**
|
||||
|
||||
```typescript
|
||||
import { all } from 'better-all'
|
||||
|
||||
const { user, config, profile } = await all({
|
||||
async user() { return fetchUser() },
|
||||
async config() { return fetchConfig() },
|
||||
async profile() {
|
||||
return fetchProfile((await this.$.user).id)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Reference: [https://github.com/shuding/better-all](https://github.com/shuding/better-all)
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: Promise.all() for Independent Operations
|
||||
impact: CRITICAL
|
||||
impactDescription: 2-10× improvement
|
||||
tags: async, parallelization, promises, waterfalls
|
||||
---
|
||||
|
||||
## Promise.all() for Independent Operations
|
||||
|
||||
When async operations have no interdependencies, execute them concurrently using `Promise.all()`.
|
||||
|
||||
**Incorrect (sequential execution, 3 round trips):**
|
||||
|
||||
```typescript
|
||||
const user = await fetchUser()
|
||||
const posts = await fetchPosts()
|
||||
const comments = await fetchComments()
|
||||
```
|
||||
|
||||
**Correct (parallel execution, 1 round trip):**
|
||||
|
||||
```typescript
|
||||
const [user, posts, comments] = await Promise.all([
|
||||
fetchUser(),
|
||||
fetchPosts(),
|
||||
fetchComments()
|
||||
])
|
||||
```
|
||||
@@ -0,0 +1,99 @@
|
||||
---
|
||||
title: Strategic Suspense Boundaries
|
||||
impact: HIGH
|
||||
impactDescription: faster initial paint
|
||||
tags: async, suspense, streaming, layout-shift
|
||||
---
|
||||
|
||||
## Strategic Suspense Boundaries
|
||||
|
||||
Instead of awaiting data in async components before returning JSX, use Suspense boundaries to show the wrapper UI faster while data loads.
|
||||
|
||||
**Incorrect (wrapper blocked by data fetching):**
|
||||
|
||||
```tsx
|
||||
async function Page() {
|
||||
const data = await fetchData() // Blocks entire page
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div>Sidebar</div>
|
||||
<div>Header</div>
|
||||
<div>
|
||||
<DataDisplay data={data} />
|
||||
</div>
|
||||
<div>Footer</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
The entire layout waits for data even though only the middle section needs it.
|
||||
|
||||
**Correct (wrapper shows immediately, data streams in):**
|
||||
|
||||
```tsx
|
||||
function Page() {
|
||||
return (
|
||||
<div>
|
||||
<div>Sidebar</div>
|
||||
<div>Header</div>
|
||||
<div>
|
||||
<Suspense fallback={<Skeleton />}>
|
||||
<DataDisplay />
|
||||
</Suspense>
|
||||
</div>
|
||||
<div>Footer</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
async function DataDisplay() {
|
||||
const data = await fetchData() // Only blocks this component
|
||||
return <div>{data.content}</div>
|
||||
}
|
||||
```
|
||||
|
||||
Sidebar, Header, and Footer render immediately. Only DataDisplay waits for data.
|
||||
|
||||
**Alternative (share promise across components):**
|
||||
|
||||
```tsx
|
||||
function Page() {
|
||||
// Start fetch immediately, but don't await
|
||||
const dataPromise = fetchData()
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div>Sidebar</div>
|
||||
<div>Header</div>
|
||||
<Suspense fallback={<Skeleton />}>
|
||||
<DataDisplay dataPromise={dataPromise} />
|
||||
<DataSummary dataPromise={dataPromise} />
|
||||
</Suspense>
|
||||
<div>Footer</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function DataDisplay({ dataPromise }: { dataPromise: Promise<Data> }) {
|
||||
const data = use(dataPromise) // Unwraps the promise
|
||||
return <div>{data.content}</div>
|
||||
}
|
||||
|
||||
function DataSummary({ dataPromise }: { dataPromise: Promise<Data> }) {
|
||||
const data = use(dataPromise) // Reuses the same promise
|
||||
return <div>{data.summary}</div>
|
||||
}
|
||||
```
|
||||
|
||||
Both components share the same promise, so only one fetch occurs. Layout renders immediately while both components wait together.
|
||||
|
||||
**When NOT to use this pattern:**
|
||||
|
||||
- Critical data needed for layout decisions (affects positioning)
|
||||
- SEO-critical content above the fold
|
||||
- Small, fast queries where suspense overhead isn't worth it
|
||||
- When you want to avoid layout shift (loading → content jump)
|
||||
|
||||
**Trade-off:** Faster initial paint vs potential layout shift. Choose based on your UX priorities.
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
title: Avoid Barrel File Imports
|
||||
impact: CRITICAL
|
||||
impactDescription: 200-800ms import cost, slow builds
|
||||
tags: bundle, imports, tree-shaking, barrel-files, performance
|
||||
---
|
||||
|
||||
## Avoid Barrel File Imports
|
||||
|
||||
Import directly from source files instead of barrel files to avoid loading thousands of unused modules. **Barrel files** are entry points that re-export multiple modules (e.g., `index.js` that does `export * from './module'`).
|
||||
|
||||
Popular icon and component libraries can have **up to 10,000 re-exports** in their entry file. For many React packages, **it takes 200-800ms just to import them**, affecting both development speed and production cold starts.
|
||||
|
||||
**Why tree-shaking doesn't help:** When a library is marked as external (not bundled), the bundler can't optimize it. If you bundle it to enable tree-shaking, builds become substantially slower analyzing the entire module graph.
|
||||
|
||||
**Incorrect (imports entire library):**
|
||||
|
||||
```tsx
|
||||
import { Check, X, Menu } from 'lucide-react'
|
||||
// Loads 1,583 modules, takes ~2.8s extra in dev
|
||||
// Runtime cost: 200-800ms on every cold start
|
||||
|
||||
import { Button, TextField } from '@mui/material'
|
||||
// Loads 2,225 modules, takes ~4.2s extra in dev
|
||||
```
|
||||
|
||||
**Correct (imports only what you need):**
|
||||
|
||||
```tsx
|
||||
import Check from 'lucide-react/dist/esm/icons/check'
|
||||
import X from 'lucide-react/dist/esm/icons/x'
|
||||
import Menu from 'lucide-react/dist/esm/icons/menu'
|
||||
// Loads only 3 modules (~2KB vs ~1MB)
|
||||
|
||||
import Button from '@mui/material/Button'
|
||||
import TextField from '@mui/material/TextField'
|
||||
// Loads only what you use
|
||||
```
|
||||
|
||||
**Alternative (Next.js 13.5+):**
|
||||
|
||||
```js
|
||||
// next.config.js - use optimizePackageImports
|
||||
module.exports = {
|
||||
experimental: {
|
||||
optimizePackageImports: ['lucide-react', '@mui/material']
|
||||
}
|
||||
}
|
||||
|
||||
// Then you can keep the ergonomic barrel imports:
|
||||
import { Check, X, Menu } from 'lucide-react'
|
||||
// Automatically transformed to direct imports at build time
|
||||
```
|
||||
|
||||
Direct imports provide 15-70% faster dev boot, 28% faster builds, 40% faster cold starts, and significantly faster HMR.
|
||||
|
||||
Libraries commonly affected: `lucide-react`, `@mui/material`, `@mui/icons-material`, `@tabler/icons-react`, `react-icons`, `@headlessui/react`, `@radix-ui/react-*`, `lodash`, `ramda`, `date-fns`, `rxjs`, `react-use`.
|
||||
|
||||
Reference: [How we optimized package imports in Next.js](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js)
|
||||
@@ -0,0 +1,31 @@
|
||||
---
|
||||
title: Conditional Module Loading
|
||||
impact: HIGH
|
||||
impactDescription: loads large data only when needed
|
||||
tags: bundle, conditional-loading, lazy-loading
|
||||
---
|
||||
|
||||
## Conditional Module Loading
|
||||
|
||||
Load large data or modules only when a feature is activated.
|
||||
|
||||
**Example (lazy-load animation frames):**
|
||||
|
||||
```tsx
|
||||
function AnimationPlayer({ enabled, setEnabled }: { enabled: boolean; setEnabled: React.Dispatch<React.SetStateAction<boolean>> }) {
|
||||
const [frames, setFrames] = useState<Frame[] | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (enabled && !frames && typeof window !== 'undefined') {
|
||||
import('./animation-frames.js')
|
||||
.then(mod => setFrames(mod.frames))
|
||||
.catch(() => setEnabled(false))
|
||||
}
|
||||
}, [enabled, frames, setEnabled])
|
||||
|
||||
if (!frames) return <Skeleton />
|
||||
return <Canvas frames={frames} />
|
||||
}
|
||||
```
|
||||
|
||||
The `typeof window !== 'undefined'` check prevents bundling this module for SSR, optimizing server bundle size and build speed.
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: Defer Non-Critical Third-Party Libraries
|
||||
impact: MEDIUM
|
||||
impactDescription: loads after hydration
|
||||
tags: bundle, third-party, analytics, defer
|
||||
---
|
||||
|
||||
## Defer Non-Critical Third-Party Libraries
|
||||
|
||||
Analytics, logging, and error tracking don't block user interaction. Load them after hydration.
|
||||
|
||||
**Incorrect (blocks initial bundle):**
|
||||
|
||||
```tsx
|
||||
import { Analytics } from '@vercel/analytics/react'
|
||||
|
||||
export default function RootLayout({ children }) {
|
||||
return (
|
||||
<html>
|
||||
<body>
|
||||
{children}
|
||||
<Analytics />
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (loads after hydration):**
|
||||
|
||||
```tsx
|
||||
import dynamic from 'next/dynamic'
|
||||
|
||||
const Analytics = dynamic(
|
||||
() => import('@vercel/analytics/react').then(m => m.Analytics),
|
||||
{ ssr: false }
|
||||
)
|
||||
|
||||
export default function RootLayout({ children }) {
|
||||
return (
|
||||
<html>
|
||||
<body>
|
||||
{children}
|
||||
<Analytics />
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
title: Dynamic Imports for Heavy Components
|
||||
impact: CRITICAL
|
||||
impactDescription: directly affects TTI and LCP
|
||||
tags: bundle, dynamic-import, code-splitting, next-dynamic
|
||||
---
|
||||
|
||||
## Dynamic Imports for Heavy Components
|
||||
|
||||
Use `next/dynamic` to lazy-load large components not needed on initial render.
|
||||
|
||||
**Incorrect (Monaco bundles with main chunk ~300KB):**
|
||||
|
||||
```tsx
|
||||
import { MonacoEditor } from './monaco-editor'
|
||||
|
||||
function CodePanel({ code }: { code: string }) {
|
||||
return <MonacoEditor value={code} />
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (Monaco loads on demand):**
|
||||
|
||||
```tsx
|
||||
import dynamic from 'next/dynamic'
|
||||
|
||||
const MonacoEditor = dynamic(
|
||||
() => import('./monaco-editor').then(m => m.MonacoEditor),
|
||||
{ ssr: false }
|
||||
)
|
||||
|
||||
function CodePanel({ code }: { code: string }) {
|
||||
return <MonacoEditor value={code} />
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
title: Preload Based on User Intent
|
||||
impact: MEDIUM
|
||||
impactDescription: reduces perceived latency
|
||||
tags: bundle, preload, user-intent, hover
|
||||
---
|
||||
|
||||
## Preload Based on User Intent
|
||||
|
||||
Preload heavy bundles before they're needed to reduce perceived latency.
|
||||
|
||||
**Example (preload on hover/focus):**
|
||||
|
||||
```tsx
|
||||
function EditorButton({ onClick }: { onClick: () => void }) {
|
||||
const preload = () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
void import('./monaco-editor')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
onMouseEnter={preload}
|
||||
onFocus={preload}
|
||||
onClick={onClick}
|
||||
>
|
||||
Open Editor
|
||||
</button>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
**Example (preload when feature flag is enabled):**
|
||||
|
||||
```tsx
|
||||
function FlagsProvider({ children, flags }: Props) {
|
||||
useEffect(() => {
|
||||
if (flags.editorEnabled && typeof window !== 'undefined') {
|
||||
void import('./monaco-editor').then(mod => mod.init())
|
||||
}
|
||||
}, [flags.editorEnabled])
|
||||
|
||||
return <FlagsContext.Provider value={flags}>
|
||||
{children}
|
||||
</FlagsContext.Provider>
|
||||
}
|
||||
```
|
||||
|
||||
The `typeof window !== 'undefined'` check prevents bundling preloaded modules for SSR, optimizing server bundle size and build speed.
|
||||
@@ -0,0 +1,74 @@
|
||||
---
|
||||
title: Deduplicate Global Event Listeners
|
||||
impact: LOW
|
||||
impactDescription: single listener for N components
|
||||
tags: client, swr, event-listeners, subscription
|
||||
---
|
||||
|
||||
## Deduplicate Global Event Listeners
|
||||
|
||||
Use `useSWRSubscription()` to share global event listeners across component instances.
|
||||
|
||||
**Incorrect (N instances = N listeners):**
|
||||
|
||||
```tsx
|
||||
function useKeyboardShortcut(key: string, callback: () => void) {
|
||||
useEffect(() => {
|
||||
const handler = (e: KeyboardEvent) => {
|
||||
if (e.metaKey && e.key === key) {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
window.addEventListener('keydown', handler)
|
||||
return () => window.removeEventListener('keydown', handler)
|
||||
}, [key, callback])
|
||||
}
|
||||
```
|
||||
|
||||
When using the `useKeyboardShortcut` hook multiple times, each instance will register a new listener.
|
||||
|
||||
**Correct (N instances = 1 listener):**
|
||||
|
||||
```tsx
|
||||
import useSWRSubscription from 'swr/subscription'
|
||||
|
||||
// Module-level Map to track callbacks per key
|
||||
const keyCallbacks = new Map<string, Set<() => void>>()
|
||||
|
||||
function useKeyboardShortcut(key: string, callback: () => void) {
|
||||
// Register this callback in the Map
|
||||
useEffect(() => {
|
||||
if (!keyCallbacks.has(key)) {
|
||||
keyCallbacks.set(key, new Set())
|
||||
}
|
||||
keyCallbacks.get(key)!.add(callback)
|
||||
|
||||
return () => {
|
||||
const set = keyCallbacks.get(key)
|
||||
if (set) {
|
||||
set.delete(callback)
|
||||
if (set.size === 0) {
|
||||
keyCallbacks.delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [key, callback])
|
||||
|
||||
useSWRSubscription('global-keydown', () => {
|
||||
const handler = (e: KeyboardEvent) => {
|
||||
if (e.metaKey && keyCallbacks.has(e.key)) {
|
||||
keyCallbacks.get(e.key)!.forEach(cb => cb())
|
||||
}
|
||||
}
|
||||
window.addEventListener('keydown', handler)
|
||||
return () => window.removeEventListener('keydown', handler)
|
||||
})
|
||||
}
|
||||
|
||||
function Profile() {
|
||||
// Multiple shortcuts will share the same listener
|
||||
useKeyboardShortcut('p', () => { /* ... */ })
|
||||
useKeyboardShortcut('k', () => { /* ... */ })
|
||||
// ...
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,71 @@
|
||||
---
|
||||
title: Version and Minimize localStorage Data
|
||||
impact: MEDIUM
|
||||
impactDescription: prevents schema conflicts, reduces storage size
|
||||
tags: client, localStorage, storage, versioning, data-minimization
|
||||
---
|
||||
|
||||
## Version and Minimize localStorage Data
|
||||
|
||||
Add version prefix to keys and store only needed fields. Prevents schema conflicts and accidental storage of sensitive data.
|
||||
|
||||
**Incorrect:**
|
||||
|
||||
```typescript
|
||||
// No version, stores everything, no error handling
|
||||
localStorage.setItem('userConfig', JSON.stringify(fullUserObject))
|
||||
const data = localStorage.getItem('userConfig')
|
||||
```
|
||||
|
||||
**Correct:**
|
||||
|
||||
```typescript
|
||||
const VERSION = 'v2'
|
||||
|
||||
function saveConfig(config: { theme: string; language: string }) {
|
||||
try {
|
||||
localStorage.setItem(`userConfig:${VERSION}`, JSON.stringify(config))
|
||||
} catch {
|
||||
// Throws in incognito/private browsing, quota exceeded, or disabled
|
||||
}
|
||||
}
|
||||
|
||||
function loadConfig() {
|
||||
try {
|
||||
const data = localStorage.getItem(`userConfig:${VERSION}`)
|
||||
return data ? JSON.parse(data) : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// Migration from v1 to v2
|
||||
function migrate() {
|
||||
try {
|
||||
const v1 = localStorage.getItem('userConfig:v1')
|
||||
if (v1) {
|
||||
const old = JSON.parse(v1)
|
||||
saveConfig({ theme: old.darkMode ? 'dark' : 'light', language: old.lang })
|
||||
localStorage.removeItem('userConfig:v1')
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
```
|
||||
|
||||
**Store minimal fields from server responses:**
|
||||
|
||||
```typescript
|
||||
// User object has 20+ fields, only store what UI needs
|
||||
function cachePrefs(user: FullUser) {
|
||||
try {
|
||||
localStorage.setItem('prefs:v1', JSON.stringify({
|
||||
theme: user.preferences.theme,
|
||||
notifications: user.preferences.notifications
|
||||
}))
|
||||
} catch {}
|
||||
}
|
||||
```
|
||||
|
||||
**Always wrap in try-catch:** `getItem()` and `setItem()` throw in incognito/private browsing (Safari, Firefox), when quota exceeded, or when disabled.
|
||||
|
||||
**Benefits:** Schema evolution via versioning, reduced storage size, prevents storing tokens/PII/internal flags.
|
||||
@@ -0,0 +1,48 @@
|
||||
---
|
||||
title: Use Passive Event Listeners for Scrolling Performance
|
||||
impact: MEDIUM
|
||||
impactDescription: eliminates scroll delay caused by event listeners
|
||||
tags: client, event-listeners, scrolling, performance, touch, wheel
|
||||
---
|
||||
|
||||
## Use Passive Event Listeners for Scrolling Performance
|
||||
|
||||
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.
|
||||
|
||||
**Incorrect:**
|
||||
|
||||
```typescript
|
||||
useEffect(() => {
|
||||
const handleTouch = (e: TouchEvent) => console.log(e.touches[0].clientX)
|
||||
const handleWheel = (e: WheelEvent) => console.log(e.deltaY)
|
||||
|
||||
document.addEventListener('touchstart', handleTouch)
|
||||
document.addEventListener('wheel', handleWheel)
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('touchstart', handleTouch)
|
||||
document.removeEventListener('wheel', handleWheel)
|
||||
}
|
||||
}, [])
|
||||
```
|
||||
|
||||
**Correct:**
|
||||
|
||||
```typescript
|
||||
useEffect(() => {
|
||||
const handleTouch = (e: TouchEvent) => console.log(e.touches[0].clientX)
|
||||
const handleWheel = (e: WheelEvent) => console.log(e.deltaY)
|
||||
|
||||
document.addEventListener('touchstart', handleTouch, { passive: true })
|
||||
document.addEventListener('wheel', handleWheel, { passive: true })
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('touchstart', handleTouch)
|
||||
document.removeEventListener('wheel', handleWheel)
|
||||
}
|
||||
}, [])
|
||||
```
|
||||
|
||||
**Use passive when:** tracking/analytics, logging, any listener that doesn't call `preventDefault()`.
|
||||
|
||||
**Don't use passive when:** implementing custom swipe gestures, custom zoom controls, or any listener that needs `preventDefault()`.
|
||||
@@ -0,0 +1,56 @@
|
||||
---
|
||||
title: Use SWR for Automatic Deduplication
|
||||
impact: MEDIUM-HIGH
|
||||
impactDescription: automatic deduplication
|
||||
tags: client, swr, deduplication, data-fetching
|
||||
---
|
||||
|
||||
## Use SWR for Automatic Deduplication
|
||||
|
||||
SWR enables request deduplication, caching, and revalidation across component instances.
|
||||
|
||||
**Incorrect (no deduplication, each instance fetches):**
|
||||
|
||||
```tsx
|
||||
function UserList() {
|
||||
const [users, setUsers] = useState([])
|
||||
useEffect(() => {
|
||||
fetch('/api/users')
|
||||
.then(r => r.json())
|
||||
.then(setUsers)
|
||||
}, [])
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (multiple instances share one request):**
|
||||
|
||||
```tsx
|
||||
import useSWR from 'swr'
|
||||
|
||||
function UserList() {
|
||||
const { data: users } = useSWR('/api/users', fetcher)
|
||||
}
|
||||
```
|
||||
|
||||
**For immutable data:**
|
||||
|
||||
```tsx
|
||||
import { useImmutableSWR } from '@/lib/swr'
|
||||
|
||||
function StaticContent() {
|
||||
const { data } = useImmutableSWR('/api/config', fetcher)
|
||||
}
|
||||
```
|
||||
|
||||
**For mutations:**
|
||||
|
||||
```tsx
|
||||
import { useSWRMutation } from 'swr/mutation'
|
||||
|
||||
function UpdateButton() {
|
||||
const { trigger } = useSWRMutation('/api/user', updateUser)
|
||||
return <button onClick={() => trigger()}>Update</button>
|
||||
}
|
||||
```
|
||||
|
||||
Reference: [https://swr.vercel.app](https://swr.vercel.app)
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
title: Batch DOM CSS Changes
|
||||
impact: MEDIUM
|
||||
impactDescription: reduces reflows/repaints
|
||||
tags: javascript, dom, css, performance, reflow
|
||||
---
|
||||
|
||||
## Batch DOM CSS Changes
|
||||
|
||||
Avoid interleaving style writes with layout reads. When you read a layout property (like `offsetWidth`, `getBoundingClientRect()`, or `getComputedStyle()`) between style changes, the browser is forced to trigger a synchronous reflow.
|
||||
|
||||
**Incorrect (interleaved reads and writes force reflows):**
|
||||
|
||||
```typescript
|
||||
function updateElementStyles(element: HTMLElement) {
|
||||
element.style.width = '100px'
|
||||
const width = element.offsetWidth // Forces reflow
|
||||
element.style.height = '200px'
|
||||
const height = element.offsetHeight // Forces another reflow
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (batch writes, then read once):**
|
||||
|
||||
```typescript
|
||||
function updateElementStyles(element: HTMLElement) {
|
||||
// Batch all writes together
|
||||
element.style.width = '100px'
|
||||
element.style.height = '200px'
|
||||
element.style.backgroundColor = 'blue'
|
||||
element.style.border = '1px solid black'
|
||||
|
||||
// Read after all writes are done (single reflow)
|
||||
const { width, height } = element.getBoundingClientRect()
|
||||
}
|
||||
```
|
||||
|
||||
**Better: use CSS classes**
|
||||
|
||||
```css
|
||||
.highlighted-box {
|
||||
width: 100px;
|
||||
height: 200px;
|
||||
background-color: blue;
|
||||
border: 1px solid black;
|
||||
}
|
||||
```
|
||||
|
||||
```typescript
|
||||
function updateElementStyles(element: HTMLElement) {
|
||||
element.classList.add('highlighted-box')
|
||||
|
||||
const { width, height } = element.getBoundingClientRect()
|
||||
}
|
||||
```
|
||||
|
||||
Prefer CSS classes over inline styles when possible. CSS files are cached by the browser, and classes provide better separation of concerns and are easier to maintain.
|
||||
@@ -0,0 +1,80 @@
|
||||
---
|
||||
title: Cache Repeated Function Calls
|
||||
impact: MEDIUM
|
||||
impactDescription: avoid redundant computation
|
||||
tags: javascript, cache, memoization, performance
|
||||
---
|
||||
|
||||
## Cache Repeated Function Calls
|
||||
|
||||
Use a module-level Map to cache function results when the same function is called repeatedly with the same inputs during render.
|
||||
|
||||
**Incorrect (redundant computation):**
|
||||
|
||||
```typescript
|
||||
function ProjectList({ projects }: { projects: Project[] }) {
|
||||
return (
|
||||
<div>
|
||||
{projects.map(project => {
|
||||
// slugify() called 100+ times for same project names
|
||||
const slug = slugify(project.name)
|
||||
|
||||
return <ProjectCard key={project.id} slug={slug} />
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (cached results):**
|
||||
|
||||
```typescript
|
||||
// Module-level cache
|
||||
const slugifyCache = new Map<string, string>()
|
||||
|
||||
function cachedSlugify(text: string): string {
|
||||
if (slugifyCache.has(text)) {
|
||||
return slugifyCache.get(text)!
|
||||
}
|
||||
const result = slugify(text)
|
||||
slugifyCache.set(text, result)
|
||||
return result
|
||||
}
|
||||
|
||||
function ProjectList({ projects }: { projects: Project[] }) {
|
||||
return (
|
||||
<div>
|
||||
{projects.map(project => {
|
||||
// Computed only once per unique project name
|
||||
const slug = cachedSlugify(project.name)
|
||||
|
||||
return <ProjectCard key={project.id} slug={slug} />
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
**Simpler pattern for single-value functions:**
|
||||
|
||||
```typescript
|
||||
let isLoggedInCache: boolean | null = null
|
||||
|
||||
function isLoggedIn(): boolean {
|
||||
if (isLoggedInCache !== null) {
|
||||
return isLoggedInCache
|
||||
}
|
||||
|
||||
isLoggedInCache = document.cookie.includes('auth=')
|
||||
return isLoggedInCache
|
||||
}
|
||||
|
||||
// Clear cache when auth changes
|
||||
function onAuthChange() {
|
||||
isLoggedInCache = null
|
||||
}
|
||||
```
|
||||
|
||||
Use a Map (not a hook) so it works everywhere: utilities, event handlers, not just React components.
|
||||
|
||||
Reference: [How we made the Vercel Dashboard twice as fast](https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast)
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: Cache Property Access in Loops
|
||||
impact: LOW-MEDIUM
|
||||
impactDescription: reduces lookups
|
||||
tags: javascript, loops, optimization, caching
|
||||
---
|
||||
|
||||
## Cache Property Access in Loops
|
||||
|
||||
Cache object property lookups in hot paths.
|
||||
|
||||
**Incorrect (3 lookups × N iterations):**
|
||||
|
||||
```typescript
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
process(obj.config.settings.value)
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (1 lookup total):**
|
||||
|
||||
```typescript
|
||||
const value = obj.config.settings.value
|
||||
const len = arr.length
|
||||
for (let i = 0; i < len; i++) {
|
||||
process(value)
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,70 @@
|
||||
---
|
||||
title: Cache Storage API Calls
|
||||
impact: LOW-MEDIUM
|
||||
impactDescription: reduces expensive I/O
|
||||
tags: javascript, localStorage, storage, caching, performance
|
||||
---
|
||||
|
||||
## Cache Storage API Calls
|
||||
|
||||
`localStorage`, `sessionStorage`, and `document.cookie` are synchronous and expensive. Cache reads in memory.
|
||||
|
||||
**Incorrect (reads storage on every call):**
|
||||
|
||||
```typescript
|
||||
function getTheme() {
|
||||
return localStorage.getItem('theme') ?? 'light'
|
||||
}
|
||||
// Called 10 times = 10 storage reads
|
||||
```
|
||||
|
||||
**Correct (Map cache):**
|
||||
|
||||
```typescript
|
||||
const storageCache = new Map<string, string | null>()
|
||||
|
||||
function getLocalStorage(key: string) {
|
||||
if (!storageCache.has(key)) {
|
||||
storageCache.set(key, localStorage.getItem(key))
|
||||
}
|
||||
return storageCache.get(key)
|
||||
}
|
||||
|
||||
function setLocalStorage(key: string, value: string) {
|
||||
localStorage.setItem(key, value)
|
||||
storageCache.set(key, value) // keep cache in sync
|
||||
}
|
||||
```
|
||||
|
||||
Use a Map (not a hook) so it works everywhere: utilities, event handlers, not just React components.
|
||||
|
||||
**Cookie caching:**
|
||||
|
||||
```typescript
|
||||
let cookieCache: Record<string, string> | null = null
|
||||
|
||||
function getCookie(name: string) {
|
||||
if (!cookieCache) {
|
||||
cookieCache = Object.fromEntries(
|
||||
document.cookie.split('; ').map(c => c.split('='))
|
||||
)
|
||||
}
|
||||
return cookieCache[name]
|
||||
}
|
||||
```
|
||||
|
||||
**Important (invalidate on external changes):**
|
||||
|
||||
If storage can change externally (another tab, server-set cookies), invalidate cache:
|
||||
|
||||
```typescript
|
||||
window.addEventListener('storage', (e) => {
|
||||
if (e.key) storageCache.delete(e.key)
|
||||
})
|
||||
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
storageCache.clear()
|
||||
}
|
||||
})
|
||||
```
|
||||
@@ -0,0 +1,32 @@
|
||||
---
|
||||
title: Combine Multiple Array Iterations
|
||||
impact: LOW-MEDIUM
|
||||
impactDescription: reduces iterations
|
||||
tags: javascript, arrays, loops, performance
|
||||
---
|
||||
|
||||
## Combine Multiple Array Iterations
|
||||
|
||||
Multiple `.filter()` or `.map()` calls iterate the array multiple times. Combine into one loop.
|
||||
|
||||
**Incorrect (3 iterations):**
|
||||
|
||||
```typescript
|
||||
const admins = users.filter(u => u.isAdmin)
|
||||
const testers = users.filter(u => u.isTester)
|
||||
const inactive = users.filter(u => !u.isActive)
|
||||
```
|
||||
|
||||
**Correct (1 iteration):**
|
||||
|
||||
```typescript
|
||||
const admins: User[] = []
|
||||
const testers: User[] = []
|
||||
const inactive: User[] = []
|
||||
|
||||
for (const user of users) {
|
||||
if (user.isAdmin) admins.push(user)
|
||||
if (user.isTester) testers.push(user)
|
||||
if (!user.isActive) inactive.push(user)
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
title: Early Return from Functions
|
||||
impact: LOW-MEDIUM
|
||||
impactDescription: avoids unnecessary computation
|
||||
tags: javascript, functions, optimization, early-return
|
||||
---
|
||||
|
||||
## Early Return from Functions
|
||||
|
||||
Return early when result is determined to skip unnecessary processing.
|
||||
|
||||
**Incorrect (processes all items even after finding answer):**
|
||||
|
||||
```typescript
|
||||
function validateUsers(users: User[]) {
|
||||
let hasError = false
|
||||
let errorMessage = ''
|
||||
|
||||
for (const user of users) {
|
||||
if (!user.email) {
|
||||
hasError = true
|
||||
errorMessage = 'Email required'
|
||||
}
|
||||
if (!user.name) {
|
||||
hasError = true
|
||||
errorMessage = 'Name required'
|
||||
}
|
||||
// Continues checking all users even after error found
|
||||
}
|
||||
|
||||
return hasError ? { valid: false, error: errorMessage } : { valid: true }
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (returns immediately on first error):**
|
||||
|
||||
```typescript
|
||||
function validateUsers(users: User[]) {
|
||||
for (const user of users) {
|
||||
if (!user.email) {
|
||||
return { valid: false, error: 'Email required' }
|
||||
}
|
||||
if (!user.name) {
|
||||
return { valid: false, error: 'Name required' }
|
||||
}
|
||||
}
|
||||
|
||||
return { valid: true }
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: Hoist RegExp Creation
|
||||
impact: LOW-MEDIUM
|
||||
impactDescription: avoids recreation
|
||||
tags: javascript, regexp, optimization, memoization
|
||||
---
|
||||
|
||||
## Hoist RegExp Creation
|
||||
|
||||
Don't create RegExp inside render. Hoist to module scope or memoize with `useMemo()`.
|
||||
|
||||
**Incorrect (new RegExp every render):**
|
||||
|
||||
```tsx
|
||||
function Highlighter({ text, query }: Props) {
|
||||
const regex = new RegExp(`(${query})`, 'gi')
|
||||
const parts = text.split(regex)
|
||||
return <>{parts.map((part, i) => ...)}</>
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (memoize or hoist):**
|
||||
|
||||
```tsx
|
||||
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
|
||||
function Highlighter({ text, query }: Props) {
|
||||
const regex = useMemo(
|
||||
() => new RegExp(`(${escapeRegex(query)})`, 'gi'),
|
||||
[query]
|
||||
)
|
||||
const parts = text.split(regex)
|
||||
return <>{parts.map((part, i) => ...)}</>
|
||||
}
|
||||
```
|
||||
|
||||
**Warning (global regex has mutable state):**
|
||||
|
||||
Global regex (`/g`) has mutable `lastIndex` state:
|
||||
|
||||
```typescript
|
||||
const regex = /foo/g
|
||||
regex.test('foo') // true, lastIndex = 3
|
||||
regex.test('foo') // false, lastIndex = 0
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
---
|
||||
title: Build Index Maps for Repeated Lookups
|
||||
impact: LOW-MEDIUM
|
||||
impactDescription: 1M ops to 2K ops
|
||||
tags: javascript, map, indexing, optimization, performance
|
||||
---
|
||||
|
||||
## Build Index Maps for Repeated Lookups
|
||||
|
||||
Multiple `.find()` calls by the same key should use a Map.
|
||||
|
||||
**Incorrect (O(n) per lookup):**
|
||||
|
||||
```typescript
|
||||
function processOrders(orders: Order[], users: User[]) {
|
||||
return orders.map(order => ({
|
||||
...order,
|
||||
user: users.find(u => u.id === order.userId)
|
||||
}))
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (O(1) per lookup):**
|
||||
|
||||
```typescript
|
||||
function processOrders(orders: Order[], users: User[]) {
|
||||
const userById = new Map(users.map(u => [u.id, u]))
|
||||
|
||||
return orders.map(order => ({
|
||||
...order,
|
||||
user: userById.get(order.userId)
|
||||
}))
|
||||
}
|
||||
```
|
||||
|
||||
Build map once (O(n)), then all lookups are O(1).
|
||||
For 1000 orders × 1000 users: 1M ops → 2K ops.
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: Early Length Check for Array Comparisons
|
||||
impact: MEDIUM-HIGH
|
||||
impactDescription: avoids expensive operations when lengths differ
|
||||
tags: javascript, arrays, performance, optimization, comparison
|
||||
---
|
||||
|
||||
## Early Length Check for Array Comparisons
|
||||
|
||||
When comparing arrays with expensive operations (sorting, deep equality, serialization), check lengths first. If lengths differ, the arrays cannot be equal.
|
||||
|
||||
In real-world applications, this optimization is especially valuable when the comparison runs in hot paths (event handlers, render loops).
|
||||
|
||||
**Incorrect (always runs expensive comparison):**
|
||||
|
||||
```typescript
|
||||
function hasChanges(current: string[], original: string[]) {
|
||||
// Always sorts and joins, even when lengths differ
|
||||
return current.sort().join() !== original.sort().join()
|
||||
}
|
||||
```
|
||||
|
||||
Two O(n log n) sorts run even when `current.length` is 5 and `original.length` is 100. There is also overhead of joining the arrays and comparing the strings.
|
||||
|
||||
**Correct (O(1) length check first):**
|
||||
|
||||
```typescript
|
||||
function hasChanges(current: string[], original: string[]) {
|
||||
// Early return if lengths differ
|
||||
if (current.length !== original.length) {
|
||||
return true
|
||||
}
|
||||
// Only sort when lengths match
|
||||
const currentSorted = current.toSorted()
|
||||
const originalSorted = original.toSorted()
|
||||
for (let i = 0; i < currentSorted.length; i++) {
|
||||
if (currentSorted[i] !== originalSorted[i]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
```
|
||||
|
||||
This new approach is more efficient because:
|
||||
- It avoids the overhead of sorting and joining the arrays when lengths differ
|
||||
- It avoids consuming memory for the joined strings (especially important for large arrays)
|
||||
- It avoids mutating the original arrays
|
||||
- It returns early when a difference is found
|
||||
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: Use Loop for Min/Max Instead of Sort
|
||||
impact: LOW
|
||||
impactDescription: O(n) instead of O(n log n)
|
||||
tags: javascript, arrays, performance, sorting, algorithms
|
||||
---
|
||||
|
||||
## Use Loop for Min/Max Instead of Sort
|
||||
|
||||
Finding the smallest or largest element only requires a single pass through the array. Sorting is wasteful and slower.
|
||||
|
||||
**Incorrect (O(n log n) - sort to find latest):**
|
||||
|
||||
```typescript
|
||||
interface Project {
|
||||
id: string
|
||||
name: string
|
||||
updatedAt: number
|
||||
}
|
||||
|
||||
function getLatestProject(projects: Project[]) {
|
||||
const sorted = [...projects].sort((a, b) => b.updatedAt - a.updatedAt)
|
||||
return sorted[0]
|
||||
}
|
||||
```
|
||||
|
||||
Sorts the entire array just to find the maximum value.
|
||||
|
||||
**Incorrect (O(n log n) - sort for oldest and newest):**
|
||||
|
||||
```typescript
|
||||
function getOldestAndNewest(projects: Project[]) {
|
||||
const sorted = [...projects].sort((a, b) => a.updatedAt - b.updatedAt)
|
||||
return { oldest: sorted[0], newest: sorted[sorted.length - 1] }
|
||||
}
|
||||
```
|
||||
|
||||
Still sorts unnecessarily when only min/max are needed.
|
||||
|
||||
**Correct (O(n) - single loop):**
|
||||
|
||||
```typescript
|
||||
function getLatestProject(projects: Project[]) {
|
||||
if (projects.length === 0) return null
|
||||
|
||||
let latest = projects[0]
|
||||
|
||||
for (let i = 1; i < projects.length; i++) {
|
||||
if (projects[i].updatedAt > latest.updatedAt) {
|
||||
latest = projects[i]
|
||||
}
|
||||
}
|
||||
|
||||
return latest
|
||||
}
|
||||
|
||||
function getOldestAndNewest(projects: Project[]) {
|
||||
if (projects.length === 0) return { oldest: null, newest: null }
|
||||
|
||||
let oldest = projects[0]
|
||||
let newest = projects[0]
|
||||
|
||||
for (let i = 1; i < projects.length; i++) {
|
||||
if (projects[i].updatedAt < oldest.updatedAt) oldest = projects[i]
|
||||
if (projects[i].updatedAt > newest.updatedAt) newest = projects[i]
|
||||
}
|
||||
|
||||
return { oldest, newest }
|
||||
}
|
||||
```
|
||||
|
||||
Single pass through the array, no copying, no sorting.
|
||||
|
||||
**Alternative (Math.min/Math.max for small arrays):**
|
||||
|
||||
```typescript
|
||||
const numbers = [5, 2, 8, 1, 9]
|
||||
const min = Math.min(...numbers)
|
||||
const max = Math.max(...numbers)
|
||||
```
|
||||
|
||||
This works for small arrays, but can be slower or just throw an error for very large arrays due to spread operator limitations. Maximal array length is approximately 124000 in Chrome 143 and 638000 in Safari 18; exact numbers may vary - see [the fiddle](https://jsfiddle.net/qw1jabsx/4/). Use the loop approach for reliability.
|
||||
@@ -0,0 +1,24 @@
|
||||
---
|
||||
title: Use Set/Map for O(1) Lookups
|
||||
impact: LOW-MEDIUM
|
||||
impactDescription: O(n) to O(1)
|
||||
tags: javascript, set, map, data-structures, performance
|
||||
---
|
||||
|
||||
## Use Set/Map for O(1) Lookups
|
||||
|
||||
Convert arrays to Set/Map for repeated membership checks.
|
||||
|
||||
**Incorrect (O(n) per check):**
|
||||
|
||||
```typescript
|
||||
const allowedIds = ['a', 'b', 'c', ...]
|
||||
items.filter(item => allowedIds.includes(item.id))
|
||||
```
|
||||
|
||||
**Correct (O(1) per check):**
|
||||
|
||||
```typescript
|
||||
const allowedIds = new Set(['a', 'b', 'c', ...])
|
||||
items.filter(item => allowedIds.has(item.id))
|
||||
```
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
title: Use toSorted() Instead of sort() for Immutability
|
||||
impact: MEDIUM-HIGH
|
||||
impactDescription: prevents mutation bugs in React state
|
||||
tags: javascript, arrays, immutability, react, state, mutation
|
||||
---
|
||||
|
||||
## Use toSorted() Instead of sort() for Immutability
|
||||
|
||||
`.sort()` mutates the array in place, which can cause bugs with React state and props. Use `.toSorted()` to create a new sorted array without mutation.
|
||||
|
||||
**Incorrect (mutates original array):**
|
||||
|
||||
```typescript
|
||||
function UserList({ users }: { users: User[] }) {
|
||||
// Mutates the users prop array!
|
||||
const sorted = useMemo(
|
||||
() => users.sort((a, b) => a.name.localeCompare(b.name)),
|
||||
[users]
|
||||
)
|
||||
return <div>{sorted.map(renderUser)}</div>
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (creates new array):**
|
||||
|
||||
```typescript
|
||||
function UserList({ users }: { users: User[] }) {
|
||||
// Creates new sorted array, original unchanged
|
||||
const sorted = useMemo(
|
||||
() => users.toSorted((a, b) => a.name.localeCompare(b.name)),
|
||||
[users]
|
||||
)
|
||||
return <div>{sorted.map(renderUser)}</div>
|
||||
}
|
||||
```
|
||||
|
||||
**Why this matters in React:**
|
||||
|
||||
1. Props/state mutations break React's immutability model - React expects props and state to be treated as read-only
|
||||
2. Causes stale closure bugs - Mutating arrays inside closures (callbacks, effects) can lead to unexpected behavior
|
||||
|
||||
**Browser support (fallback for older browsers):**
|
||||
|
||||
`.toSorted()` is available in all modern browsers (Chrome 110+, Safari 16+, Firefox 115+, Node.js 20+). For older environments, use spread operator:
|
||||
|
||||
```typescript
|
||||
// Fallback for older browsers
|
||||
const sorted = [...items].sort((a, b) => a.value - b.value)
|
||||
```
|
||||
|
||||
**Other immutable array methods:**
|
||||
|
||||
- `.toSorted()` - immutable sort
|
||||
- `.toReversed()` - immutable reverse
|
||||
- `.toSpliced()` - immutable splice
|
||||
- `.with()` - immutable element replacement
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
title: Use Activity Component for Show/Hide
|
||||
impact: MEDIUM
|
||||
impactDescription: preserves state/DOM
|
||||
tags: rendering, activity, visibility, state-preservation
|
||||
---
|
||||
|
||||
## Use Activity Component for Show/Hide
|
||||
|
||||
Use React's `<Activity>` to preserve state/DOM for expensive components that frequently toggle visibility.
|
||||
|
||||
**Usage:**
|
||||
|
||||
```tsx
|
||||
import { Activity } from 'react'
|
||||
|
||||
function Dropdown({ isOpen }: Props) {
|
||||
return (
|
||||
<Activity mode={isOpen ? 'visible' : 'hidden'}>
|
||||
<ExpensiveMenu />
|
||||
</Activity>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
Avoids expensive re-renders and state loss.
|
||||
@@ -0,0 +1,47 @@
|
||||
---
|
||||
title: Animate SVG Wrapper Instead of SVG Element
|
||||
impact: LOW
|
||||
impactDescription: enables hardware acceleration
|
||||
tags: rendering, svg, css, animation, performance
|
||||
---
|
||||
|
||||
## Animate SVG Wrapper Instead of SVG Element
|
||||
|
||||
Many browsers don't have hardware acceleration for CSS3 animations on SVG elements. Wrap SVG in a `<div>` and animate the wrapper instead.
|
||||
|
||||
**Incorrect (animating SVG directly - no hardware acceleration):**
|
||||
|
||||
```tsx
|
||||
function LoadingSpinner() {
|
||||
return (
|
||||
<svg
|
||||
className="animate-spin"
|
||||
width="24"
|
||||
height="24"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<circle cx="12" cy="12" r="10" stroke="currentColor" />
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (animating wrapper div - hardware accelerated):**
|
||||
|
||||
```tsx
|
||||
function LoadingSpinner() {
|
||||
return (
|
||||
<div className="animate-spin">
|
||||
<svg
|
||||
width="24"
|
||||
height="24"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<circle cx="12" cy="12" r="10" stroke="currentColor" />
|
||||
</svg>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
This applies to all CSS transforms and transitions (`transform`, `opacity`, `translate`, `scale`, `rotate`). The wrapper div allows browsers to use GPU acceleration for smoother animations.
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
title: Use Explicit Conditional Rendering
|
||||
impact: LOW
|
||||
impactDescription: prevents rendering 0 or NaN
|
||||
tags: rendering, conditional, jsx, falsy-values
|
||||
---
|
||||
|
||||
## Use Explicit Conditional Rendering
|
||||
|
||||
Use explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be `0`, `NaN`, or other falsy values that render.
|
||||
|
||||
**Incorrect (renders "0" when count is 0):**
|
||||
|
||||
```tsx
|
||||
function Badge({ count }: { count: number }) {
|
||||
return (
|
||||
<div>
|
||||
{count && <span className="badge">{count}</span>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// When count = 0, renders: <div>0</div>
|
||||
// When count = 5, renders: <div><span class="badge">5</span></div>
|
||||
```
|
||||
|
||||
**Correct (renders nothing when count is 0):**
|
||||
|
||||
```tsx
|
||||
function Badge({ count }: { count: number }) {
|
||||
return (
|
||||
<div>
|
||||
{count > 0 ? <span className="badge">{count}</span> : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// When count = 0, renders: <div></div>
|
||||
// When count = 5, renders: <div><span class="badge">5</span></div>
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: CSS content-visibility for Long Lists
|
||||
impact: HIGH
|
||||
impactDescription: faster initial render
|
||||
tags: rendering, css, content-visibility, long-lists
|
||||
---
|
||||
|
||||
## CSS content-visibility for Long Lists
|
||||
|
||||
Apply `content-visibility: auto` to defer off-screen rendering.
|
||||
|
||||
**CSS:**
|
||||
|
||||
```css
|
||||
.message-item {
|
||||
content-visibility: auto;
|
||||
contain-intrinsic-size: 0 80px;
|
||||
}
|
||||
```
|
||||
|
||||
**Example:**
|
||||
|
||||
```tsx
|
||||
function MessageList({ messages }: { messages: Message[] }) {
|
||||
return (
|
||||
<div className="overflow-y-auto h-screen">
|
||||
{messages.map(msg => (
|
||||
<div key={msg.id} className="message-item">
|
||||
<Avatar user={msg.author} />
|
||||
<div>{msg.content}</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
For 1000 messages, browser skips layout/paint for ~990 off-screen items (10× faster initial render).
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
title: Hoist Static JSX Elements
|
||||
impact: LOW
|
||||
impactDescription: avoids re-creation
|
||||
tags: rendering, jsx, static, optimization
|
||||
---
|
||||
|
||||
## Hoist Static JSX Elements
|
||||
|
||||
Extract static JSX outside components to avoid re-creation.
|
||||
|
||||
**Incorrect (recreates element every render):**
|
||||
|
||||
```tsx
|
||||
function LoadingSkeleton() {
|
||||
return <div className="animate-pulse h-20 bg-gray-200" />
|
||||
}
|
||||
|
||||
function Container() {
|
||||
return (
|
||||
<div>
|
||||
{loading && <LoadingSkeleton />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (reuses same element):**
|
||||
|
||||
```tsx
|
||||
const loadingSkeleton = (
|
||||
<div className="animate-pulse h-20 bg-gray-200" />
|
||||
)
|
||||
|
||||
function Container() {
|
||||
return (
|
||||
<div>
|
||||
{loading && loadingSkeleton}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
This is especially helpful for large and static SVG nodes, which can be expensive to recreate on every render.
|
||||
|
||||
**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler automatically hoists static JSX elements and optimizes component re-renders, making manual hoisting unnecessary.
|
||||
@@ -0,0 +1,82 @@
|
||||
---
|
||||
title: Prevent Hydration Mismatch Without Flickering
|
||||
impact: MEDIUM
|
||||
impactDescription: avoids visual flicker and hydration errors
|
||||
tags: rendering, ssr, hydration, localStorage, flicker
|
||||
---
|
||||
|
||||
## Prevent Hydration Mismatch Without Flickering
|
||||
|
||||
When rendering content that depends on client-side storage (localStorage, cookies), avoid both SSR breakage and post-hydration flickering by injecting a synchronous script that updates the DOM before React hydrates.
|
||||
|
||||
**Incorrect (breaks SSR):**
|
||||
|
||||
```tsx
|
||||
function ThemeWrapper({ children }: { children: ReactNode }) {
|
||||
// localStorage is not available on server - throws error
|
||||
const theme = localStorage.getItem('theme') || 'light'
|
||||
|
||||
return (
|
||||
<div className={theme}>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
Server-side rendering will fail because `localStorage` is undefined.
|
||||
|
||||
**Incorrect (visual flickering):**
|
||||
|
||||
```tsx
|
||||
function ThemeWrapper({ children }: { children: ReactNode }) {
|
||||
const [theme, setTheme] = useState('light')
|
||||
|
||||
useEffect(() => {
|
||||
// Runs after hydration - causes visible flash
|
||||
const stored = localStorage.getItem('theme')
|
||||
if (stored) {
|
||||
setTheme(stored)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className={theme}>
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
Component first renders with default value (`light`), then updates after hydration, causing a visible flash of incorrect content.
|
||||
|
||||
**Correct (no flicker, no hydration mismatch):**
|
||||
|
||||
```tsx
|
||||
function ThemeWrapper({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<>
|
||||
<div id="theme-wrapper">
|
||||
{children}
|
||||
</div>
|
||||
<script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `
|
||||
(function() {
|
||||
try {
|
||||
var theme = localStorage.getItem('theme') || 'light';
|
||||
var el = document.getElementById('theme-wrapper');
|
||||
if (el) el.className = theme;
|
||||
} catch (e) {}
|
||||
})();
|
||||
`,
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
The inline script executes synchronously before showing the element, ensuring the DOM already has the correct value. No flickering, no hydration mismatch.
|
||||
|
||||
This pattern is especially useful for theme toggles, user preferences, authentication states, and any client-only data that should render immediately without flashing default values.
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: Optimize SVG Precision
|
||||
impact: LOW
|
||||
impactDescription: reduces file size
|
||||
tags: rendering, svg, optimization, svgo
|
||||
---
|
||||
|
||||
## Optimize SVG Precision
|
||||
|
||||
Reduce SVG coordinate precision to decrease file size. The optimal precision depends on the viewBox size, but in general reducing precision should be considered.
|
||||
|
||||
**Incorrect (excessive precision):**
|
||||
|
||||
```svg
|
||||
<path d="M 10.293847 20.847362 L 30.938472 40.192837" />
|
||||
```
|
||||
|
||||
**Correct (1 decimal place):**
|
||||
|
||||
```svg
|
||||
<path d="M 10.3 20.8 L 30.9 40.2" />
|
||||
```
|
||||
|
||||
**Automate with SVGO:**
|
||||
|
||||
```bash
|
||||
npx svgo --precision=1 --multipass icon.svg
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
title: Defer State Reads to Usage Point
|
||||
impact: MEDIUM
|
||||
impactDescription: avoids unnecessary subscriptions
|
||||
tags: rerender, searchParams, localStorage, optimization
|
||||
---
|
||||
|
||||
## Defer State Reads to Usage Point
|
||||
|
||||
Don't subscribe to dynamic state (searchParams, localStorage) if you only read it inside callbacks.
|
||||
|
||||
**Incorrect (subscribes to all searchParams changes):**
|
||||
|
||||
```tsx
|
||||
function ShareButton({ chatId }: { chatId: string }) {
|
||||
const searchParams = useSearchParams()
|
||||
|
||||
const handleShare = () => {
|
||||
const ref = searchParams.get('ref')
|
||||
shareChat(chatId, { ref })
|
||||
}
|
||||
|
||||
return <button onClick={handleShare}>Share</button>
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (reads on demand, no subscription):**
|
||||
|
||||
```tsx
|
||||
function ShareButton({ chatId }: { chatId: string }) {
|
||||
const handleShare = () => {
|
||||
const params = new URLSearchParams(window.location.search)
|
||||
const ref = params.get('ref')
|
||||
shareChat(chatId, { ref })
|
||||
}
|
||||
|
||||
return <button onClick={handleShare}>Share</button>
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: Narrow Effect Dependencies
|
||||
impact: LOW
|
||||
impactDescription: minimizes effect re-runs
|
||||
tags: rerender, useEffect, dependencies, optimization
|
||||
---
|
||||
|
||||
## Narrow Effect Dependencies
|
||||
|
||||
Specify primitive dependencies instead of objects to minimize effect re-runs.
|
||||
|
||||
**Incorrect (re-runs on any user field change):**
|
||||
|
||||
```tsx
|
||||
useEffect(() => {
|
||||
console.log(user.id)
|
||||
}, [user])
|
||||
```
|
||||
|
||||
**Correct (re-runs only when id changes):**
|
||||
|
||||
```tsx
|
||||
useEffect(() => {
|
||||
console.log(user.id)
|
||||
}, [user.id])
|
||||
```
|
||||
|
||||
**For derived state, compute outside effect:**
|
||||
|
||||
```tsx
|
||||
// Incorrect: runs on width=767, 766, 765...
|
||||
useEffect(() => {
|
||||
if (width < 768) {
|
||||
enableMobileMode()
|
||||
}
|
||||
}, [width])
|
||||
|
||||
// Correct: runs only on boolean transition
|
||||
const isMobile = width < 768
|
||||
useEffect(() => {
|
||||
if (isMobile) {
|
||||
enableMobileMode()
|
||||
}
|
||||
}, [isMobile])
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: Subscribe to Derived State
|
||||
impact: MEDIUM
|
||||
impactDescription: reduces re-render frequency
|
||||
tags: rerender, derived-state, media-query, optimization
|
||||
---
|
||||
|
||||
## Subscribe to Derived State
|
||||
|
||||
Subscribe to derived boolean state instead of continuous values to reduce re-render frequency.
|
||||
|
||||
**Incorrect (re-renders on every pixel change):**
|
||||
|
||||
```tsx
|
||||
function Sidebar() {
|
||||
const width = useWindowWidth() // updates continuously
|
||||
const isMobile = width < 768
|
||||
return <nav className={isMobile ? 'mobile' : 'desktop'} />
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (re-renders only when boolean changes):**
|
||||
|
||||
```tsx
|
||||
function Sidebar() {
|
||||
const isMobile = useMediaQuery('(max-width: 767px)')
|
||||
return <nav className={isMobile ? 'mobile' : 'desktop'} />
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,74 @@
|
||||
---
|
||||
title: Use Functional setState Updates
|
||||
impact: MEDIUM
|
||||
impactDescription: prevents stale closures and unnecessary callback recreations
|
||||
tags: react, hooks, useState, useCallback, callbacks, closures
|
||||
---
|
||||
|
||||
## Use Functional setState Updates
|
||||
|
||||
When updating state based on the current state value, use the functional update form of setState instead of directly referencing the state variable. This prevents stale closures, eliminates unnecessary dependencies, and creates stable callback references.
|
||||
|
||||
**Incorrect (requires state as dependency):**
|
||||
|
||||
```tsx
|
||||
function TodoList() {
|
||||
const [items, setItems] = useState(initialItems)
|
||||
|
||||
// Callback must depend on items, recreated on every items change
|
||||
const addItems = useCallback((newItems: Item[]) => {
|
||||
setItems([...items, ...newItems])
|
||||
}, [items]) // ❌ items dependency causes recreations
|
||||
|
||||
// Risk of stale closure if dependency is forgotten
|
||||
const removeItem = useCallback((id: string) => {
|
||||
setItems(items.filter(item => item.id !== id))
|
||||
}, []) // ❌ Missing items dependency - will use stale items!
|
||||
|
||||
return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />
|
||||
}
|
||||
```
|
||||
|
||||
The first callback is recreated every time `items` changes, which can cause child components to re-render unnecessarily. The second callback has a stale closure bug—it will always reference the initial `items` value.
|
||||
|
||||
**Correct (stable callbacks, no stale closures):**
|
||||
|
||||
```tsx
|
||||
function TodoList() {
|
||||
const [items, setItems] = useState(initialItems)
|
||||
|
||||
// Stable callback, never recreated
|
||||
const addItems = useCallback((newItems: Item[]) => {
|
||||
setItems(curr => [...curr, ...newItems])
|
||||
}, []) // ✅ No dependencies needed
|
||||
|
||||
// Always uses latest state, no stale closure risk
|
||||
const removeItem = useCallback((id: string) => {
|
||||
setItems(curr => curr.filter(item => item.id !== id))
|
||||
}, []) // ✅ Safe and stable
|
||||
|
||||
return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />
|
||||
}
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
|
||||
1. **Stable callback references** - Callbacks don't need to be recreated when state changes
|
||||
2. **No stale closures** - Always operates on the latest state value
|
||||
3. **Fewer dependencies** - Simplifies dependency arrays and reduces memory leaks
|
||||
4. **Prevents bugs** - Eliminates the most common source of React closure bugs
|
||||
|
||||
**When to use functional updates:**
|
||||
|
||||
- Any setState that depends on the current state value
|
||||
- Inside useCallback/useMemo when state is needed
|
||||
- Event handlers that reference state
|
||||
- Async operations that update state
|
||||
|
||||
**When direct updates are fine:**
|
||||
|
||||
- Setting state to a static value: `setCount(0)`
|
||||
- Setting state from props/arguments only: `setName(newName)`
|
||||
- State doesn't depend on previous value
|
||||
|
||||
**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler can automatically optimize some cases, but functional updates are still recommended for correctness and to prevent stale closure bugs.
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
title: Use Lazy State Initialization
|
||||
impact: MEDIUM
|
||||
impactDescription: wasted computation on every render
|
||||
tags: react, hooks, useState, performance, initialization
|
||||
---
|
||||
|
||||
## Use Lazy State Initialization
|
||||
|
||||
Pass a function to `useState` for expensive initial values. Without the function form, the initializer runs on every render even though the value is only used once.
|
||||
|
||||
**Incorrect (runs on every render):**
|
||||
|
||||
```tsx
|
||||
function FilteredList({ items }: { items: Item[] }) {
|
||||
// buildSearchIndex() runs on EVERY render, even after initialization
|
||||
const [searchIndex, setSearchIndex] = useState(buildSearchIndex(items))
|
||||
const [query, setQuery] = useState('')
|
||||
|
||||
// When query changes, buildSearchIndex runs again unnecessarily
|
||||
return <SearchResults index={searchIndex} query={query} />
|
||||
}
|
||||
|
||||
function UserProfile() {
|
||||
// JSON.parse runs on every render
|
||||
const [settings, setSettings] = useState(
|
||||
JSON.parse(localStorage.getItem('settings') || '{}')
|
||||
)
|
||||
|
||||
return <SettingsForm settings={settings} onChange={setSettings} />
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (runs only once):**
|
||||
|
||||
```tsx
|
||||
function FilteredList({ items }: { items: Item[] }) {
|
||||
// buildSearchIndex() runs ONLY on initial render
|
||||
const [searchIndex, setSearchIndex] = useState(() => buildSearchIndex(items))
|
||||
const [query, setQuery] = useState('')
|
||||
|
||||
return <SearchResults index={searchIndex} query={query} />
|
||||
}
|
||||
|
||||
function UserProfile() {
|
||||
// JSON.parse runs only on initial render
|
||||
const [settings, setSettings] = useState(() => {
|
||||
const stored = localStorage.getItem('settings')
|
||||
return stored ? JSON.parse(stored) : {}
|
||||
})
|
||||
|
||||
return <SettingsForm settings={settings} onChange={setSettings} />
|
||||
}
|
||||
```
|
||||
|
||||
Use lazy initialization when computing initial values from localStorage/sessionStorage, building data structures (indexes, maps), reading from the DOM, or performing heavy transformations.
|
||||
|
||||
For simple primitives (`useState(0)`), direct references (`useState(props.value)`), or cheap literals (`useState({})`), the function form is unnecessary.
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
title: Extract to Memoized Components
|
||||
impact: MEDIUM
|
||||
impactDescription: enables early returns
|
||||
tags: rerender, memo, useMemo, optimization
|
||||
---
|
||||
|
||||
## Extract to Memoized Components
|
||||
|
||||
Extract expensive work into memoized components to enable early returns before computation.
|
||||
|
||||
**Incorrect (computes avatar even when loading):**
|
||||
|
||||
```tsx
|
||||
function Profile({ user, loading }: Props) {
|
||||
const avatar = useMemo(() => {
|
||||
const id = computeAvatarId(user)
|
||||
return <Avatar id={id} />
|
||||
}, [user])
|
||||
|
||||
if (loading) return <Skeleton />
|
||||
return <div>{avatar}</div>
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (skips computation when loading):**
|
||||
|
||||
```tsx
|
||||
const UserAvatar = memo(function UserAvatar({ user }: { user: User }) {
|
||||
const id = useMemo(() => computeAvatarId(user), [user])
|
||||
return <Avatar id={id} />
|
||||
})
|
||||
|
||||
function Profile({ user, loading }: Props) {
|
||||
if (loading) return <Skeleton />
|
||||
return (
|
||||
<div>
|
||||
<UserAvatar user={user} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, manual memoization with `memo()` and `useMemo()` is not necessary. The compiler automatically optimizes re-renders.
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
title: Use Transitions for Non-Urgent Updates
|
||||
impact: MEDIUM
|
||||
impactDescription: maintains UI responsiveness
|
||||
tags: rerender, transitions, startTransition, performance
|
||||
---
|
||||
|
||||
## Use Transitions for Non-Urgent Updates
|
||||
|
||||
Mark frequent, non-urgent state updates as transitions to maintain UI responsiveness.
|
||||
|
||||
**Incorrect (blocks UI on every scroll):**
|
||||
|
||||
```tsx
|
||||
function ScrollTracker() {
|
||||
const [scrollY, setScrollY] = useState(0)
|
||||
useEffect(() => {
|
||||
const handler = () => setScrollY(window.scrollY)
|
||||
window.addEventListener('scroll', handler, { passive: true })
|
||||
return () => window.removeEventListener('scroll', handler)
|
||||
}, [])
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (non-blocking updates):**
|
||||
|
||||
```tsx
|
||||
import { startTransition } from 'react'
|
||||
|
||||
function ScrollTracker() {
|
||||
const [scrollY, setScrollY] = useState(0)
|
||||
useEffect(() => {
|
||||
const handler = () => {
|
||||
startTransition(() => setScrollY(window.scrollY))
|
||||
}
|
||||
window.addEventListener('scroll', handler, { passive: true })
|
||||
return () => window.removeEventListener('scroll', handler)
|
||||
}, [])
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,73 @@
|
||||
---
|
||||
title: Use after() for Non-Blocking Operations
|
||||
impact: MEDIUM
|
||||
impactDescription: faster response times
|
||||
tags: server, async, logging, analytics, side-effects
|
||||
---
|
||||
|
||||
## Use after() for Non-Blocking Operations
|
||||
|
||||
Use Next.js's `after()` to schedule work that should execute after a response is sent. This prevents logging, analytics, and other side effects from blocking the response.
|
||||
|
||||
**Incorrect (blocks response):**
|
||||
|
||||
```tsx
|
||||
import { logUserAction } from '@/app/utils'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
// Perform mutation
|
||||
await updateDatabase(request)
|
||||
|
||||
// Logging blocks the response
|
||||
const userAgent = request.headers.get('user-agent') || 'unknown'
|
||||
await logUserAction({ userAgent })
|
||||
|
||||
return new Response(JSON.stringify({ status: 'success' }), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (non-blocking):**
|
||||
|
||||
```tsx
|
||||
import { after } from 'next/server'
|
||||
import { headers, cookies } from 'next/headers'
|
||||
import { logUserAction } from '@/app/utils'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
// Perform mutation
|
||||
await updateDatabase(request)
|
||||
|
||||
// Log after response is sent
|
||||
after(async () => {
|
||||
const userAgent = (await headers()).get('user-agent') || 'unknown'
|
||||
const sessionCookie = (await cookies()).get('session-id')?.value || 'anonymous'
|
||||
|
||||
logUserAction({ sessionCookie, userAgent })
|
||||
})
|
||||
|
||||
return new Response(JSON.stringify({ status: 'success' }), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
The response is sent immediately while logging happens in the background.
|
||||
|
||||
**Common use cases:**
|
||||
|
||||
- Analytics tracking
|
||||
- Audit logging
|
||||
- Sending notifications
|
||||
- Cache invalidation
|
||||
- Cleanup tasks
|
||||
|
||||
**Important notes:**
|
||||
|
||||
- `after()` runs even if the response fails or redirects
|
||||
- Works in Server Actions, Route Handlers, and Server Components
|
||||
|
||||
Reference: [https://nextjs.org/docs/app/api-reference/functions/after](https://nextjs.org/docs/app/api-reference/functions/after)
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
title: Cross-Request LRU Caching
|
||||
impact: HIGH
|
||||
impactDescription: caches across requests
|
||||
tags: server, cache, lru, cross-request
|
||||
---
|
||||
|
||||
## Cross-Request LRU Caching
|
||||
|
||||
`React.cache()` only works within one request. For data shared across sequential requests (user clicks button A then button B), use an LRU cache.
|
||||
|
||||
**Implementation:**
|
||||
|
||||
```typescript
|
||||
import { LRUCache } from 'lru-cache'
|
||||
|
||||
const cache = new LRUCache<string, any>({
|
||||
max: 1000,
|
||||
ttl: 5 * 60 * 1000 // 5 minutes
|
||||
})
|
||||
|
||||
export async function getUser(id: string) {
|
||||
const cached = cache.get(id)
|
||||
if (cached) return cached
|
||||
|
||||
const user = await db.user.findUnique({ where: { id } })
|
||||
cache.set(id, user)
|
||||
return user
|
||||
}
|
||||
|
||||
// Request 1: DB query, result cached
|
||||
// Request 2: cache hit, no DB query
|
||||
```
|
||||
|
||||
Use when sequential user actions hit multiple endpoints needing the same data within seconds.
|
||||
|
||||
**With Vercel's [Fluid Compute](https://vercel.com/docs/fluid-compute):** LRU caching is especially effective because multiple concurrent requests can share the same function instance and cache. This means the cache persists across requests without needing external storage like Redis.
|
||||
|
||||
**In traditional serverless:** Each invocation runs in isolation, so consider Redis for cross-process caching.
|
||||
|
||||
Reference: [https://github.com/isaacs/node-lru-cache](https://github.com/isaacs/node-lru-cache)
|
||||
@@ -0,0 +1,76 @@
|
||||
---
|
||||
title: Per-Request Deduplication with React.cache()
|
||||
impact: MEDIUM
|
||||
impactDescription: deduplicates within request
|
||||
tags: server, cache, react-cache, deduplication
|
||||
---
|
||||
|
||||
## Per-Request Deduplication with React.cache()
|
||||
|
||||
Use `React.cache()` for server-side request deduplication. Authentication and database queries benefit most.
|
||||
|
||||
**Usage:**
|
||||
|
||||
```typescript
|
||||
import { cache } from 'react'
|
||||
|
||||
export const getCurrentUser = cache(async () => {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
return await db.user.findUnique({
|
||||
where: { id: session.user.id }
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
Within a single request, multiple calls to `getCurrentUser()` execute the query only once.
|
||||
|
||||
**Avoid inline objects as arguments:**
|
||||
|
||||
`React.cache()` uses shallow equality (`Object.is`) to determine cache hits. Inline objects create new references each call, preventing cache hits.
|
||||
|
||||
**Incorrect (always cache miss):**
|
||||
|
||||
```typescript
|
||||
const getUser = cache(async (params: { uid: number }) => {
|
||||
return await db.user.findUnique({ where: { id: params.uid } })
|
||||
})
|
||||
|
||||
// Each call creates new object, never hits cache
|
||||
getUser({ uid: 1 })
|
||||
getUser({ uid: 1 }) // Cache miss, runs query again
|
||||
```
|
||||
|
||||
**Correct (cache hit):**
|
||||
|
||||
```typescript
|
||||
const getUser = cache(async (uid: number) => {
|
||||
return await db.user.findUnique({ where: { id: uid } })
|
||||
})
|
||||
|
||||
// Primitive args use value equality
|
||||
getUser(1)
|
||||
getUser(1) // Cache hit, returns cached result
|
||||
```
|
||||
|
||||
If you must pass objects, pass the same reference:
|
||||
|
||||
```typescript
|
||||
const params = { uid: 1 }
|
||||
getUser(params) // Query runs
|
||||
getUser(params) // Cache hit (same reference)
|
||||
```
|
||||
|
||||
**Next.js-Specific Note:**
|
||||
|
||||
In Next.js, the `fetch` API is automatically extended with request memoization. Requests with the same URL and options are automatically deduplicated within a single request, so you don't need `React.cache()` for `fetch` calls. However, `React.cache()` is still essential for other async tasks:
|
||||
|
||||
- Database queries (Prisma, Drizzle, etc.)
|
||||
- Heavy computations
|
||||
- Authentication checks
|
||||
- File system operations
|
||||
- Any non-fetch async work
|
||||
|
||||
Use `React.cache()` to deduplicate these operations across your component tree.
|
||||
|
||||
Reference: [React.cache documentation](https://react.dev/reference/react/cache)
|
||||
@@ -0,0 +1,83 @@
|
||||
---
|
||||
title: Parallel Data Fetching with Component Composition
|
||||
impact: CRITICAL
|
||||
impactDescription: eliminates server-side waterfalls
|
||||
tags: server, rsc, parallel-fetching, composition
|
||||
---
|
||||
|
||||
## Parallel Data Fetching with Component Composition
|
||||
|
||||
React Server Components execute sequentially within a tree. Restructure with composition to parallelize data fetching.
|
||||
|
||||
**Incorrect (Sidebar waits for Page's fetch to complete):**
|
||||
|
||||
```tsx
|
||||
export default async function Page() {
|
||||
const header = await fetchHeader()
|
||||
return (
|
||||
<div>
|
||||
<div>{header}</div>
|
||||
<Sidebar />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
async function Sidebar() {
|
||||
const items = await fetchSidebarItems()
|
||||
return <nav>{items.map(renderItem)}</nav>
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (both fetch simultaneously):**
|
||||
|
||||
```tsx
|
||||
async function Header() {
|
||||
const data = await fetchHeader()
|
||||
return <div>{data}</div>
|
||||
}
|
||||
|
||||
async function Sidebar() {
|
||||
const items = await fetchSidebarItems()
|
||||
return <nav>{items.map(renderItem)}</nav>
|
||||
}
|
||||
|
||||
export default function Page() {
|
||||
return (
|
||||
<div>
|
||||
<Header />
|
||||
<Sidebar />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
**Alternative with children prop:**
|
||||
|
||||
```tsx
|
||||
async function Header() {
|
||||
const data = await fetchHeader()
|
||||
return <div>{data}</div>
|
||||
}
|
||||
|
||||
async function Sidebar() {
|
||||
const items = await fetchSidebarItems()
|
||||
return <nav>{items.map(renderItem)}</nav>
|
||||
}
|
||||
|
||||
function Layout({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<div>
|
||||
<Header />
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function Page() {
|
||||
return (
|
||||
<Layout>
|
||||
<Sidebar />
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: Minimize Serialization at RSC Boundaries
|
||||
impact: HIGH
|
||||
impactDescription: reduces data transfer size
|
||||
tags: server, rsc, serialization, props
|
||||
---
|
||||
|
||||
## Minimize Serialization at RSC Boundaries
|
||||
|
||||
The React Server/Client boundary serializes all object properties into strings and embeds them in the HTML response and subsequent RSC requests. This serialized data directly impacts page weight and load time, so **size matters a lot**. Only pass fields that the client actually uses.
|
||||
|
||||
**Incorrect (serializes all 50 fields):**
|
||||
|
||||
```tsx
|
||||
async function Page() {
|
||||
const user = await fetchUser() // 50 fields
|
||||
return <Profile user={user} />
|
||||
}
|
||||
|
||||
'use client'
|
||||
function Profile({ user }: { user: User }) {
|
||||
return <div>{user.name}</div> // uses 1 field
|
||||
}
|
||||
```
|
||||
|
||||
**Correct (serializes only 1 field):**
|
||||
|
||||
```tsx
|
||||
async function Page() {
|
||||
const user = await fetchUser()
|
||||
return <Profile name={user.name} />
|
||||
}
|
||||
|
||||
'use client'
|
||||
function Profile({ name }: { name: string }) {
|
||||
return <div>{name}</div>
|
||||
}
|
||||
```
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../.agents
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../.agents
|
||||
@@ -307,9 +307,6 @@ OPENAI_API_KEY=sk-xxxxxxxxx
|
||||
# Shared between Better-Auth and Next-Auth
|
||||
# AUTH_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
|
||||
# Auth URL (accessible from browser, optional if same domain)
|
||||
# NEXT_PUBLIC_AUTH_URL=http://localhost:3210
|
||||
|
||||
# Require email verification before allowing users to sign in (default: false)
|
||||
# Set to '1' to force users to verify their email before signing in
|
||||
# NEXT_PUBLIC_AUTH_EMAIL_VERIFICATION=0
|
||||
|
||||
@@ -43,9 +43,6 @@ NEXT_PUBLIC_ENABLE_BETTER_AUTH=1
|
||||
# Better Auth secret for JWT signing (generate with: openssl rand -base64 32)
|
||||
AUTH_SECRET=${UNSAFE_SECRET}
|
||||
|
||||
# Authentication URL
|
||||
NEXT_PUBLIC_AUTH_URL=${APP_URL}
|
||||
|
||||
# SSO providers configuration - using Casdoor for development
|
||||
AUTH_SSO_PROVIDERS=casdoor
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
# 统一使用 LF 行尾符(与 Mac/Linux 一致)
|
||||
* text=auto eol=lf
|
||||
|
||||
# 确保这些文件类型始终使用 LF
|
||||
*.ts text eol=lf
|
||||
*.tsx text eol=lf
|
||||
*.js text eol=lf
|
||||
*.jsx text eol=lf
|
||||
*.json text eol=lf
|
||||
*.md text eol=lf
|
||||
*.mdx text eol=lf
|
||||
*.yml text eol=lf
|
||||
*.yaml text eol=lf
|
||||
*.toml text eol=lf
|
||||
*.css text eol=lf
|
||||
*.scss text eol=lf
|
||||
*.html text eol=lf
|
||||
*.sh text eol=lf
|
||||
|
||||
# 二进制文件
|
||||
*.png binary
|
||||
*.jpg binary
|
||||
*.jpeg binary
|
||||
*.gif binary
|
||||
*.ico binary
|
||||
*.webp binary
|
||||
*.svg binary
|
||||
*.woff binary
|
||||
*.woff2 binary
|
||||
*.ttf binary
|
||||
*.eot binary
|
||||
*.mp4 binary
|
||||
*.mp3 binary
|
||||
*.zip binary
|
||||
*.gz binary
|
||||
@@ -108,6 +108,12 @@ CLAUDE.local.md
|
||||
# MCP tools
|
||||
.serena/**
|
||||
|
||||
# Migration scripts data
|
||||
scripts/clerk-to-betterauth/test/*.csv
|
||||
scripts/clerk-to-betterauth/test/*.json
|
||||
scripts/clerk-to-betterauth/prod/*.csv
|
||||
scripts/clerk-to-betterauth/prod/*.json
|
||||
|
||||
# Misc
|
||||
./packages/lobe-ui
|
||||
*.ppt*
|
||||
@@ -117,3 +123,5 @@ e2e/reports
|
||||
out
|
||||
i18n-unused-keys-report.json
|
||||
.vitest-reports
|
||||
|
||||
pnpm-lock.yaml
|
||||
+793
@@ -2,6 +2,799 @@
|
||||
|
||||
# Changelog
|
||||
|
||||
## [Version 2.0.0-next.339](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.338...v2.0.0-next.339)
|
||||
|
||||
<sup>Released on **2026-01-22**</sup>
|
||||
|
||||
#### ♻ Code Refactoring
|
||||
|
||||
- **misc**: Move vercel-react-best-practices skills to .agents directory.
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **misc**: Skill setting page and skill store.
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **model-runtime**: Filter unsupported image types (SVG) before sending to vision models.
|
||||
- **misc**: Fix group broadcast trigger tool use, fix local system tools.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### Code refactoring
|
||||
|
||||
- **misc**: Move vercel-react-best-practices skills to .agents directory, closes [#11703](https://github.com/lobehub/lobe-chat/issues/11703) ([6df7731](https://github.com/lobehub/lobe-chat/commit/6df7731))
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **misc**: Skill setting page and skill store, closes [#11665](https://github.com/lobehub/lobe-chat/issues/11665) ([d8c0c26](https://github.com/lobehub/lobe-chat/commit/d8c0c26))
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **model-runtime**: Filter unsupported image types (SVG) before sending to vision models, closes [#11698](https://github.com/lobehub/lobe-chat/issues/11698) ([c0c99a7](https://github.com/lobehub/lobe-chat/commit/c0c99a7))
|
||||
- **misc**: Fix group broadcast trigger tool use, closes [#11646](https://github.com/lobehub/lobe-chat/issues/11646) ([831a9b3](https://github.com/lobehub/lobe-chat/commit/831a9b3))
|
||||
- **misc**: Fix local system tools, closes [#11702](https://github.com/lobehub/lobe-chat/issues/11702) ([6548fc7](https://github.com/lobehub/lobe-chat/commit/6548fc7))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.338](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.337...v2.0.0-next.338)
|
||||
|
||||
<sup>Released on **2026-01-22**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Updata cron job ui & fixed commnuity pagenation goto error.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Updata cron job ui & fixed commnuity pagenation goto error, closes [#11700](https://github.com/lobehub/lobe-chat/issues/11700) ([42ad2a0](https://github.com/lobehub/lobe-chat/commit/42ad2a0))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.337](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.336...v2.0.0-next.337)
|
||||
|
||||
<sup>Released on **2026-01-22**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Fix memory schema, update the agentbuilder tools not always use humanIntervention.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Fix memory schema, closes [#11645](https://github.com/lobehub/lobe-chat/issues/11645) ([3baf780](https://github.com/lobehub/lobe-chat/commit/3baf780))
|
||||
- **misc**: Update the agentbuilder tools not always use humanIntervention, closes [#11696](https://github.com/lobehub/lobe-chat/issues/11696) ([0d3017b](https://github.com/lobehub/lobe-chat/commit/0d3017b))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.336](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.335...v2.0.0-next.336)
|
||||
|
||||
<sup>Released on **2026-01-22**</sup>
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **misc**: Support agent group unpublish agents.
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Fix tool argument scape and improve multi task run.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **misc**: Support agent group unpublish agents, closes [#11687](https://github.com/lobehub/lobe-chat/issues/11687) ([4e060be](https://github.com/lobehub/lobe-chat/commit/4e060be))
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Fix tool argument scape and improve multi task run, closes [#11691](https://github.com/lobehub/lobe-chat/issues/11691) ([b13bb8a](https://github.com/lobehub/lobe-chat/commit/b13bb8a))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.335](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.334...v2.0.0-next.335)
|
||||
|
||||
<sup>Released on **2026-01-22**</sup>
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **database**: Added user memory activity.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **database**: Added user memory activity, closes [#11680](https://github.com/lobehub/lobe-chat/issues/11680) ([0160fbd](https://github.com/lobehub/lobe-chat/commit/0160fbd))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.334](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.333...v2.0.0-next.334)
|
||||
|
||||
<sup>Released on **2026-01-21**</sup>
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **misc**: Add platform-aware download client menu option.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **misc**: Add platform-aware download client menu option, closes [#11676](https://github.com/lobehub/lobe-chat/issues/11676) ([55abddc](https://github.com/lobehub/lobe-chat/commit/55abddc))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.333](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.332...v2.0.0-next.333)
|
||||
|
||||
<sup>Released on **2026-01-21**</sup>
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **desktop**: Add legacy local database detection and migration guidance.
|
||||
- **misc**: Update the sandbox preinstall libs in sys role.
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Fix multi tasks no summary issue.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **desktop**: Add legacy local database detection and migration guidance, closes [#11682](https://github.com/lobehub/lobe-chat/issues/11682) ([5664b84](https://github.com/lobehub/lobe-chat/commit/5664b84))
|
||||
- **misc**: Update the sandbox preinstall libs in sys role, closes [#11688](https://github.com/lobehub/lobe-chat/issues/11688) ([404c577](https://github.com/lobehub/lobe-chat/commit/404c577))
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Fix multi tasks no summary issue, closes [#11685](https://github.com/lobehub/lobe-chat/issues/11685) ([26ce317](https://github.com/lobehub/lobe-chat/commit/26ce317))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.332](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.331...v2.0.0-next.332)
|
||||
|
||||
<sup>Released on **2026-01-21**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Improve e2e server and complete i18n resources.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Improve e2e server and complete i18n resources, closes [#11678](https://github.com/lobehub/lobe-chat/issues/11678) ([d450dd9](https://github.com/lobehub/lobe-chat/commit/d450dd9))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.331](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.330...v2.0.0-next.331)
|
||||
|
||||
<sup>Released on **2026-01-21**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Slove the agent group editor not focus in editdata area.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Slove the agent group editor not focus in editdata area, closes [#11677](https://github.com/lobehub/lobe-chat/issues/11677) ([9ac84e6](https://github.com/lobehub/lobe-chat/commit/9ac84e6))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.330](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.329...v2.0.0-next.330)
|
||||
|
||||
<sup>Released on **2026-01-21**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Fix multi agent tasks issue.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Fix multi agent tasks issue, closes [#11672](https://github.com/lobehub/lobe-chat/issues/11672) ([9de773b](https://github.com/lobehub/lobe-chat/commit/9de773b))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.329](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.328...v2.0.0-next.329)
|
||||
|
||||
<sup>Released on **2026-01-21**</sup>
|
||||
|
||||
#### ♻ Code Refactoring
|
||||
|
||||
- **auth**: Remove NEXT_PUBLIC_AUTH_URL env variable.
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Sloved the old removeSessionTopics not work.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### Code refactoring
|
||||
|
||||
- **auth**: Remove NEXT_PUBLIC_AUTH_URL env variable, closes [#11658](https://github.com/lobehub/lobe-chat/issues/11658) ([c0f9875](https://github.com/lobehub/lobe-chat/commit/c0f9875))
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Sloved the old removeSessionTopics not work, closes [#11671](https://github.com/lobehub/lobe-chat/issues/11671) ([06d41e5](https://github.com/lobehub/lobe-chat/commit/06d41e5))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.328](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.327...v2.0.0-next.328)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **misc**: Support client tasks mode.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **misc**: Support client tasks mode, closes [#11666](https://github.com/lobehub/lobe-chat/issues/11666) ([98cf57b](https://github.com/lobehub/lobe-chat/commit/98cf57b))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.327](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.326...v2.0.0-next.327)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### ♻ Code Refactoring
|
||||
|
||||
- **model-select**: Migrate FunctionCallingModelSelect to LobeSelect.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### Code refactoring
|
||||
|
||||
- **model-select**: Migrate FunctionCallingModelSelect to LobeSelect, closes [#11664](https://github.com/lobehub/lobe-chat/issues/11664) ([ad51305](https://github.com/lobehub/lobe-chat/commit/ad51305))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.326](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.325...v2.0.0-next.326)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **desktop**: Gracefully handle missing update manifest 404 errors.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **desktop**: Gracefully handle missing update manifest 404 errors, closes [#11625](https://github.com/lobehub/lobe-chat/issues/11625) ([13e95b9](https://github.com/lobehub/lobe-chat/commit/13e95b9))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.325](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.324...v2.0.0-next.325)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### ♻ Code Refactoring
|
||||
|
||||
- **ModelSwitchPanel**: Migrate from Popover to DropdownMenu with virtual scrolling.
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **sidebar-drawer**: Fix drawer positioning and title style.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### Code refactoring
|
||||
|
||||
- **ModelSwitchPanel**: Migrate from Popover to DropdownMenu with virtual scrolling, closes [#11663](https://github.com/lobehub/lobe-chat/issues/11663) ([c9d9dff](https://github.com/lobehub/lobe-chat/commit/c9d9dff))
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **sidebar-drawer**: Fix drawer positioning and title style, closes [#11655](https://github.com/lobehub/lobe-chat/issues/11655) ([cf5320e](https://github.com/lobehub/lobe-chat/commit/cf5320e))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.324](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.323...v2.0.0-next.324)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: TypewriterEffect not refreshing on language change.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: TypewriterEffect not refreshing on language change, closes [#11657](https://github.com/lobehub/lobe-chat/issues/11657) ([ba30f46](https://github.com/lobehub/lobe-chat/commit/ba30f46))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.323](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.322...v2.0.0-next.323)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### ♻ Code Refactoring
|
||||
|
||||
- **misc**: Optimize lobehub models and default configuration.
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **misc**: Add the agents and agents group fork feature.
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **model-runtime**: Fix Qwen parallel tool calls arguments incorrectly merged.
|
||||
- **topic**: Correct topic item href route for agent and group pages.
|
||||
- **misc**: Fix Topic component causing stack overflow and freezing the app, simplify updater config logic, slove the nuqs error in commnuity agent group page.
|
||||
|
||||
#### 💄 Styles
|
||||
|
||||
- **misc**: Optimize profile settings skeleton screen.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### Code refactoring
|
||||
|
||||
- **misc**: Optimize lobehub models and default configuration, closes [#11621](https://github.com/lobehub/lobe-chat/issues/11621) ([5074fbe](https://github.com/lobehub/lobe-chat/commit/5074fbe))
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **misc**: Add the agents and agents group fork feature, closes [#11652](https://github.com/lobehub/lobe-chat/issues/11652) ([b1c3b83](https://github.com/lobehub/lobe-chat/commit/b1c3b83))
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **model-runtime**: Fix Qwen parallel tool calls arguments incorrectly merged, closes [#11649](https://github.com/lobehub/lobe-chat/issues/11649) ([ddbe661](https://github.com/lobehub/lobe-chat/commit/ddbe661))
|
||||
- **topic**: Correct topic item href route for agent and group pages, closes [#11607](https://github.com/lobehub/lobe-chat/issues/11607) ([2fffe8b](https://github.com/lobehub/lobe-chat/commit/2fffe8b))
|
||||
- **misc**: Fix Topic component causing stack overflow and freezing the app, closes [#11609](https://github.com/lobehub/lobe-chat/issues/11609) ([600cb85](https://github.com/lobehub/lobe-chat/commit/600cb85))
|
||||
- **misc**: Simplify updater config logic, closes [#11636](https://github.com/lobehub/lobe-chat/issues/11636) ([5c645f0](https://github.com/lobehub/lobe-chat/commit/5c645f0))
|
||||
- **misc**: Slove the nuqs error in commnuity agent group page, closes [#11651](https://github.com/lobehub/lobe-chat/issues/11651) ([1c29bca](https://github.com/lobehub/lobe-chat/commit/1c29bca))
|
||||
|
||||
#### Styles
|
||||
|
||||
- **misc**: Optimize profile settings skeleton screen, closes [#11656](https://github.com/lobehub/lobe-chat/issues/11656) ([e61ae85](https://github.com/lobehub/lobe-chat/commit/e61ae85))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.322](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.321...v2.0.0-next.322)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **memory-user-memory**: Should fallback to server configured provider & model.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **memory-user-memory**: Should fallback to server configured provider & model, closes [#11643](https://github.com/lobehub/lobe-chat/issues/11643) ([af446d9](https://github.com/lobehub/lobe-chat/commit/af446d9))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.321](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.320...v2.0.0-next.321)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **memory-user-memory**: Support to configure preferred model.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **memory-user-memory**: Support to configure preferred model, closes [#11637](https://github.com/lobehub/lobe-chat/issues/11637) ([49374da](https://github.com/lobehub/lobe-chat/commit/49374da))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.320](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.319...v2.0.0-next.320)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **ShareModal**: Wrap ShareMessageModal with Provider in context menu.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **ShareModal**: Wrap ShareMessageModal with Provider in context menu, closes [#11434](https://github.com/lobehub/lobe-chat/issues/11434) [#11382](https://github.com/lobehub/lobe-chat/issues/11382) ([0d30e5f](https://github.com/lobehub/lobe-chat/commit/0d30e5f))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.319](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.318...v2.0.0-next.319)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Slove commnuity user avatarUrl is wrong, should update others in profile.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Slove commnuity user avatarUrl is wrong, should update others in profile, closes [#11634](https://github.com/lobehub/lobe-chat/issues/11634) ([04465c8](https://github.com/lobehub/lobe-chat/commit/04465c8))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.318](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.317...v2.0.0-next.318)
|
||||
|
||||
<sup>Released on **2026-01-20**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Fix parallel tools calling race issue.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Fix parallel tools calling race issue, closes [#11626](https://github.com/lobehub/lobe-chat/issues/11626) ([34bdcd4](https://github.com/lobehub/lobe-chat/commit/34bdcd4))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.317](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.316...v2.0.0-next.317)
|
||||
|
||||
<sup>Released on **2026-01-19**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **desktop**: Resolve onboarding navigation issues after logout.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **desktop**: Resolve onboarding navigation issues after logout, closes [#11628](https://github.com/lobehub/lobe-chat/issues/11628) ([05a0873](https://github.com/lobehub/lobe-chat/commit/05a0873))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.316](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.315...v2.0.0-next.316)
|
||||
|
||||
<sup>Released on **2026-01-19**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: When use trpc client should include the credentials cookies.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: When use trpc client should include the credentials cookies, closes [#11629](https://github.com/lobehub/lobe-chat/issues/11629) ([8ece553](https://github.com/lobehub/lobe-chat/commit/8ece553))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.315](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.314...v2.0.0-next.315)
|
||||
|
||||
<sup>Released on **2026-01-19**</sup>
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **misc**: Add the cloudEndpoint & Klavis Tools Call in Excuation Task.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **misc**: Add the cloudEndpoint & Klavis Tools Call in Excuation Task, closes [#11627](https://github.com/lobehub/lobe-chat/issues/11627) ([0ffe6c4](https://github.com/lobehub/lobe-chat/commit/0ffe6c4))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.314](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.313...v2.0.0-next.314)
|
||||
|
||||
<sup>Released on **2026-01-19**</sup>
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **misc**: Improve desktop onboarding window management and footer actions.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **misc**: Improve desktop onboarding window management and footer actions, closes [#11619](https://github.com/lobehub/lobe-chat/issues/11619) ([6ed280e](https://github.com/lobehub/lobe-chat/commit/6ed280e))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.313](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.312...v2.0.0-next.313)
|
||||
|
||||
<sup>Released on **2026-01-19**</sup>
|
||||
|
||||
#### 🐛 Bug Fixes
|
||||
|
||||
- **misc**: Fix server agent task run with headless, internlm provider base url and homepage.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### What's fixed
|
||||
|
||||
- **misc**: Fix server agent task run with headless, closes [#11600](https://github.com/lobehub/lobe-chat/issues/11600) ([435eede](https://github.com/lobehub/lobe-chat/commit/435eede))
|
||||
- **misc**: Internlm provider base url and homepage, closes [#11612](https://github.com/lobehub/lobe-chat/issues/11612) ([38725da](https://github.com/lobehub/lobe-chat/commit/38725da))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.312](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.311...v2.0.0-next.312)
|
||||
|
||||
<sup>Released on **2026-01-19**</sup>
|
||||
|
||||
#### ♻ Code Refactoring
|
||||
|
||||
- **misc**: Change the /community/assistant to /agent routes.
|
||||
|
||||
#### ✨ Features
|
||||
|
||||
- **misc**: Improve the agentbuilder systemRole.
|
||||
|
||||
<br/>
|
||||
|
||||
<details>
|
||||
<summary><kbd>Improvements and Fixes</kbd></summary>
|
||||
|
||||
#### Code refactoring
|
||||
|
||||
- **misc**: Change the /community/assistant to /agent routes, closes [#11606](https://github.com/lobehub/lobe-chat/issues/11606) ([7f004c5](https://github.com/lobehub/lobe-chat/commit/7f004c5))
|
||||
|
||||
#### What's improved
|
||||
|
||||
- **misc**: Improve the agentbuilder systemRole, closes [#11608](https://github.com/lobehub/lobe-chat/issues/11608) ([2f032d4](https://github.com/lobehub/lobe-chat/commit/2f032d4))
|
||||
|
||||
</details>
|
||||
|
||||
<div align="right">
|
||||
|
||||
[](#readme-top)
|
||||
|
||||
</div>
|
||||
|
||||
## [Version 2.0.0-next.311](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.310...v2.0.0-next.311)
|
||||
|
||||
<sup>Released on **2026-01-19**</sup>
|
||||
|
||||
@@ -32,6 +32,10 @@ This repository adopts a monorepo structure.
|
||||
|
||||
see @.cursor/rules/typescript.mdc
|
||||
|
||||
### Code Comments
|
||||
|
||||
- **Avoid meaningless comments**: Do not write comments that merely restate what the code does. Comments should explain _why_ something is done, not _what_ is being done. The code itself should be self-explanatory.
|
||||
|
||||
### Testing
|
||||
|
||||
- **Required Rule**: read `.cursor/rules/testing-guide/testing-guide.mdc` before writing tests
|
||||
|
||||
+1
-2
@@ -189,8 +189,7 @@ ENV KEY_VAULTS_SECRET="" \
|
||||
|
||||
# Better Auth
|
||||
ENV AUTH_SECRET="" \
|
||||
AUTH_SSO_PROVIDERS="" \
|
||||
NEXT_PUBLIC_AUTH_URL=""
|
||||
AUTH_SSO_PROVIDERS=""
|
||||
|
||||
# Clerk
|
||||
ENV CLERK_SECRET_KEY="" \
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { APP_WINDOW_MIN_SIZE } from '@lobechat/desktop-bridge';
|
||||
|
||||
import type { BrowserWindowOpts } from './core/browser/Browser';
|
||||
|
||||
export const BrowsersIdentifiers = {
|
||||
@@ -11,7 +13,8 @@ export const appBrowsers = {
|
||||
height: 800,
|
||||
identifier: 'app',
|
||||
keepAlive: true,
|
||||
minWidth: 400,
|
||||
minHeight: APP_WINDOW_MIN_SIZE.height,
|
||||
minWidth: APP_WINDOW_MIN_SIZE.width,
|
||||
path: '/',
|
||||
showOnInit: true,
|
||||
titleBarStyle: 'hidden',
|
||||
|
||||
@@ -23,6 +23,9 @@ export const userDataDir = app.getPath('userData');
|
||||
|
||||
export const appStorageDir = join(userDataDir, 'lobehub-storage');
|
||||
|
||||
// Legacy local database directory used in older desktop versions
|
||||
export const legacyLocalDbDir = join(appStorageDir, 'lobehub-local-db');
|
||||
|
||||
// ------ Application storage directory ---- //
|
||||
|
||||
// Local storage files (simulating S3)
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { DataSyncConfig, MarketAuthorizationParams } from '@lobechat/electron-client-ipc';
|
||||
import {
|
||||
AuthorizationPhase,
|
||||
AuthorizationProgress,
|
||||
DataSyncConfig,
|
||||
MarketAuthorizationParams,
|
||||
} from '@lobechat/electron-client-ipc';
|
||||
import { BrowserWindow, shell } from 'electron';
|
||||
import crypto from 'node:crypto';
|
||||
import querystring from 'node:querystring';
|
||||
@@ -9,9 +14,11 @@ import { createLogger } from '@/utils/logger';
|
||||
import RemoteServerConfigCtr from './RemoteServerConfigCtr';
|
||||
import { ControllerModule, IpcMethod } from './index';
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('controllers:AuthCtr');
|
||||
|
||||
const MAX_POLL_TIME = 2 * 60 * 1000; // 2 minutes (reduced from 5 minutes for better UX)
|
||||
const POLL_INTERVAL = 3000; // 3 seconds
|
||||
|
||||
/**
|
||||
* Authentication Controller
|
||||
* Implements OAuth authorization flow using intermediate page + polling mechanism
|
||||
@@ -107,6 +114,12 @@ export default class AuthCtr extends ControllerModule {
|
||||
await shell.openExternal(authUrl.toString());
|
||||
logger.debug('Opening authorization URL in default browser');
|
||||
|
||||
this.broadcastAuthorizationProgress({
|
||||
elapsed: 0,
|
||||
maxPollTime: MAX_POLL_TIME,
|
||||
phase: 'browser_opened',
|
||||
});
|
||||
|
||||
// Start polling for credentials
|
||||
this.startPolling();
|
||||
|
||||
@@ -117,6 +130,24 @@ export default class AuthCtr extends ControllerModule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel current authorization process
|
||||
*/
|
||||
@IpcMethod()
|
||||
async cancelAuthorization() {
|
||||
if (this.authRequestState) {
|
||||
logger.info('User cancelled authorization');
|
||||
this.clearAuthorizationState();
|
||||
this.broadcastAuthorizationProgress({
|
||||
elapsed: 0,
|
||||
maxPollTime: MAX_POLL_TIME,
|
||||
phase: 'cancelled',
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
return { error: 'No active authorization', success: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Request Market OAuth authorization (desktop)
|
||||
*/
|
||||
@@ -152,14 +183,29 @@ export default class AuthCtr extends ControllerModule {
|
||||
}
|
||||
|
||||
logger.info('Starting credential polling');
|
||||
const pollInterval = 3000; // 3 seconds
|
||||
const maxPollTime = 5 * 60 * 1000; // 5 minutes
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
// Broadcast initial state
|
||||
this.broadcastAuthorizationProgress({
|
||||
elapsed: 0,
|
||||
maxPollTime: MAX_POLL_TIME,
|
||||
phase: 'waiting_for_auth',
|
||||
});
|
||||
|
||||
this.pollingInterval = setInterval(async () => {
|
||||
const elapsed = Date.now() - startTime;
|
||||
|
||||
// Broadcast progress on every tick
|
||||
this.broadcastAuthorizationProgress({
|
||||
elapsed,
|
||||
maxPollTime: MAX_POLL_TIME,
|
||||
phase: 'waiting_for_auth',
|
||||
});
|
||||
|
||||
try {
|
||||
// Check if polling has timed out
|
||||
if (Date.now() - startTime > maxPollTime) {
|
||||
if (elapsed > MAX_POLL_TIME) {
|
||||
logger.warn('Credential polling timed out');
|
||||
this.clearAuthorizationState();
|
||||
this.broadcastAuthorizationFailed('Authorization timed out');
|
||||
@@ -173,6 +219,13 @@ export default class AuthCtr extends ControllerModule {
|
||||
logger.info('Successfully received credentials from polling');
|
||||
this.stopPolling();
|
||||
|
||||
// Broadcast verifying state
|
||||
this.broadcastAuthorizationProgress({
|
||||
elapsed,
|
||||
maxPollTime: MAX_POLL_TIME,
|
||||
phase: 'verifying',
|
||||
});
|
||||
|
||||
// Validate state parameter
|
||||
if (result.state !== this.authRequestState) {
|
||||
logger.error(
|
||||
@@ -198,7 +251,7 @@ export default class AuthCtr extends ControllerModule {
|
||||
this.clearAuthorizationState();
|
||||
this.broadcastAuthorizationFailed('Polling error: ' + error.message);
|
||||
}
|
||||
}, pollInterval);
|
||||
}, POLL_INTERVAL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -511,6 +564,21 @@ export default class AuthCtr extends ControllerModule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast authorization progress event
|
||||
*/
|
||||
private broadcastAuthorizationProgress(progress: AuthorizationProgress) {
|
||||
// Avoid logging too frequently
|
||||
// logger.debug('Broadcasting authorizationProgress event');
|
||||
const allWindows = BrowserWindow.getAllWindows();
|
||||
|
||||
for (const win of allWindows) {
|
||||
if (!win.isDestroyed()) {
|
||||
win.webContents.send('authorizationProgress', progress);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast authorization failed event
|
||||
*/
|
||||
@@ -563,7 +631,7 @@ export default class AuthCtr extends ControllerModule {
|
||||
// Hash codeVerifier using SHA-256
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(codeVerifier);
|
||||
const digest = await crypto.subtle.digest('SHA-256', data as unknown as NodeJS.BufferSource);
|
||||
const digest = await crypto.subtle.digest('SHA-256', data.buffer);
|
||||
|
||||
// Convert hash result to base64url encoding
|
||||
const challenge = Buffer.from(digest)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type {
|
||||
InterceptRouteParams,
|
||||
OpenSettingsWindowOptions,
|
||||
WindowResizableParams,
|
||||
WindowMinimumSizeParams,
|
||||
WindowSizeParams,
|
||||
} from '@lobechat/electron-client-ipc';
|
||||
import { findMatchingRoute } from '~common/routes';
|
||||
@@ -81,9 +81,21 @@ export default class BrowserWindowsCtr extends ControllerModule {
|
||||
}
|
||||
|
||||
@IpcMethod()
|
||||
setWindowResizable(params: WindowResizableParams) {
|
||||
setWindowMinimumSize(params: WindowMinimumSizeParams) {
|
||||
this.withSenderIdentifier((identifier) => {
|
||||
this.app.browserManager.setWindowResizable(identifier, params.resizable);
|
||||
const currentSize = this.app.browserManager.getWindowSize(identifier);
|
||||
const nextWindowSize = {
|
||||
...currentSize,
|
||||
};
|
||||
if (params.height) {
|
||||
nextWindowSize.height = Math.max(currentSize.height, params.height);
|
||||
}
|
||||
if (params.width) {
|
||||
nextWindowSize.width = Math.max(currentSize.width, params.width);
|
||||
}
|
||||
|
||||
this.app.browserManager.setWindowSize(identifier, nextWindowSize);
|
||||
this.app.browserManager.setWindowMinimumSize(identifier, params);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { ElectronAppState, ThemeMode } from '@lobechat/electron-client-ipc';
|
||||
import { app, dialog, nativeTheme, shell } from 'electron';
|
||||
import { macOS } from 'electron-is';
|
||||
import { pathExists, readdir } from 'fs-extra';
|
||||
import process from 'node:process';
|
||||
|
||||
import { legacyLocalDbDir } from '@/const/dir';
|
||||
import { createLogger } from '@/utils/logger';
|
||||
import {
|
||||
getAccessibilityStatus,
|
||||
@@ -214,6 +216,23 @@ export default class SystemController extends ControllerModule {
|
||||
return nativeTheme.themeSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect whether user used the legacy local database in older desktop versions.
|
||||
* Legacy path: {app.getPath('userData')}/lobehub-storage/lobehub-local-db
|
||||
*/
|
||||
@IpcMethod()
|
||||
async hasLegacyLocalDb(): Promise<boolean> {
|
||||
if (!(await pathExists(legacyLocalDbDir))) return false;
|
||||
|
||||
try {
|
||||
const entries = await readdir(legacyLocalDbDir);
|
||||
return entries.length > 0;
|
||||
} catch {
|
||||
// If directory exists but cannot be read, treat as "used" to surface guidance.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private async setSystemThemeMode(themeMode: ThemeMode) {
|
||||
nativeTheme.themeSource = themeMode;
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ vi.mock('@/utils/logger', () => ({
|
||||
// Mock electron
|
||||
vi.mock('electron', () => ({
|
||||
app: {
|
||||
getAppPath: vi.fn(() => '/mock/app/path'),
|
||||
getLocale: vi.fn(() => 'en-US'),
|
||||
getPath: vi.fn((name: string) => `/mock/path/${name}`),
|
||||
},
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { TITLE_BAR_HEIGHT } from '@lobechat/desktop-bridge';
|
||||
import { APP_WINDOW_MIN_SIZE, TITLE_BAR_HEIGHT } from '@lobechat/desktop-bridge';
|
||||
import { MainBroadcastEventKey, MainBroadcastParams } from '@lobechat/electron-client-ipc';
|
||||
import {
|
||||
BrowserWindow,
|
||||
BrowserWindowConstructorOptions,
|
||||
Menu,
|
||||
session as electronSession,
|
||||
ipcMain,
|
||||
screen,
|
||||
@@ -11,7 +12,7 @@ import console from 'node:console';
|
||||
import { join } from 'node:path';
|
||||
|
||||
import { preloadDir, resourcesDir } from '@/const/dir';
|
||||
import { isMac } from '@/const/env';
|
||||
import { isDev, isMac } from '@/const/env';
|
||||
import { ELECTRON_BE_PROTOCOL_SCHEME } from '@/const/protocol';
|
||||
import RemoteServerConfigCtr from '@/controllers/RemoteServerConfigCtr';
|
||||
import { backendProxyProtocolManager } from '@/core/infrastructure/BackendProxyProtocolManager';
|
||||
@@ -191,6 +192,7 @@ export default class Browser {
|
||||
this.setupCloseListener(browserWindow);
|
||||
this.setupFocusListener(browserWindow);
|
||||
this.setupWillPreventUnloadListener(browserWindow);
|
||||
this.setupDevContextMenu(browserWindow);
|
||||
}
|
||||
|
||||
private setupWillPreventUnloadListener(browserWindow: BrowserWindow): void {
|
||||
@@ -236,6 +238,43 @@ export default class Browser {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup context menu with "Inspect Element" option in development mode
|
||||
*/
|
||||
private setupDevContextMenu(browserWindow: BrowserWindow): void {
|
||||
if (!isDev) return;
|
||||
|
||||
logger.debug(`[${this.identifier}] Setting up dev context menu.`);
|
||||
|
||||
browserWindow.webContents.on('context-menu', (_event, params) => {
|
||||
const { x, y } = params;
|
||||
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
click: () => {
|
||||
browserWindow.webContents.inspectElement(x, y);
|
||||
},
|
||||
label: 'Inspect Element',
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{
|
||||
click: () => {
|
||||
browserWindow.webContents.openDevTools();
|
||||
},
|
||||
label: 'Open DevTools',
|
||||
},
|
||||
{
|
||||
click: () => {
|
||||
browserWindow.webContents.reload();
|
||||
},
|
||||
label: 'Reload',
|
||||
},
|
||||
]);
|
||||
|
||||
menu.popup({ window: browserWindow });
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== Window Actions ====================
|
||||
|
||||
show(): void {
|
||||
@@ -291,9 +330,19 @@ export default class Browser {
|
||||
});
|
||||
}
|
||||
|
||||
setWindowResizable(resizable: boolean): void {
|
||||
logger.debug(`[${this.identifier}] Setting window resizable: ${resizable}`);
|
||||
this._browserWindow?.setResizable(resizable);
|
||||
setWindowMinimumSize(size: { height?: number; width?: number }): void {
|
||||
logger.debug(`[${this.identifier}] Setting window minimum size: ${JSON.stringify(size)}`);
|
||||
|
||||
const currentMinimumSize = this._browserWindow?.getMinimumSize?.() ?? [0, 0];
|
||||
const rawWidth = size.width ?? currentMinimumSize[0];
|
||||
const rawHeight = size.height ?? currentMinimumSize[1];
|
||||
|
||||
// Electron doesn't "reset" minimum size with 0x0 reliably.
|
||||
// Treat 0 / negative as fallback to app-level default preset.
|
||||
const width = rawWidth > 0 ? rawWidth : APP_WINDOW_MIN_SIZE.width;
|
||||
const height = rawHeight > 0 ? rawHeight : APP_WINDOW_MIN_SIZE.height;
|
||||
|
||||
this._browserWindow?.setMinimumSize?.(width, height);
|
||||
}
|
||||
|
||||
// ==================== Window Position ====================
|
||||
|
||||
@@ -250,9 +250,14 @@ export class BrowserManager {
|
||||
browser?.setWindowSize(size);
|
||||
}
|
||||
|
||||
setWindowResizable(identifier: string, resizable: boolean) {
|
||||
getWindowSize(identifier: string) {
|
||||
const browser = this.browsers.get(identifier);
|
||||
browser?.setWindowResizable(resizable);
|
||||
return browser?.browserWindow.getBounds();
|
||||
}
|
||||
|
||||
setWindowMinimumSize(identifier: string, size: { height?: number; width?: number }) {
|
||||
const browser = this.browsers.get(identifier);
|
||||
browser?.setWindowMinimumSize(size);
|
||||
}
|
||||
|
||||
getIdentifierByWebContents(webContents: WebContents): string | null {
|
||||
|
||||
@@ -149,17 +149,6 @@ export class I18nManager {
|
||||
*/
|
||||
private notifyRendererProcess(lng: string) {
|
||||
logger.debug(`Notifying renderer process of language change: ${lng}`);
|
||||
|
||||
// Send language change event to all windows
|
||||
// const windows = this.app.browserManager.windows;
|
||||
//
|
||||
// if (windows && windows.length > 0) {
|
||||
// windows.forEach((window) => {
|
||||
// if (window?.webContents) {
|
||||
// window.webContents.send('language-changed', lng);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
private async loadLocale(language: string) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { UpdateInfo } from '@lobechat/electron-client-ipc';
|
||||
import { app as electronApp } from 'electron';
|
||||
import log from 'electron-log';
|
||||
import { autoUpdater } from 'electron-updater';
|
||||
|
||||
@@ -14,10 +16,8 @@ import { createLogger } from '@/utils/logger';
|
||||
|
||||
import type { App as AppCore } from '../App';
|
||||
|
||||
// Allow forcing dev update config via env (for testing updates in packaged app)
|
||||
const FORCE_DEV_UPDATE_CONFIG = getDesktopEnv().FORCE_DEV_UPDATE_CONFIG;
|
||||
|
||||
// Create logger
|
||||
const logger = createLogger('core:UpdaterManager');
|
||||
|
||||
export class UpdaterManager {
|
||||
@@ -31,11 +31,10 @@ export class UpdaterManager {
|
||||
constructor(app: AppCore) {
|
||||
this.app = app;
|
||||
|
||||
// 设置日志
|
||||
log.transports.file.level = 'info';
|
||||
autoUpdater.logger = log;
|
||||
|
||||
logger.debug(`[Updater] Log file should be at: ${log.transports.file.getFile().path}`); // 打印路径
|
||||
logger.debug(`[Updater] Log file should be at: ${log.transports.file.getFile().path}`);
|
||||
}
|
||||
|
||||
get mainWindow() {
|
||||
@@ -44,50 +43,36 @@ export class UpdaterManager {
|
||||
|
||||
public initialize = async () => {
|
||||
logger.debug('Initializing UpdaterManager');
|
||||
// If updates are disabled and in production environment, don't initialize updates
|
||||
if (!updaterConfig.enableAppUpdate && !isDev) {
|
||||
|
||||
if (!updaterConfig.enableAppUpdate) {
|
||||
logger.info('App updates are disabled, skipping updater initialization');
|
||||
return;
|
||||
}
|
||||
|
||||
// Configure autoUpdater
|
||||
autoUpdater.autoDownload = false; // Set to false, we'll control downloads manually
|
||||
autoUpdater.autoDownload = false;
|
||||
autoUpdater.autoInstallOnAppQuit = false;
|
||||
autoUpdater.allowDowngrade = false;
|
||||
|
||||
// Enable test mode in development environment or when forced via env
|
||||
// IMPORTANT: This must be set BEFORE channel configuration so that
|
||||
// dev-app-update.yml takes precedence over programmatic configuration
|
||||
const useDevConfig = isDev || FORCE_DEV_UPDATE_CONFIG;
|
||||
if (useDevConfig) {
|
||||
// In dev mode, use dev-app-update.yml for all configuration including channel
|
||||
// Don't set channel here - let dev-app-update.yml control it (defaults to "latest")
|
||||
autoUpdater.forceDevUpdateConfig = true;
|
||||
logger.info(
|
||||
`Using dev update config (isDev=${isDev}, FORCE_DEV_UPDATE_CONFIG=${FORCE_DEV_UPDATE_CONFIG})`,
|
||||
);
|
||||
logger.info('Dev mode: Using dev-app-update.yml for update configuration');
|
||||
} else {
|
||||
// Only configure channel and update provider programmatically in production
|
||||
// Note: channel is configured in configureUpdateProvider based on provider type
|
||||
autoUpdater.allowPrerelease = channel !== 'stable';
|
||||
logger.info(`Production mode: channel=${channel}, allowPrerelease=${channel !== 'stable'}`);
|
||||
this.configureUpdateProvider();
|
||||
}
|
||||
|
||||
// Register events
|
||||
this.registerEvents();
|
||||
|
||||
// If auto-check for updates is configured, set up periodic checks
|
||||
if (updaterConfig.app.autoCheckUpdate) {
|
||||
// Delay update check by 1 minute after startup to avoid network instability
|
||||
setTimeout(() => this.checkForUpdates(), 60 * 1000);
|
||||
|
||||
// Set up periodic checks
|
||||
setInterval(() => this.checkForUpdates(), updaterConfig.app.checkUpdateInterval);
|
||||
}
|
||||
|
||||
// Log the channel and allowPrerelease values
|
||||
logger.debug(
|
||||
`Initialized with channel: ${autoUpdater.channel}, allowPrerelease: ${autoUpdater.allowPrerelease}`,
|
||||
);
|
||||
@@ -105,15 +90,12 @@ export class UpdaterManager {
|
||||
this.checking = true;
|
||||
this.isManualCheck = manual;
|
||||
|
||||
// Ensure allowPrerelease is correctly set before each check
|
||||
// This guards against any internal state reset by electron-updater
|
||||
if (!isStableChannel) {
|
||||
autoUpdater.allowPrerelease = true;
|
||||
}
|
||||
|
||||
logger.info(`${manual ? 'Manually checking' : 'Auto checking'} for updates...`);
|
||||
|
||||
// Log detailed updater configuration for debugging
|
||||
const inferredChannel =
|
||||
autoUpdater.channel ||
|
||||
(autoUpdater.currentVersion?.prerelease?.[0]
|
||||
@@ -133,7 +115,6 @@ export class UpdaterManager {
|
||||
logger.info('[Updater Config] usingFallbackProvider:', this.usingFallbackProvider);
|
||||
logger.info('[Updater Config] GitHub config:', JSON.stringify(githubConfig));
|
||||
|
||||
// If manual check, notify renderer process about check start
|
||||
if (manual) {
|
||||
this.mainWindow.broadcast('manualUpdateCheckStart');
|
||||
}
|
||||
@@ -141,11 +122,22 @@ export class UpdaterManager {
|
||||
try {
|
||||
await autoUpdater.checkForUpdates();
|
||||
} catch (error) {
|
||||
logger.error('Error checking for updates:', error.message);
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
|
||||
// Edge case: Release tag exists but update manifest assets (latest/stable-*.yml) aren't uploaded yet.
|
||||
// Treat this gap period as "no updates available" instead of a user-facing error.
|
||||
if (this.isMissingUpdateManifestError(error)) {
|
||||
logger.warn('[Updater] Update manifest not ready yet, treating as no update:', message);
|
||||
if (manual) {
|
||||
this.mainWindow.broadcast('manualUpdateNotAvailable', this.getCurrentUpdateInfo());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
logger.error('Error checking for updates:', message);
|
||||
|
||||
// If manual check, notify renderer process about check error
|
||||
if (manual) {
|
||||
this.mainWindow.broadcast('updateError', (error as Error).message);
|
||||
this.mainWindow.broadcast('updateError', message);
|
||||
}
|
||||
} finally {
|
||||
this.checking = false;
|
||||
@@ -162,7 +154,6 @@ export class UpdaterManager {
|
||||
this.downloading = true;
|
||||
logger.info(`${manual ? 'Manually downloading' : 'Auto downloading'} update...`);
|
||||
|
||||
// If manual download or manual check, notify renderer process about download start
|
||||
if (manual || this.isManualCheck) {
|
||||
this.mainWindow.broadcast('updateDownloadStart');
|
||||
}
|
||||
@@ -173,7 +164,6 @@ export class UpdaterManager {
|
||||
this.downloading = false;
|
||||
logger.error('Error downloading update:', error);
|
||||
|
||||
// If manual download or manual check, notify renderer process about download error
|
||||
if (manual || this.isManualCheck) {
|
||||
this.mainWindow.broadcast('updateError', (error as Error).message);
|
||||
}
|
||||
@@ -186,14 +176,10 @@ export class UpdaterManager {
|
||||
public installNow = () => {
|
||||
logger.info('Installing update now...');
|
||||
|
||||
// Mark application for exit
|
||||
this.app.isQuiting = true;
|
||||
|
||||
// Close all windows first to ensure clean exit
|
||||
logger.info('Closing all windows before update installation...');
|
||||
const { BrowserWindow, app } = require('electron');
|
||||
// do not close windows and quit first
|
||||
// on Windows, window-all-closed -> app.quit()` can terminate the process before the timer fires
|
||||
if (!isWindows) {
|
||||
const allWindows = BrowserWindow.getAllWindows();
|
||||
allWindows.forEach((window) => {
|
||||
@@ -203,16 +189,10 @@ export class UpdaterManager {
|
||||
});
|
||||
}
|
||||
|
||||
// Release single instance lock before quitting
|
||||
// This ensures the new instance can acquire the lock
|
||||
logger.info('Releasing single instance lock...');
|
||||
app.releaseSingleInstanceLock();
|
||||
|
||||
// Small delay to ensure windows are closed and lock is released
|
||||
setTimeout(() => {
|
||||
// quitAndInstall parameters:
|
||||
// - isSilent: true (don't show installation UI)
|
||||
// - isForceRunAfter: true (force start app after installation)
|
||||
logger.info('Calling autoUpdater.quitAndInstall...');
|
||||
autoUpdater.quitAndInstall(true, true);
|
||||
}, 100);
|
||||
@@ -224,10 +204,7 @@ export class UpdaterManager {
|
||||
public installLater = () => {
|
||||
logger.info('Update will be installed on next restart');
|
||||
|
||||
// Mark for installation on next launch, but don't exit application
|
||||
autoUpdater.autoInstallOnAppQuit = true;
|
||||
|
||||
// Notify renderer process that update will be installed on next launch
|
||||
this.mainWindow.broadcast('updateWillInstallLater');
|
||||
};
|
||||
|
||||
@@ -241,7 +218,6 @@ export class UpdaterManager {
|
||||
logger.info('Simulating update available...');
|
||||
|
||||
const mainWindow = this.mainWindow;
|
||||
// Simulate a new version update
|
||||
const mockUpdateInfo = {
|
||||
releaseDate: new Date().toISOString(),
|
||||
releaseNotes: ` #### Version 1.0.0 Release Notes
|
||||
@@ -253,14 +229,11 @@ export class UpdaterManager {
|
||||
version: '1.0.0',
|
||||
};
|
||||
|
||||
// Set update available state
|
||||
this.updateAvailable = true;
|
||||
|
||||
// Notify renderer process
|
||||
if (this.isManualCheck) {
|
||||
mainWindow.broadcast('manualUpdateAvailable', mockUpdateInfo);
|
||||
} else {
|
||||
// In auto-check mode, directly simulate download
|
||||
this.simulateDownloadProgress();
|
||||
}
|
||||
};
|
||||
@@ -276,7 +249,6 @@ export class UpdaterManager {
|
||||
|
||||
const mainWindow = this.app.browserManager.getMainWindow();
|
||||
if (mainWindow) {
|
||||
// Simulate a new version update
|
||||
const mockUpdateInfo = {
|
||||
releaseDate: new Date().toISOString(),
|
||||
releaseNotes: ` #### Version 1.0.0 Release Notes
|
||||
@@ -288,10 +260,7 @@ export class UpdaterManager {
|
||||
version: '1.0.0',
|
||||
};
|
||||
|
||||
// Set download state
|
||||
this.downloading = false;
|
||||
|
||||
// Notify renderer process
|
||||
mainWindow.broadcast('updateDownloaded', mockUpdateInfo);
|
||||
}
|
||||
};
|
||||
@@ -307,28 +276,22 @@ export class UpdaterManager {
|
||||
|
||||
const mainWindow = this.app.browserManager.getMainWindow();
|
||||
|
||||
// Set download state
|
||||
this.downloading = true;
|
||||
|
||||
// Only broadcast download start event if manual check
|
||||
if (this.isManualCheck) {
|
||||
mainWindow.broadcast('updateDownloadStart');
|
||||
}
|
||||
|
||||
// Simulate progress updates
|
||||
let progress = 0;
|
||||
const interval = setInterval(() => {
|
||||
progress += 10;
|
||||
|
||||
if (
|
||||
progress <= 100 && // Only broadcast download progress if manual check
|
||||
this.isManualCheck
|
||||
) {
|
||||
if (progress <= 100 && this.isManualCheck) {
|
||||
mainWindow.broadcast('updateDownloadProgress', {
|
||||
bytesPerSecond: 1024 * 1024,
|
||||
percent: progress, // 1MB/s
|
||||
total: 1024 * 1024 * 100, // 100MB
|
||||
transferred: 1024 * 1024 * progress, // Progress * 1MB
|
||||
percent: progress,
|
||||
total: 1024 * 1024 * 100,
|
||||
transferred: 1024 * 1024 * progress,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -348,8 +311,6 @@ export class UpdaterManager {
|
||||
*/
|
||||
private configureUpdateProvider() {
|
||||
if (isStableChannel && UPDATE_SERVER_URL && !this.usingFallbackProvider) {
|
||||
// Stable channel uses custom update server (generic HTTP) as primary
|
||||
// S3 has stable-mac.yml, so we set channel to 'stable'
|
||||
autoUpdater.channel = 'stable';
|
||||
logger.info(`Configuring generic provider for stable channel (primary)`);
|
||||
logger.info(`Update server URL: ${UPDATE_SERVER_URL}`);
|
||||
@@ -360,9 +321,6 @@ export class UpdaterManager {
|
||||
url: UPDATE_SERVER_URL,
|
||||
});
|
||||
} else {
|
||||
// GitHub provider:
|
||||
// - stable: use default latest-mac.yml (GitHub uploads latest* only)
|
||||
// - beta/nightly: leave channel unset so prerelease matching uses tag (e.g. next)
|
||||
const reason = this.usingFallbackProvider ? '(fallback from S3)' : '';
|
||||
logger.info(`Configuring GitHub provider for ${channel} channel ${reason}`);
|
||||
if (autoUpdater.channel !== null) {
|
||||
@@ -370,7 +328,6 @@ export class UpdaterManager {
|
||||
}
|
||||
logger.info('Channel left unset (defaults to latest-mac.yml for GitHub)');
|
||||
|
||||
// For beta/nightly channels, we need prerelease versions
|
||||
const needPrerelease = channel !== 'stable';
|
||||
|
||||
autoUpdater.setFeedURL({
|
||||
@@ -379,8 +336,6 @@ export class UpdaterManager {
|
||||
repo: githubConfig.repo,
|
||||
});
|
||||
|
||||
// Ensure allowPrerelease is set correctly after setFeedURL
|
||||
// setFeedURL may reset some internal states
|
||||
autoUpdater.allowPrerelease = needPrerelease;
|
||||
|
||||
logger.info(
|
||||
@@ -394,7 +349,6 @@ export class UpdaterManager {
|
||||
* Called when primary provider (S3) fails
|
||||
*/
|
||||
private switchToFallbackAndRetry = async () => {
|
||||
// Only fallback if we're on stable channel with S3 configured and haven't already fallen back
|
||||
if (!isStableChannel || !UPDATE_SERVER_URL || this.usingFallbackProvider) {
|
||||
return false;
|
||||
}
|
||||
@@ -403,7 +357,6 @@ export class UpdaterManager {
|
||||
this.usingFallbackProvider = true;
|
||||
this.configureUpdateProvider();
|
||||
|
||||
// Retry update check with fallback provider
|
||||
try {
|
||||
await autoUpdater.checkForUpdates();
|
||||
return true;
|
||||
@@ -437,13 +390,11 @@ export class UpdaterManager {
|
||||
logger.info(`Update available: ${info.version}`);
|
||||
this.updateAvailable = true;
|
||||
|
||||
// Reset to primary provider for next check cycle
|
||||
this.resetToPrimaryProvider();
|
||||
|
||||
if (this.isManualCheck) {
|
||||
this.mainWindow.broadcast('manualUpdateAvailable', info);
|
||||
} else {
|
||||
// If it's an automatic check, start downloading automatically
|
||||
logger.info('Auto check found update, starting download automatically...');
|
||||
this.downloadUpdate();
|
||||
}
|
||||
@@ -452,7 +403,6 @@ export class UpdaterManager {
|
||||
autoUpdater.on('update-not-available', (info) => {
|
||||
logger.info(`Update not available. Current: ${info.version}`);
|
||||
|
||||
// Reset to primary provider for next check cycle
|
||||
this.resetToPrimaryProvider();
|
||||
|
||||
if (this.isManualCheck) {
|
||||
@@ -461,8 +411,19 @@ export class UpdaterManager {
|
||||
});
|
||||
|
||||
autoUpdater.on('error', async (err) => {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
|
||||
// Edge case: Release tag exists but update manifest assets aren't uploaded yet.
|
||||
// Skip fallback switching and avoid user-facing errors.
|
||||
if (this.isMissingUpdateManifestError(err)) {
|
||||
logger.warn('[Updater] Update manifest not ready yet, skipping error handling:', message);
|
||||
if (this.isManualCheck) {
|
||||
this.mainWindow.broadcast('manualUpdateNotAvailable', this.getCurrentUpdateInfo());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
logger.error('Error in auto-updater:', err);
|
||||
// Log configuration state when error occurs for debugging
|
||||
logger.error('[Updater Error Context] Channel:', autoUpdater.channel);
|
||||
logger.error('[Updater Error Context] allowPrerelease:', autoUpdater.allowPrerelease);
|
||||
logger.error('[Updater Error Context] Build channel from config:', channel);
|
||||
@@ -471,12 +432,11 @@ export class UpdaterManager {
|
||||
logger.error('[Updater Error Context] usingFallbackProvider:', this.usingFallbackProvider);
|
||||
logger.error('[Updater Error Context] GitHub config:', JSON.stringify(githubConfig));
|
||||
|
||||
// Try fallback to GitHub if S3 failed
|
||||
if (!this.usingFallbackProvider && isStableChannel && UPDATE_SERVER_URL) {
|
||||
logger.info('Attempting fallback to GitHub provider...');
|
||||
const fallbackSucceeded = await this.switchToFallbackAndRetry();
|
||||
if (fallbackSucceeded) {
|
||||
return; // Fallback initiated, don't report error yet
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,10 +457,33 @@ export class UpdaterManager {
|
||||
autoUpdater.on('update-downloaded', (info) => {
|
||||
logger.info(`Update downloaded: ${info.version}`);
|
||||
this.downloading = false;
|
||||
// Always notify about downloaded update
|
||||
this.mainWindow.broadcast('updateDownloaded', info);
|
||||
});
|
||||
|
||||
logger.debug('Updater events registered');
|
||||
}
|
||||
|
||||
private isMissingUpdateManifestError(error: unknown): boolean {
|
||||
const message = error instanceof Error ? error.message : String(error ?? '');
|
||||
if (!message) return false;
|
||||
|
||||
// Expect patterns like:
|
||||
// - "Cannot find latest-mac.yml ... HttpError: 404 ..."
|
||||
// - "Cannot find stable.yml ... 404 ..."
|
||||
if (!/cannot find/i.test(message)) return false;
|
||||
if (!/\b404\b/.test(message)) return false;
|
||||
|
||||
// Match channel manifest filenames across platforms/architectures:
|
||||
// latest.yml, latest-mac.yml, latest-linux.yml, stable.yml, stable-mac.yml, etc.
|
||||
const manifestMatch = message.match(/\b(?:latest|stable)(?:-[\da-z]+)?\.yml\b/i);
|
||||
return Boolean(manifestMatch);
|
||||
}
|
||||
|
||||
private getCurrentUpdateInfo(): UpdateInfo {
|
||||
const version = autoUpdater.currentVersion?.version || electronApp.getVersion();
|
||||
return {
|
||||
releaseDate: new Date().toISOString(),
|
||||
version,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ vi.mock('electron-updater', () => ({
|
||||
autoInstallOnAppQuit: false,
|
||||
channel: 'stable',
|
||||
checkForUpdates: vi.fn(),
|
||||
currentVersion: undefined as any,
|
||||
downloadUpdate: vi.fn(),
|
||||
forceDevUpdateConfig: false,
|
||||
logger: null as any,
|
||||
@@ -46,6 +47,7 @@ vi.mock('electron', () => ({
|
||||
getAllWindows: mockGetAllWindows,
|
||||
},
|
||||
app: {
|
||||
getVersion: vi.fn().mockReturnValue('0.0.0'),
|
||||
releaseSingleInstanceLock: mockReleaseSingleInstanceLock,
|
||||
},
|
||||
}));
|
||||
@@ -76,7 +78,6 @@ vi.mock('@/modules/updater/configs', () => ({
|
||||
checkUpdateInterval: 60 * 60 * 1000,
|
||||
},
|
||||
enableAppUpdate: true,
|
||||
enableRenderHotUpdate: true,
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -109,6 +110,7 @@ describe('UpdaterManager', () => {
|
||||
(autoUpdater as any).allowPrerelease = false;
|
||||
(autoUpdater as any).allowDowngrade = false;
|
||||
(autoUpdater as any).forceDevUpdateConfig = false;
|
||||
(autoUpdater as any).currentVersion = undefined;
|
||||
|
||||
// Capture registered events
|
||||
registeredEvents = new Map();
|
||||
@@ -213,6 +215,24 @@ describe('UpdaterManager', () => {
|
||||
|
||||
expect(mockBroadcast).toHaveBeenCalledWith('updateError', 'Network error');
|
||||
});
|
||||
|
||||
it('should treat missing latest/stable yml 404 as not-available during manual check', async () => {
|
||||
const error = new Error(
|
||||
'Cannot find latest-mac.yml in the latest release artifacts (https://github.com/lobehub/lobe-chat/releases/download/v2.0.0-next.311/latest-mac.yml): HttpError: 404',
|
||||
);
|
||||
vi.mocked(autoUpdater.checkForUpdates).mockRejectedValueOnce(error);
|
||||
|
||||
await updaterManager.checkForUpdates({ manual: true });
|
||||
|
||||
expect(mockBroadcast).toHaveBeenCalledWith(
|
||||
'manualUpdateNotAvailable',
|
||||
expect.objectContaining({
|
||||
releaseDate: expect.any(String),
|
||||
version: expect.any(String),
|
||||
}),
|
||||
);
|
||||
expect(mockBroadcast).not.toHaveBeenCalledWith('updateError', expect.anything());
|
||||
});
|
||||
});
|
||||
|
||||
describe('downloadUpdate', () => {
|
||||
@@ -487,6 +507,26 @@ describe('UpdaterManager', () => {
|
||||
|
||||
expect(mockBroadcast).not.toHaveBeenCalledWith('updateError', expect.anything());
|
||||
});
|
||||
|
||||
it('should not broadcast updateError for missing manifest 404 (gap period)', async () => {
|
||||
vi.mocked(autoUpdater.checkForUpdates).mockResolvedValue({} as any);
|
||||
await updaterManager.checkForUpdates({ manual: true });
|
||||
|
||||
const error = new Error(
|
||||
'Cannot find latest-mac.yml in the latest release artifacts (https://github.com/lobehub/lobe-chat/releases/download/v2.0.0-next.311/latest-mac.yml): HttpError: 404',
|
||||
);
|
||||
const handler = registeredEvents.get('error');
|
||||
await handler?.(error);
|
||||
|
||||
expect(mockBroadcast).toHaveBeenCalledWith(
|
||||
'manualUpdateNotAvailable',
|
||||
expect.objectContaining({
|
||||
releaseDate: expect.any(String),
|
||||
version: expect.any(String),
|
||||
}),
|
||||
);
|
||||
expect(mockBroadcast).not.toHaveBeenCalledWith('updateError', expect.anything());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ vi.mock('electron', () => ({
|
||||
setApplicationMenu: vi.fn(),
|
||||
},
|
||||
app: {
|
||||
getAppPath: vi.fn(() => '/mock/app/path'),
|
||||
getName: vi.fn(() => 'LobeChat'),
|
||||
getPath: vi.fn((type: string) => {
|
||||
if (type === 'logs') return '/path/to/logs';
|
||||
|
||||
@@ -27,10 +27,6 @@ export const updaterConfig = {
|
||||
// Update check interval (milliseconds)
|
||||
checkUpdateInterval: 60 * 60 * 1000, // 1 hour
|
||||
},
|
||||
|
||||
// Whether to enable application updates
|
||||
enableAppUpdate: !isDev,
|
||||
|
||||
// 是否启用渲染层热更新
|
||||
enableRenderHotUpdate: !isDev,
|
||||
};
|
||||
|
||||
@@ -1,4 +1,195 @@
|
||||
[
|
||||
{
|
||||
"children": {
|
||||
"improvements": ["Move vercel-react-best-practices skills to .agents directory."],
|
||||
"features": ["Skill setting page and skill store."],
|
||||
"fixes": ["Fix group broadcast trigger tool use, fix local system tools."]
|
||||
},
|
||||
"date": "2026-01-22",
|
||||
"version": "2.0.0-next.339"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["Updata cron job ui & fixed commnuity pagenation goto error."]
|
||||
},
|
||||
"date": "2026-01-22",
|
||||
"version": "2.0.0-next.338"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": [
|
||||
"Fix memory schema, update the agentbuilder tools not always use humanIntervention."
|
||||
]
|
||||
},
|
||||
"date": "2026-01-22",
|
||||
"version": "2.0.0-next.337"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"features": ["Support agent group unpublish agents."],
|
||||
"fixes": ["Fix tool argument scape and improve multi task run."]
|
||||
},
|
||||
"date": "2026-01-22",
|
||||
"version": "2.0.0-next.336"
|
||||
},
|
||||
{
|
||||
"children": {},
|
||||
"date": "2026-01-22",
|
||||
"version": "2.0.0-next.335"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"features": ["Add platform-aware download client menu option."]
|
||||
},
|
||||
"date": "2026-01-21",
|
||||
"version": "2.0.0-next.334"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"features": ["Update the sandbox preinstall libs in sys role."],
|
||||
"fixes": ["Fix multi tasks no summary issue."]
|
||||
},
|
||||
"date": "2026-01-21",
|
||||
"version": "2.0.0-next.333"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["Improve e2e server and complete i18n resources."]
|
||||
},
|
||||
"date": "2026-01-21",
|
||||
"version": "2.0.0-next.332"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["Slove the agent group editor not focus in editdata area."]
|
||||
},
|
||||
"date": "2026-01-21",
|
||||
"version": "2.0.0-next.331"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["Fix multi agent tasks issue."]
|
||||
},
|
||||
"date": "2026-01-21",
|
||||
"version": "2.0.0-next.330"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["Sloved the old removeSessionTopics not work."]
|
||||
},
|
||||
"date": "2026-01-21",
|
||||
"version": "2.0.0-next.329"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"features": ["Support client tasks mode."]
|
||||
},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.328"
|
||||
},
|
||||
{
|
||||
"children": {},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.327"
|
||||
},
|
||||
{
|
||||
"children": {},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.326"
|
||||
},
|
||||
{
|
||||
"children": {},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.325"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["TypewriterEffect not refreshing on language change."]
|
||||
},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.324"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"improvements": ["Optimize profile settings skeleton screen."],
|
||||
"features": ["Add the agents and agents group fork feature."],
|
||||
"fixes": [
|
||||
"Fix Topic component causing stack overflow and freezing the app, simplify updater config logic, slove the nuqs error in commnuity agent group page."
|
||||
]
|
||||
},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.323"
|
||||
},
|
||||
{
|
||||
"children": {},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.322"
|
||||
},
|
||||
{
|
||||
"children": {},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.321"
|
||||
},
|
||||
{
|
||||
"children": {},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.320"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["Slove commnuity user avatarUrl is wrong, should update others in profile."]
|
||||
},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.319"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["Fix parallel tools calling race issue."]
|
||||
},
|
||||
"date": "2026-01-20",
|
||||
"version": "2.0.0-next.318"
|
||||
},
|
||||
{
|
||||
"children": {},
|
||||
"date": "2026-01-19",
|
||||
"version": "2.0.0-next.317"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["When use trpc client should include the credentials cookies."]
|
||||
},
|
||||
"date": "2026-01-19",
|
||||
"version": "2.0.0-next.316"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"features": ["Add the cloudEndpoint & Klavis Tools Call in Excuation Task."]
|
||||
},
|
||||
"date": "2026-01-19",
|
||||
"version": "2.0.0-next.315"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"features": ["Improve desktop onboarding window management and footer actions."]
|
||||
},
|
||||
"date": "2026-01-19",
|
||||
"version": "2.0.0-next.314"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"fixes": ["Fix server agent task run with headless, internlm provider base url and homepage."]
|
||||
},
|
||||
"date": "2026-01-19",
|
||||
"version": "2.0.0-next.313"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"improvements": ["Change the /community/assistant to /agent routes."],
|
||||
"features": ["Improve the agentbuilder systemRole."]
|
||||
},
|
||||
"date": "2026-01-19",
|
||||
"version": "2.0.0-next.312"
|
||||
},
|
||||
{
|
||||
"children": {
|
||||
"improvements": ["Refactor market sdk into market servers."]
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
---
|
||||
title: LobeChat Plugin Ecosystem - Functionality Extensions and Development Resources
|
||||
description: >-
|
||||
Discover how the LobeChat plugin ecosystem enhances the utility and flexibility of the LobeChat assistant, along with the development resources and plugin development guidelines provided.
|
||||
|
||||
Discover how the LobeChat plugin ecosystem enhances the utility and
|
||||
flexibility of the LobeChat assistant, along with the development resources
|
||||
and plugin development guidelines provided.
|
||||
tags:
|
||||
- LobeChat
|
||||
- Plugins
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
---
|
||||
title: >-
|
||||
LobeChat Supports Multimodal Interaction: Visual Recognition Enhances Intelligent Dialogue
|
||||
|
||||
LobeChat Supports Multimodal Interaction: Visual Recognition Enhances
|
||||
Intelligent Dialogue
|
||||
description: >-
|
||||
LobeChat supports various large language models with visual recognition capabilities, allowing users to upload or drag and drop images. The assistant will recognize the content and engage in intelligent dialogue, creating a more intelligent and diverse chat environment.
|
||||
|
||||
LobeChat supports various large language models with visual recognition
|
||||
capabilities, allowing users to upload or drag and drop images. The assistant
|
||||
will recognize the content and engage in intelligent dialogue, creating a more
|
||||
intelligent and diverse chat environment.
|
||||
tags:
|
||||
- Visual Recognition
|
||||
- LobeChat
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
---
|
||||
title: LobeChat Text-to-Image Generation Technology
|
||||
description: >-
|
||||
LobeChat supports Text-to-Speech (TTS) and Speech-to-Text (STT) technologies, offering high-quality voice options for a personalized communication experience. Learn more about Lobe TTS Toolkit.
|
||||
|
||||
LobeChat supports Text-to-Speech (TTS) and Speech-to-Text (STT) technologies,
|
||||
offering high-quality voice options for a personalized communication
|
||||
experience. Learn more about Lobe TTS Toolkit.
|
||||
tags:
|
||||
- TTS
|
||||
- STT
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
---
|
||||
title: 'LobeChat Text-to-Image: Text-to-Image Generation Technology'
|
||||
description: >-
|
||||
LobeChat now supports the latest text-to-image generation technology, allowing users to directly invoke the text-to-image tool during conversations with the assistant for creative purposes. By utilizing AI tools such as DALL-E 3, MidJourney, and Pollinations, assistants can turn your ideas into images, making the creative process more intimate and immersive.
|
||||
|
||||
LobeChat now supports the latest text-to-image generation technology, allowing
|
||||
users to directly invoke the text-to-image tool during conversations with the
|
||||
assistant for creative purposes. By utilizing AI tools such as DALL-E 3,
|
||||
MidJourney, and Pollinations, assistants can turn your ideas into images,
|
||||
making the creative process more intimate and immersive.
|
||||
tags:
|
||||
- Text-to-Image
|
||||
- LobeChat
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat 文生图:文本转图片生成技术
|
||||
description: >-
|
||||
LobeChat 现在支持最新的文本到图片生成技术,让用户可以在与助手对话中直接调用文生图工具进行创作。利用 DALL-E 3、MidJourney 和 Pollinations 等 AI 工具,助手们可以将你的想法转化为图像,让创作过程更私密和沉浸式。
|
||||
|
||||
LobeChat 现在支持最新的文本到图片生成技术,让用户可以在与助手对话中直接调用文生图工具进行创作。利用 DALL-E 3、MidJourney 和
|
||||
Pollinations 等 AI 工具,助手们可以将你的想法转化为图像,让创作过程更私密和沉浸式。
|
||||
tags:
|
||||
- Text to Image
|
||||
- 文生图
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat Supports Multi-User Management with Clerk and Next-Auth
|
||||
description: >-
|
||||
LobeChat offers various user authentication and management solutions, including Clerk and Next-Auth, to meet the diverse needs of different users.
|
||||
|
||||
LobeChat offers various user authentication and management solutions,
|
||||
including Clerk and Next-Auth, to meet the diverse needs of different users.
|
||||
tags:
|
||||
- User Management
|
||||
- Next-Auth
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
---
|
||||
title: 'LobeChat 1.0: New Architecture and New Possibilities'
|
||||
description: >-
|
||||
LobeChat 1.0 brings a brand-new architecture and features for server-side databases and user authentication management, opening up new possibilities. On this basis, LobeChat Cloud has entered beta testing.
|
||||
|
||||
LobeChat 1.0 brings a brand-new architecture and features for server-side
|
||||
databases and user authentication management, opening up new possibilities. On
|
||||
this basis, LobeChat Cloud has entered beta testing.
|
||||
tags:
|
||||
- LobeChat
|
||||
- Version 1.0
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat 1.0:新的架构与新的可能
|
||||
description: >-
|
||||
LobeChat 1.0 带来了服务端数据库、用户鉴权管理的全新架构与特性,开启了新的可能 。在此基础上, LobeChat Cloud 开启 Beta 版测试。
|
||||
|
||||
LobeChat 1.0 带来了服务端数据库、用户鉴权管理的全新架构与特性,开启了新的可能 。在此基础上, LobeChat Cloud 开启 Beta
|
||||
版测试。
|
||||
tags:
|
||||
- LobeChat
|
||||
- 服务端数据库
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
---
|
||||
title: 'LobeChat Fully Enters the GPT-4 Era: GPT-4o Mini Officially Launched'
|
||||
description: >-
|
||||
LobeChat v1.6 has been released with support for GPT-4o mini, while LobeChat Cloud services have been fully upgraded to provide users with a more powerful AI conversation experience.
|
||||
|
||||
LobeChat v1.6 has been released with support for GPT-4o mini, while LobeChat
|
||||
Cloud services have been fully upgraded to provide users with a more powerful
|
||||
AI conversation experience.
|
||||
tags:
|
||||
- LobeChat
|
||||
- GPT-4o Mini
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat 全面进入 GPT-4 时代:GPT-4o mini 正式上线
|
||||
description: >-
|
||||
LobeChat v1.6 重磅发布 GPT-4o mini 支持,同时 LobeChat Cloud 服务全面升级默认模型,为用户带来更强大的 AI 对话体验。
|
||||
|
||||
LobeChat v1.6 重磅发布 GPT-4o mini 支持,同时 LobeChat Cloud 服务全面升级默认模型,为用户带来更强大的 AI
|
||||
对话体验。
|
||||
tags:
|
||||
- LobeChat
|
||||
- GPT-4o mini
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
---
|
||||
title: LobeChat Database Docker Image Official Release
|
||||
description: >-
|
||||
LobeChat v1.8.0 launches the official database Docker image, supporting cloud data synchronization and user management, along with comprehensive self-deployment documentation.
|
||||
|
||||
LobeChat v1.8.0 launches the official database Docker image, supporting cloud
|
||||
data synchronization and user management, along with comprehensive
|
||||
self-deployment documentation.
|
||||
tags:
|
||||
- LobeChat
|
||||
- Docker Image
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
---
|
||||
title: >-
|
||||
LobeChat Launches Knowledge Base Feature: A New Experience in Intelligent File Management and Dialogue
|
||||
|
||||
LobeChat Launches Knowledge Base Feature: A New Experience in Intelligent File
|
||||
Management and Dialogue
|
||||
description: >-
|
||||
LobeChat introduces a brand new knowledge base feature that supports all types of file management, intelligent vectorization, and file dialogue, making knowledge management and information retrieval easier and smarter.
|
||||
|
||||
LobeChat introduces a brand new knowledge base feature that supports all types
|
||||
of file management, intelligent vectorization, and file dialogue, making
|
||||
knowledge management and information retrieval easier and smarter.
|
||||
tags:
|
||||
- LobeChat
|
||||
- Knowledge Base
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat Perfectly Adapts to OpenAI O1 Series Models
|
||||
description: >-
|
||||
LobeChat v1.17.0 now supports OpenAI's latest o1-preview and o1-mini models, bringing users enhanced coding and mathematical capabilities.
|
||||
|
||||
LobeChat v1.17.0 now supports OpenAI's latest o1-preview and o1-mini models,
|
||||
bringing users enhanced coding and mathematical capabilities.
|
||||
tags:
|
||||
- OpenAI O1
|
||||
- LobeChat
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
---
|
||||
title: 'Major Update: LobeChat Enters the Era of Artifacts'
|
||||
description: >-
|
||||
LobeChat v1.19 brings significant updates, including full feature support for Claude Artifacts, a brand new discovery page design, and support for GitHub Models providers, greatly enhancing the capabilities of the AI assistant.
|
||||
|
||||
LobeChat v1.19 brings significant updates, including full feature support for
|
||||
Claude Artifacts, a brand new discovery page design, and support for GitHub
|
||||
Models providers, greatly enhancing the capabilities of the AI assistant.
|
||||
tags:
|
||||
- LobeChat
|
||||
- AI Assistant
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: 重磅更新:LobeChat 迎来 Artifacts 时代
|
||||
description: >-
|
||||
LobeChat v1.19 带来了重大更新,包括 Claude Artifacts 完整特性支持、全新的发现页面设计,以及 GitHub Models 服务商支持,让 AI 助手的能力得到显著提升。
|
||||
|
||||
LobeChat v1.19 带来了重大更新,包括 Claude Artifacts 完整特性支持、全新的发现页面设计,以及 GitHub Models
|
||||
服务商支持,让 AI 助手的能力得到显著提升。
|
||||
tags:
|
||||
- LobeChat
|
||||
- Artifacts
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
---
|
||||
title: LobeChat Introduces Persistent Assistant Sidebar Feature
|
||||
description: >-
|
||||
LobeChat v1.26.0 launches the persistent assistant sidebar feature, supporting quick key switching for easy access to frequently used assistants, significantly enhancing efficiency.
|
||||
|
||||
LobeChat v1.26.0 launches the persistent assistant sidebar feature, supporting
|
||||
quick key switching for easy access to frequently used assistants,
|
||||
significantly enhancing efficiency.
|
||||
tags:
|
||||
- Persistent Assistant
|
||||
- Sidebar Feature
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
---
|
||||
title: LobeChat Supports Sharing Conversations in Text Format (Markdown/JSON)
|
||||
description: >-
|
||||
LobeChat v1.28.0 introduces support for exporting conversations in Markdown and OpenAI format JSON, making it easy to convert conversation content into note materials, development debugging data, and training corpora, significantly enhancing the reusability of conversation content.
|
||||
|
||||
LobeChat v1.28.0 introduces support for exporting conversations in Markdown
|
||||
and OpenAI format JSON, making it easy to convert conversation content into
|
||||
note materials, development debugging data, and training corpora,
|
||||
significantly enhancing the reusability of conversation content.
|
||||
tags:
|
||||
- Text Format Export
|
||||
- Markdown Export
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat 支持分享对话为文本格式(Markdown/JSON)
|
||||
description: >-
|
||||
LobeChat v1.28.0 新增 Markdown 和 OpenAI 格式 JSON 导出支持,让对话内容能轻松转化为笔记素材、开发调试数据和训练语料,显著提升对话内容的复用价值。
|
||||
|
||||
LobeChat v1.28.0 新增 Markdown 和 OpenAI 格式 JSON
|
||||
导出支持,让对话内容能轻松转化为笔记素材、开发调试数据和训练语料,显著提升对话内容的复用价值。
|
||||
tags:
|
||||
- 对话内容
|
||||
- Markdown导出
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: New Model Providers Added to LobeChat in November
|
||||
description: >-
|
||||
LobeChat model providers now support Gitee AI, InternLM (ShuSheng PuYu), xAI, and Cloudflare WorkersAI
|
||||
|
||||
LobeChat model providers now support Gitee AI, InternLM (ShuSheng PuYu), xAI,
|
||||
and Cloudflare WorkersAI
|
||||
tags:
|
||||
- LobeChat
|
||||
- AI Model Providers
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat Supports Branching Conversations
|
||||
description: >-
|
||||
LobeChat now allows you to create new conversation branches from any message, freeing your thoughts.
|
||||
|
||||
LobeChat now allows you to create new conversation branches from any message,
|
||||
freeing your thoughts.
|
||||
tags:
|
||||
- Branching Conversations
|
||||
- LobeChat
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat Supports User Data Statistics and Activity Sharing
|
||||
description: >-
|
||||
LobeChat now supports multi-dimensional user data statistics and activity sharing
|
||||
|
||||
LobeChat now supports multi-dimensional user data statistics and activity
|
||||
sharing
|
||||
tags:
|
||||
- LobeChat
|
||||
- User Statistics
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: LobeChat Launches New AI Provider Management System
|
||||
description: >-
|
||||
LobeChat has revamped its AI Provider Management System, now supporting custom AI providers and models.
|
||||
|
||||
LobeChat has revamped its AI Provider Management System, now supporting custom
|
||||
AI providers and models.
|
||||
tags:
|
||||
- LobeChat
|
||||
- AI Provider
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
---
|
||||
title: >-
|
||||
LobeChat Integrates DeepSeek R1, Bringing a Revolutionary Chain of Thought Experience
|
||||
|
||||
LobeChat Integrates DeepSeek R1, Bringing a Revolutionary Chain of Thought
|
||||
Experience
|
||||
description: >-
|
||||
LobeChat v1.49.12 fully supports the DeepSeek R1 model, providing users with an unprecedented interactive experience in the chain of thought.
|
||||
|
||||
LobeChat v1.49.12 fully supports the DeepSeek R1 model, providing users with
|
||||
an unprecedented interactive experience in the chain of thought.
|
||||
tags:
|
||||
- LobeChat
|
||||
- DeepSeek
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: New Authentication Provider Guide
|
||||
---
|
||||
|
||||
# New Authentication Provider Guide
|
||||
|
||||
LobeChat uses [Auth.js v5](https://authjs.dev/) as the external authentication service. Auth.js is an open-source authentication library that provides a simple way to implement authentication and authorization features. This document will introduce how to use Auth.js to implement a new authentication provider.
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: 新身份验证方式开发指南
|
||||
---
|
||||
|
||||
# 新身份验证方式开发指南
|
||||
|
||||
LobeChat 使用 [Auth.js v5](https://authjs.dev/) 作为外部身份验证服务。Auth.js 是一个开源的身份验证库,它提供了一种简单的方式来实现身份验证和授权功能。本文档将介绍如何使用 Auth.js 来实现新的身份验证方式。
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: Adding New Image Models
|
||||
---
|
||||
|
||||
# Adding New Image Models
|
||||
|
||||
> Learn more about the AI image generation modal design in the [AI Image Generation Modal Design Discussion](https://github.com/lobehub/lobe-chat/discussions/7442)
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: 添加新的图像模型
|
||||
---
|
||||
|
||||
# 添加新的图像模型
|
||||
|
||||
> 了解更多关于 AI 绘画模态的设计,请参考 [AI 绘画模态设计讨论](https://github.com/lobehub/lobe-chat/discussions/7442)
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: Architecture Design
|
||||
---
|
||||
|
||||
# Architecture Design
|
||||
|
||||
LobeChat is an AI chat application built on the Next.js framework, aiming to provide an AI productivity platform that enables users to interact with AI through natural language. The following is an overview of the architecture design of LobeChat:
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user