Commit ae18e3db authored by Jiachi Liu's avatar Jiachi Liu

prompt of suggestion

parent 2de49121
...@@ -3,9 +3,54 @@ import { cookies, headers } from 'next/headers' ...@@ -3,9 +3,54 @@ import { cookies, headers } from 'next/headers'
import { connection } from 'next/server' import { connection } from 'next/server'
// ============================================================================= // =============================================================================
// Case 1: cookies() - Request-specific data (no "use cache") // Component wrappers to create depth between Suspense and dynamic API
// ============================================================================= // =============================================================================
async function CookieContent() {
function Dashboard({ children }: { children: React.ReactNode }) {
return <div className="dashboard">{children}</div>
}
function UserSection({ children }: { children: React.ReactNode }) {
return <div className="user-section">{children}</div>
}
function UserProfile({ children }: { children: React.ReactNode }) {
return <div className="user-profile">{children}</div>
}
function ProfileSettings({ children }: { children: React.ReactNode }) {
return <div className="profile-settings">{children}</div>
}
function SettingsPanel({ children }: { children: React.ReactNode }) {
return <div className="settings-panel">{children}</div>
}
function PreferencesForm({ children }: { children: React.ReactNode }) {
return <div className="preferences-form">{children}</div>
}
function FormFields({ children }: { children: React.ReactNode }) {
return <div className="form-fields">{children}</div>
}
function RequestHandler({ children }: { children: React.ReactNode }) {
return <div className="request-handler">{children}</div>
}
function NetworkLayer({ children }: { children: React.ReactNode }) {
return <div className="network-layer">{children}</div>
}
function DataProvider({ children }: { children: React.ReactNode }) {
return <div className="data-provider">{children}</div>
}
// =============================================================================
// Case 1: cookies() - Deep nesting (7 layers)
// Dashboard > UserSection > UserProfile > ProfileSettings > SettingsPanel > PreferencesForm > FormFields > CookieReader
// =============================================================================
async function CookieReader() {
const cookieStore = await cookies() const cookieStore = await cookies()
const allCookies = cookieStore.getAll() const allCookies = cookieStore.getAll()
return ( return (
...@@ -15,10 +60,31 @@ async function CookieContent() { ...@@ -15,10 +60,31 @@ async function CookieContent() {
) )
} }
function DeepCookieContent() {
return (
<Dashboard>
<UserSection>
<UserProfile>
<ProfileSettings>
<SettingsPanel>
<PreferencesForm>
<FormFields>
<CookieReader />
</FormFields>
</PreferencesForm>
</SettingsPanel>
</ProfileSettings>
</UserProfile>
</UserSection>
</Dashboard>
)
}
// ============================================================================= // =============================================================================
// Case 2: headers() - Request-specific data (no "use cache") // Case 2: headers() - Medium nesting (3 layers)
// RequestHandler > NetworkLayer > DataProvider > HeaderReader
// ============================================================================= // =============================================================================
async function HeaderContent() { async function HeaderReader() {
const headersList = await headers() const headersList = await headers()
const userAgent = headersList.get('user-agent') || 'Unknown' const userAgent = headersList.get('user-agent') || 'Unknown'
return ( return (
...@@ -28,10 +94,31 @@ async function HeaderContent() { ...@@ -28,10 +94,31 @@ async function HeaderContent() {
) )
} }
function HeaderContent() {
return (
<RequestHandler>
<NetworkLayer>
<DataProvider>
<HeaderReader />
</DataProvider>
</NetworkLayer>
</RequestHandler>
)
}
// ============================================================================= // =============================================================================
// Case 3: connection() - Explicitly opt into dynamic rendering // Case 3: connection() - Medium nesting (2 layers)
// ApiClient > ConnectionHandler > ConnectionReader
// ============================================================================= // =============================================================================
async function ConnectionContent() { function ApiClient({ children }: { children: React.ReactNode }) {
return <div className="api-client">{children}</div>
}
function ConnectionHandler({ children }: { children: React.ReactNode }) {
return <div className="connection-handler">{children}</div>
}
async function ConnectionReader() {
await connection() await connection()
return ( return (
<div data-testid="connection-content"> <div data-testid="connection-content">
...@@ -40,8 +127,30 @@ async function ConnectionContent() { ...@@ -40,8 +127,30 @@ async function ConnectionContent() {
) )
} }
function ConnectionContent() {
return (
<ApiClient>
<ConnectionHandler>
<ConnectionReader />
</ConnectionHandler>
</ApiClient>
)
}
// ============================================================================= // =============================================================================
// Case 4: Static async content (for comparison) // Case 4: Direct dynamic call (same level - optimal)
// =============================================================================
async function DirectHeaderContent() {
const headersList = await headers()
return (
<div data-testid="direct-header-content">
Direct header access: {headersList.get('host')}
</div>
)
}
// =============================================================================
// Case 5: Static async content (for comparison - no dynamic API)
// ============================================================================= // =============================================================================
async function StaticAsyncContent() { async function StaticAsyncContent() {
await new Promise((resolve) => setTimeout(resolve, 50)) await new Promise((resolve) => setTimeout(resolve, 50))
...@@ -54,23 +163,24 @@ async function StaticAsyncContent() { ...@@ -54,23 +163,24 @@ async function StaticAsyncContent() {
export default function DynamicPage() { export default function DynamicPage() {
return ( return (
<div data-testid="dynamic-page-root"> <div data-testid="dynamic-page-root" style={{ padding: '20px' }}>
<h1>Dynamic API Test Cases</h1> <h1>Dynamic API Layer Demo</h1>
<p> <p style={{ color: '#666', marginBottom: '24px' }}>
These cases demonstrate dynamic APIs that trigger dynamic rendering. Open the profiler panel to see component layers between Suspense and
dynamic API calls. Click "prompt" to copy optimization suggestions.
</p> </p>
<section> <section style={{ marginBottom: '24px' }}>
<h2>1. cookies() - Request cookies</h2> <h2>1. Deep nesting - 7 layers to cookies()</h2>
<Suspense <Suspense
fallback={<div data-testid="cookie-loading">Loading cookies...</div>} fallback={<div data-testid="cookie-loading">Loading cookies...</div>}
> >
<CookieContent /> <DeepCookieContent />
</Suspense> </Suspense>
</section> </section>
<section> <section style={{ marginBottom: '24px' }}>
<h2>2. headers() - Request headers</h2> <h2>2. Medium nesting - 3 layers to headers()</h2>
<Suspense <Suspense
fallback={<div data-testid="header-loading">Loading headers...</div>} fallback={<div data-testid="header-loading">Loading headers...</div>}
> >
...@@ -78,8 +188,8 @@ export default function DynamicPage() { ...@@ -78,8 +188,8 @@ export default function DynamicPage() {
</Suspense> </Suspense>
</section> </section>
<section> <section style={{ marginBottom: '24px' }}>
<h2>3. connection() - Explicit dynamic opt-in</h2> <h2>3. Medium nesting - 2 layers to connection()</h2>
<Suspense <Suspense
fallback={ fallback={
<div data-testid="connection-loading">Loading connection...</div> <div data-testid="connection-loading">Loading connection...</div>
...@@ -89,8 +199,19 @@ export default function DynamicPage() { ...@@ -89,8 +199,19 @@ export default function DynamicPage() {
</Suspense> </Suspense>
</section> </section>
<section> <section style={{ marginBottom: '24px' }}>
<h2>4. Static async content (for comparison)</h2> <h2>4. Direct call - headers() at same level (optimal)</h2>
<Suspense
fallback={
<div data-testid="direct-header-loading">Loading direct...</div>
}
>
<DirectHeaderContent />
</Suspense>
</section>
<section style={{ marginBottom: '24px' }}>
<h2>5. Static async (no dynamic API)</h2>
<Suspense <Suspense
fallback={ fallback={
<div data-testid="static-async-loading">Loading static...</div> <div data-testid="static-async-loading">Loading static...</div>
......
import Link from 'next/link'
export default function RootLayout({ export default function RootLayout({
children, children,
}: { }: {
...@@ -5,7 +7,32 @@ export default function RootLayout({ ...@@ -5,7 +7,32 @@ export default function RootLayout({
}) { }) {
return ( return (
<html> <html>
<body>{children}</body> <body>
<nav
style={{
padding: '12px 16px',
borderBottom: '1px solid #eee',
display: 'flex',
gap: '16px',
fontFamily: 'system-ui, sans-serif',
fontSize: '14px',
}}
>
<Link href="/" style={{ color: '#0070f3' }}>
Home
</Link>
<Link href="/dynamic" style={{ color: '#0070f3' }}>
Dynamic APIs
</Link>
<Link href="/nested" style={{ color: '#0070f3' }}>
Nested Suspense
</Link>
<Link href="/item/test-slug" style={{ color: '#0070f3' }}>
Dynamic Params
</Link>
</nav>
{children}
</body>
</html> </html>
) )
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment