@lobehub/lobehub 2.0.0-next.320 → 2.0.0-next.322
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.codex/skills/vercel-react-best-practices/AGENTS.md +2410 -0
- package/.codex/skills/vercel-react-best-practices/SKILL.md +125 -0
- package/.codex/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/.codex/skills/vercel-react-best-practices/rules/advanced-use-latest.md +49 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-api-routes.md +38 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-defer-await.md +80 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-dependencies.md +36 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-parallel.md +28 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +99 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +59 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-conditional.md +31 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +49 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +35 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-preload.md +50 -0
- package/.codex/skills/vercel-react-best-practices/rules/client-event-listeners.md +74 -0
- package/.codex/skills/vercel-react-best-practices/rules/client-localstorage-schema.md +71 -0
- package/.codex/skills/vercel-react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/.codex/skills/vercel-react-best-practices/rules/client-swr-dedup.md +56 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +57 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-cache-function-results.md +80 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-cache-property-access.md +28 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-cache-storage.md +70 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-combine-iterations.md +32 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-early-exit.md +50 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-index-maps.md +37 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-length-check-first.md +49 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-min-max-loop.md +82 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-activity.md +26 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +40 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +46 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +82 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-dependencies.md +45 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-derived-state.md +29 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +74 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +58 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-memo.md +44 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-transitions.md +40 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +73 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-cache-lru.md +41 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-cache-react.md +76 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +83 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-serialization.md +38 -0
- package/.cursor/skills/vercel-react-best-practices/AGENTS.md +2410 -0
- package/.cursor/skills/vercel-react-best-practices/SKILL.md +125 -0
- package/.cursor/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/.cursor/skills/vercel-react-best-practices/rules/advanced-use-latest.md +49 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-api-routes.md +38 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-defer-await.md +80 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-dependencies.md +36 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-parallel.md +28 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +99 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +59 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-conditional.md +31 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +49 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +35 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-preload.md +50 -0
- package/.cursor/skills/vercel-react-best-practices/rules/client-event-listeners.md +74 -0
- package/.cursor/skills/vercel-react-best-practices/rules/client-localstorage-schema.md +71 -0
- package/.cursor/skills/vercel-react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/.cursor/skills/vercel-react-best-practices/rules/client-swr-dedup.md +56 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +57 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-cache-function-results.md +80 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-cache-property-access.md +28 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-cache-storage.md +70 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-combine-iterations.md +32 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-early-exit.md +50 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-index-maps.md +37 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-length-check-first.md +49 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-min-max-loop.md +82 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-activity.md +26 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +40 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +46 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +82 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-dependencies.md +45 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-derived-state.md +29 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +74 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +58 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-memo.md +44 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-transitions.md +40 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +73 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-cache-lru.md +41 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-cache-react.md +76 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +83 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-serialization.md +38 -0
- package/CHANGELOG.md +50 -0
- package/changelog/v1.json +10 -0
- package/package.json +1 -1
- package/src/app/[variants]/(main)/agent/profile/index.tsx +15 -2
- package/src/features/PageEditor/PageEditor.tsx +20 -8
- package/src/layout/GlobalProvider/FaviconProvider.tsx +45 -21
- package/src/server/globalConfig/parseMemoryExtractionConfig.ts +43 -4
- package/src/server/services/memory/userMemory/__tests__/extract.payload.test.ts +101 -0
- package/src/server/services/memory/userMemory/__tests__/extract.runtime.test.ts +121 -0
- package/src/server/services/memory/userMemory/extract.ts +164 -17
- package/src/utils/styles.ts +10 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Functional setState Updates
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: prevents stale closures and unnecessary callback recreations
|
|
5
|
+
tags: react, hooks, useState, useCallback, callbacks, closures
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Functional setState Updates
|
|
9
|
+
|
|
10
|
+
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.
|
|
11
|
+
|
|
12
|
+
**Incorrect (requires state as dependency):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function TodoList() {
|
|
16
|
+
const [items, setItems] = useState(initialItems)
|
|
17
|
+
|
|
18
|
+
// Callback must depend on items, recreated on every items change
|
|
19
|
+
const addItems = useCallback((newItems: Item[]) => {
|
|
20
|
+
setItems([...items, ...newItems])
|
|
21
|
+
}, [items]) // ❌ items dependency causes recreations
|
|
22
|
+
|
|
23
|
+
// Risk of stale closure if dependency is forgotten
|
|
24
|
+
const removeItem = useCallback((id: string) => {
|
|
25
|
+
setItems(items.filter(item => item.id !== id))
|
|
26
|
+
}, []) // ❌ Missing items dependency - will use stale items!
|
|
27
|
+
|
|
28
|
+
return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
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.
|
|
33
|
+
|
|
34
|
+
**Correct (stable callbacks, no stale closures):**
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
function TodoList() {
|
|
38
|
+
const [items, setItems] = useState(initialItems)
|
|
39
|
+
|
|
40
|
+
// Stable callback, never recreated
|
|
41
|
+
const addItems = useCallback((newItems: Item[]) => {
|
|
42
|
+
setItems(curr => [...curr, ...newItems])
|
|
43
|
+
}, []) // ✅ No dependencies needed
|
|
44
|
+
|
|
45
|
+
// Always uses latest state, no stale closure risk
|
|
46
|
+
const removeItem = useCallback((id: string) => {
|
|
47
|
+
setItems(curr => curr.filter(item => item.id !== id))
|
|
48
|
+
}, []) // ✅ Safe and stable
|
|
49
|
+
|
|
50
|
+
return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Benefits:**
|
|
55
|
+
|
|
56
|
+
1. **Stable callback references** - Callbacks don't need to be recreated when state changes
|
|
57
|
+
2. **No stale closures** - Always operates on the latest state value
|
|
58
|
+
3. **Fewer dependencies** - Simplifies dependency arrays and reduces memory leaks
|
|
59
|
+
4. **Prevents bugs** - Eliminates the most common source of React closure bugs
|
|
60
|
+
|
|
61
|
+
**When to use functional updates:**
|
|
62
|
+
|
|
63
|
+
- Any setState that depends on the current state value
|
|
64
|
+
- Inside useCallback/useMemo when state is needed
|
|
65
|
+
- Event handlers that reference state
|
|
66
|
+
- Async operations that update state
|
|
67
|
+
|
|
68
|
+
**When direct updates are fine:**
|
|
69
|
+
|
|
70
|
+
- Setting state to a static value: `setCount(0)`
|
|
71
|
+
- Setting state from props/arguments only: `setName(newName)`
|
|
72
|
+
- State doesn't depend on previous value
|
|
73
|
+
|
|
74
|
+
**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 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Lazy State Initialization
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: wasted computation on every render
|
|
5
|
+
tags: react, hooks, useState, performance, initialization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Lazy State Initialization
|
|
9
|
+
|
|
10
|
+
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.
|
|
11
|
+
|
|
12
|
+
**Incorrect (runs on every render):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function FilteredList({ items }: { items: Item[] }) {
|
|
16
|
+
// buildSearchIndex() runs on EVERY render, even after initialization
|
|
17
|
+
const [searchIndex, setSearchIndex] = useState(buildSearchIndex(items))
|
|
18
|
+
const [query, setQuery] = useState('')
|
|
19
|
+
|
|
20
|
+
// When query changes, buildSearchIndex runs again unnecessarily
|
|
21
|
+
return <SearchResults index={searchIndex} query={query} />
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function UserProfile() {
|
|
25
|
+
// JSON.parse runs on every render
|
|
26
|
+
const [settings, setSettings] = useState(
|
|
27
|
+
JSON.parse(localStorage.getItem('settings') || '{}')
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
return <SettingsForm settings={settings} onChange={setSettings} />
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**Correct (runs only once):**
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
function FilteredList({ items }: { items: Item[] }) {
|
|
38
|
+
// buildSearchIndex() runs ONLY on initial render
|
|
39
|
+
const [searchIndex, setSearchIndex] = useState(() => buildSearchIndex(items))
|
|
40
|
+
const [query, setQuery] = useState('')
|
|
41
|
+
|
|
42
|
+
return <SearchResults index={searchIndex} query={query} />
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function UserProfile() {
|
|
46
|
+
// JSON.parse runs only on initial render
|
|
47
|
+
const [settings, setSettings] = useState(() => {
|
|
48
|
+
const stored = localStorage.getItem('settings')
|
|
49
|
+
return stored ? JSON.parse(stored) : {}
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
return <SettingsForm settings={settings} onChange={setSettings} />
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Use lazy initialization when computing initial values from localStorage/sessionStorage, building data structures (indexes, maps), reading from the DOM, or performing heavy transformations.
|
|
57
|
+
|
|
58
|
+
For simple primitives (`useState(0)`), direct references (`useState(props.value)`), or cheap literals (`useState({})`), the function form is unnecessary.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Extract to Memoized Components
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: enables early returns
|
|
5
|
+
tags: rerender, memo, useMemo, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Extract to Memoized Components
|
|
9
|
+
|
|
10
|
+
Extract expensive work into memoized components to enable early returns before computation.
|
|
11
|
+
|
|
12
|
+
**Incorrect (computes avatar even when loading):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function Profile({ user, loading }: Props) {
|
|
16
|
+
const avatar = useMemo(() => {
|
|
17
|
+
const id = computeAvatarId(user)
|
|
18
|
+
return <Avatar id={id} />
|
|
19
|
+
}, [user])
|
|
20
|
+
|
|
21
|
+
if (loading) return <Skeleton />
|
|
22
|
+
return <div>{avatar}</div>
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Correct (skips computation when loading):**
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
const UserAvatar = memo(function UserAvatar({ user }: { user: User }) {
|
|
30
|
+
const id = useMemo(() => computeAvatarId(user), [user])
|
|
31
|
+
return <Avatar id={id} />
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
function Profile({ user, loading }: Props) {
|
|
35
|
+
if (loading) return <Skeleton />
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
<UserAvatar user={user} />
|
|
39
|
+
</div>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**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 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Transitions for Non-Urgent Updates
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: maintains UI responsiveness
|
|
5
|
+
tags: rerender, transitions, startTransition, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Transitions for Non-Urgent Updates
|
|
9
|
+
|
|
10
|
+
Mark frequent, non-urgent state updates as transitions to maintain UI responsiveness.
|
|
11
|
+
|
|
12
|
+
**Incorrect (blocks UI on every scroll):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function ScrollTracker() {
|
|
16
|
+
const [scrollY, setScrollY] = useState(0)
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const handler = () => setScrollY(window.scrollY)
|
|
19
|
+
window.addEventListener('scroll', handler, { passive: true })
|
|
20
|
+
return () => window.removeEventListener('scroll', handler)
|
|
21
|
+
}, [])
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (non-blocking updates):**
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { startTransition } from 'react'
|
|
29
|
+
|
|
30
|
+
function ScrollTracker() {
|
|
31
|
+
const [scrollY, setScrollY] = useState(0)
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const handler = () => {
|
|
34
|
+
startTransition(() => setScrollY(window.scrollY))
|
|
35
|
+
}
|
|
36
|
+
window.addEventListener('scroll', handler, { passive: true })
|
|
37
|
+
return () => window.removeEventListener('scroll', handler)
|
|
38
|
+
}, [])
|
|
39
|
+
}
|
|
40
|
+
```
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use after() for Non-Blocking Operations
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: faster response times
|
|
5
|
+
tags: server, async, logging, analytics, side-effects
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use after() for Non-Blocking Operations
|
|
9
|
+
|
|
10
|
+
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.
|
|
11
|
+
|
|
12
|
+
**Incorrect (blocks response):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { logUserAction } from '@/app/utils'
|
|
16
|
+
|
|
17
|
+
export async function POST(request: Request) {
|
|
18
|
+
// Perform mutation
|
|
19
|
+
await updateDatabase(request)
|
|
20
|
+
|
|
21
|
+
// Logging blocks the response
|
|
22
|
+
const userAgent = request.headers.get('user-agent') || 'unknown'
|
|
23
|
+
await logUserAction({ userAgent })
|
|
24
|
+
|
|
25
|
+
return new Response(JSON.stringify({ status: 'success' }), {
|
|
26
|
+
status: 200,
|
|
27
|
+
headers: { 'Content-Type': 'application/json' }
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Correct (non-blocking):**
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import { after } from 'next/server'
|
|
36
|
+
import { headers, cookies } from 'next/headers'
|
|
37
|
+
import { logUserAction } from '@/app/utils'
|
|
38
|
+
|
|
39
|
+
export async function POST(request: Request) {
|
|
40
|
+
// Perform mutation
|
|
41
|
+
await updateDatabase(request)
|
|
42
|
+
|
|
43
|
+
// Log after response is sent
|
|
44
|
+
after(async () => {
|
|
45
|
+
const userAgent = (await headers()).get('user-agent') || 'unknown'
|
|
46
|
+
const sessionCookie = (await cookies()).get('session-id')?.value || 'anonymous'
|
|
47
|
+
|
|
48
|
+
logUserAction({ sessionCookie, userAgent })
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
return new Response(JSON.stringify({ status: 'success' }), {
|
|
52
|
+
status: 200,
|
|
53
|
+
headers: { 'Content-Type': 'application/json' }
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The response is sent immediately while logging happens in the background.
|
|
59
|
+
|
|
60
|
+
**Common use cases:**
|
|
61
|
+
|
|
62
|
+
- Analytics tracking
|
|
63
|
+
- Audit logging
|
|
64
|
+
- Sending notifications
|
|
65
|
+
- Cache invalidation
|
|
66
|
+
- Cleanup tasks
|
|
67
|
+
|
|
68
|
+
**Important notes:**
|
|
69
|
+
|
|
70
|
+
- `after()` runs even if the response fails or redirects
|
|
71
|
+
- Works in Server Actions, Route Handlers, and Server Components
|
|
72
|
+
|
|
73
|
+
Reference: [https://nextjs.org/docs/app/api-reference/functions/after](https://nextjs.org/docs/app/api-reference/functions/after)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Cross-Request LRU Caching
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: caches across requests
|
|
5
|
+
tags: server, cache, lru, cross-request
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Cross-Request LRU Caching
|
|
9
|
+
|
|
10
|
+
`React.cache()` only works within one request. For data shared across sequential requests (user clicks button A then button B), use an LRU cache.
|
|
11
|
+
|
|
12
|
+
**Implementation:**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { LRUCache } from 'lru-cache'
|
|
16
|
+
|
|
17
|
+
const cache = new LRUCache<string, any>({
|
|
18
|
+
max: 1000,
|
|
19
|
+
ttl: 5 * 60 * 1000 // 5 minutes
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
export async function getUser(id: string) {
|
|
23
|
+
const cached = cache.get(id)
|
|
24
|
+
if (cached) return cached
|
|
25
|
+
|
|
26
|
+
const user = await db.user.findUnique({ where: { id } })
|
|
27
|
+
cache.set(id, user)
|
|
28
|
+
return user
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Request 1: DB query, result cached
|
|
32
|
+
// Request 2: cache hit, no DB query
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Use when sequential user actions hit multiple endpoints needing the same data within seconds.
|
|
36
|
+
|
|
37
|
+
**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.
|
|
38
|
+
|
|
39
|
+
**In traditional serverless:** Each invocation runs in isolation, so consider Redis for cross-process caching.
|
|
40
|
+
|
|
41
|
+
Reference: [https://github.com/isaacs/node-lru-cache](https://github.com/isaacs/node-lru-cache)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Per-Request Deduplication with React.cache()
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: deduplicates within request
|
|
5
|
+
tags: server, cache, react-cache, deduplication
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Per-Request Deduplication with React.cache()
|
|
9
|
+
|
|
10
|
+
Use `React.cache()` for server-side request deduplication. Authentication and database queries benefit most.
|
|
11
|
+
|
|
12
|
+
**Usage:**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { cache } from 'react'
|
|
16
|
+
|
|
17
|
+
export const getCurrentUser = cache(async () => {
|
|
18
|
+
const session = await auth()
|
|
19
|
+
if (!session?.user?.id) return null
|
|
20
|
+
return await db.user.findUnique({
|
|
21
|
+
where: { id: session.user.id }
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Within a single request, multiple calls to `getCurrentUser()` execute the query only once.
|
|
27
|
+
|
|
28
|
+
**Avoid inline objects as arguments:**
|
|
29
|
+
|
|
30
|
+
`React.cache()` uses shallow equality (`Object.is`) to determine cache hits. Inline objects create new references each call, preventing cache hits.
|
|
31
|
+
|
|
32
|
+
**Incorrect (always cache miss):**
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
const getUser = cache(async (params: { uid: number }) => {
|
|
36
|
+
return await db.user.findUnique({ where: { id: params.uid } })
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
// Each call creates new object, never hits cache
|
|
40
|
+
getUser({ uid: 1 })
|
|
41
|
+
getUser({ uid: 1 }) // Cache miss, runs query again
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**Correct (cache hit):**
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
const getUser = cache(async (uid: number) => {
|
|
48
|
+
return await db.user.findUnique({ where: { id: uid } })
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
// Primitive args use value equality
|
|
52
|
+
getUser(1)
|
|
53
|
+
getUser(1) // Cache hit, returns cached result
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
If you must pass objects, pass the same reference:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
const params = { uid: 1 }
|
|
60
|
+
getUser(params) // Query runs
|
|
61
|
+
getUser(params) // Cache hit (same reference)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Next.js-Specific Note:**
|
|
65
|
+
|
|
66
|
+
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:
|
|
67
|
+
|
|
68
|
+
- Database queries (Prisma, Drizzle, etc.)
|
|
69
|
+
- Heavy computations
|
|
70
|
+
- Authentication checks
|
|
71
|
+
- File system operations
|
|
72
|
+
- Any non-fetch async work
|
|
73
|
+
|
|
74
|
+
Use `React.cache()` to deduplicate these operations across your component tree.
|
|
75
|
+
|
|
76
|
+
Reference: [React.cache documentation](https://react.dev/reference/react/cache)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Parallel Data Fetching with Component Composition
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: eliminates server-side waterfalls
|
|
5
|
+
tags: server, rsc, parallel-fetching, composition
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Parallel Data Fetching with Component Composition
|
|
9
|
+
|
|
10
|
+
React Server Components execute sequentially within a tree. Restructure with composition to parallelize data fetching.
|
|
11
|
+
|
|
12
|
+
**Incorrect (Sidebar waits for Page's fetch to complete):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
export default async function Page() {
|
|
16
|
+
const header = await fetchHeader()
|
|
17
|
+
return (
|
|
18
|
+
<div>
|
|
19
|
+
<div>{header}</div>
|
|
20
|
+
<Sidebar />
|
|
21
|
+
</div>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function Sidebar() {
|
|
26
|
+
const items = await fetchSidebarItems()
|
|
27
|
+
return <nav>{items.map(renderItem)}</nav>
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**Correct (both fetch simultaneously):**
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
async function Header() {
|
|
35
|
+
const data = await fetchHeader()
|
|
36
|
+
return <div>{data}</div>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function Sidebar() {
|
|
40
|
+
const items = await fetchSidebarItems()
|
|
41
|
+
return <nav>{items.map(renderItem)}</nav>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default function Page() {
|
|
45
|
+
return (
|
|
46
|
+
<div>
|
|
47
|
+
<Header />
|
|
48
|
+
<Sidebar />
|
|
49
|
+
</div>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Alternative with children prop:**
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
async function Header() {
|
|
58
|
+
const data = await fetchHeader()
|
|
59
|
+
return <div>{data}</div>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function Sidebar() {
|
|
63
|
+
const items = await fetchSidebarItems()
|
|
64
|
+
return <nav>{items.map(renderItem)}</nav>
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function Layout({ children }: { children: ReactNode }) {
|
|
68
|
+
return (
|
|
69
|
+
<div>
|
|
70
|
+
<Header />
|
|
71
|
+
{children}
|
|
72
|
+
</div>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export default function Page() {
|
|
77
|
+
return (
|
|
78
|
+
<Layout>
|
|
79
|
+
<Sidebar />
|
|
80
|
+
</Layout>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Minimize Serialization at RSC Boundaries
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: reduces data transfer size
|
|
5
|
+
tags: server, rsc, serialization, props
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Minimize Serialization at RSC Boundaries
|
|
9
|
+
|
|
10
|
+
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.
|
|
11
|
+
|
|
12
|
+
**Incorrect (serializes all 50 fields):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
async function Page() {
|
|
16
|
+
const user = await fetchUser() // 50 fields
|
|
17
|
+
return <Profile user={user} />
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
'use client'
|
|
21
|
+
function Profile({ user }: { user: User }) {
|
|
22
|
+
return <div>{user.name}</div> // uses 1 field
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Correct (serializes only 1 field):**
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
async function Page() {
|
|
30
|
+
const user = await fetchUser()
|
|
31
|
+
return <Profile name={user.name} />
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
'use client'
|
|
35
|
+
function Profile({ name }: { name: string }) {
|
|
36
|
+
return <div>{name}</div>
|
|
37
|
+
}
|
|
38
|
+
```
|
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,56 @@
|
|
|
2
2
|
|
|
3
3
|
# Changelog
|
|
4
4
|
|
|
5
|
+
## [Version 2.0.0-next.322](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.321...v2.0.0-next.322)
|
|
6
|
+
|
|
7
|
+
<sup>Released on **2026-01-20**</sup>
|
|
8
|
+
|
|
9
|
+
#### 🐛 Bug Fixes
|
|
10
|
+
|
|
11
|
+
- **memory-user-memory**: Should fallback to server configured provider & model.
|
|
12
|
+
|
|
13
|
+
<br/>
|
|
14
|
+
|
|
15
|
+
<details>
|
|
16
|
+
<summary><kbd>Improvements and Fixes</kbd></summary>
|
|
17
|
+
|
|
18
|
+
#### What's fixed
|
|
19
|
+
|
|
20
|
+
- **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))
|
|
21
|
+
|
|
22
|
+
</details>
|
|
23
|
+
|
|
24
|
+
<div align="right">
|
|
25
|
+
|
|
26
|
+
[](#readme-top)
|
|
27
|
+
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
## [Version 2.0.0-next.321](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.320...v2.0.0-next.321)
|
|
31
|
+
|
|
32
|
+
<sup>Released on **2026-01-20**</sup>
|
|
33
|
+
|
|
34
|
+
#### ✨ Features
|
|
35
|
+
|
|
36
|
+
- **memory-user-memory**: Support to configure preferred model.
|
|
37
|
+
|
|
38
|
+
<br/>
|
|
39
|
+
|
|
40
|
+
<details>
|
|
41
|
+
<summary><kbd>Improvements and Fixes</kbd></summary>
|
|
42
|
+
|
|
43
|
+
#### What's improved
|
|
44
|
+
|
|
45
|
+
- **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))
|
|
46
|
+
|
|
47
|
+
</details>
|
|
48
|
+
|
|
49
|
+
<div align="right">
|
|
50
|
+
|
|
51
|
+
[](#readme-top)
|
|
52
|
+
|
|
53
|
+
</div>
|
|
54
|
+
|
|
5
55
|
## [Version 2.0.0-next.320](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.319...v2.0.0-next.320)
|
|
6
56
|
|
|
7
57
|
<sup>Released on **2026-01-20**</sup>
|
package/changelog/v1.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lobehub/lobehub",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.322",
|
|
4
4
|
"description": "LobeHub - an open-source,comprehensive AI Agent framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"framework",
|
|
@@ -8,6 +8,7 @@ import AgentBuilder from '@/features/AgentBuilder';
|
|
|
8
8
|
import WideScreenContainer from '@/features/WideScreenContainer';
|
|
9
9
|
import { useAgentStore } from '@/store/agent';
|
|
10
10
|
import { agentSelectors } from '@/store/agent/selectors';
|
|
11
|
+
import { StyleSheet } from '@/utils/styles';
|
|
11
12
|
|
|
12
13
|
import Header from './features/Header';
|
|
13
14
|
import ProfileEditor from './features/ProfileEditor';
|
|
@@ -15,13 +16,25 @@ import ProfileHydration from './features/ProfileHydration';
|
|
|
15
16
|
import ProfileProvider from './features/ProfileProvider';
|
|
16
17
|
import { useProfileStore } from './features/store';
|
|
17
18
|
|
|
19
|
+
const styles = StyleSheet.create({
|
|
20
|
+
contentWrapper: {
|
|
21
|
+
cursor: 'text',
|
|
22
|
+
display: 'flex',
|
|
23
|
+
overflowY: 'auto',
|
|
24
|
+
position: 'relative',
|
|
25
|
+
},
|
|
26
|
+
profileArea: {
|
|
27
|
+
minWidth: 0,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
18
31
|
const ProfileArea = memo(() => {
|
|
19
32
|
const editor = useProfileStore((s) => s.editor);
|
|
20
33
|
const isAgentConfigLoading = useAgentStore(agentSelectors.isAgentConfigLoading);
|
|
21
34
|
|
|
22
35
|
return (
|
|
23
36
|
<>
|
|
24
|
-
<Flexbox flex={1} height={'100%'}>
|
|
37
|
+
<Flexbox flex={1} height={'100%'} style={styles.profileArea}>
|
|
25
38
|
{isAgentConfigLoading ? (
|
|
26
39
|
<Loading debugId="ProfileArea" />
|
|
27
40
|
) : (
|
|
@@ -33,7 +46,7 @@ const ProfileArea = memo(() => {
|
|
|
33
46
|
onClick={() => {
|
|
34
47
|
editor?.focus();
|
|
35
48
|
}}
|
|
36
|
-
style={
|
|
49
|
+
style={styles.contentWrapper}
|
|
37
50
|
width={'100%'}
|
|
38
51
|
>
|
|
39
52
|
<WideScreenContainer>
|