Consider a Next.js server component that imports a server-only module to fetch data. What will be rendered on the client?
import getData from './server-only-module'; export default async function Page() { const data = await getData(); return <div>{data.message}</div>; } // server-only-module.js import 'server-only'; export default async function getData() { return { message: 'Hello from server!' }; }
Think about where server-only modules run and how data is fetched before rendering.
The server-only module runs on the server, so the data is fetched before rendering. The component renders the message correctly as "Hello from server!" on the client.
Given a server-only module at lib/server-utils.js, which import statement is valid in a server component?
Consider relative paths and module resolution in Next.js.
Using a relative path like ../lib/server-utils correctly imports the module from the parent directory's lib folder. Absolute or root-based imports without configuration will cause errors.
Examine the code below. Why does it cause a build error?
// client-component.jsx 'use client'; import secret from '../lib/server-secret'; export default function ClientComponent() { return <div>{secret}</div>; } // lib/server-secret.js import 'server-only'; const secret = 'top-secret'; export default secret;
Think about the difference between client and server components and module usage.
Client components cannot import server-only modules because server-only modules may use Node.js APIs unavailable in the browser. This causes a build error.
Given the server-only module and server component below, what is the value of result?
// server-only-module.js import 'server-only'; export function compute() { return 42; } // page.jsx import { compute } from './server-only-module'; export default function Page() { const result = compute(); return <div>{result}</div>; }
Consider how server-only modules export functions and how they are used in server components.
The compute function returns 42. The server component calls it and assigns the result to result. So result is 42.
Choose the most accurate description of server-only modules in Next.js.
Think about where server-only modules execute and their import limitations.
Server-only modules run only on the server and cannot be imported by client components because they may use Node.js APIs unavailable in browsers.