0
0
NextJSframework~20 mins

Server-only modules in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server-only Modules Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js server component importing a server-only module?

Consider a Next.js server component that imports a server-only module to fetch data. What will be rendered on the client?

NextJS
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!' };
}
A<div>Hello from server!</div>
B<div>[object Promise]</div>
CError: Module not found on client
D<div>undefined</div>
Attempts:
2 left
💡 Hint

Think about where server-only modules run and how data is fetched before rendering.

📝 Syntax
intermediate
1:30remaining
Which import statement correctly imports a server-only module in Next.js?

Given a server-only module at lib/server-utils.js, which import statement is valid in a server component?

Aimport utils from 'lib/server-utils';
Bimport utils from './server-utils';
Cimport utils from '../lib/server-utils';
Dimport utils from '/lib/server-utils';
Attempts:
2 left
💡 Hint

Consider relative paths and module resolution in Next.js.

🔧 Debug
advanced
2:30remaining
Why does this Next.js server component fail to compile when importing a server-only module in a client component?

Examine the code below. Why does it cause a build error?

NextJS
// 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;
AClient components cannot import server-only modules, causing a build error.
BThe 'use client' directive is missing in the server-secret module.
CThe import path '../lib/server-secret' is invalid.
DThe secret variable is not exported correctly.
Attempts:
2 left
💡 Hint

Think about the difference between client and server components and module usage.

state_output
advanced
2:00remaining
What is the value of 'result' after running this Next.js server component code?

Given the server-only module and server component below, what is the value of result?

NextJS
// 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>;
}
Aundefined
B42
CError: compute is not a function
Dnull
Attempts:
2 left
💡 Hint

Consider how server-only modules export functions and how they are used in server components.

🧠 Conceptual
expert
3:00remaining
Which statement best describes the behavior of server-only modules in Next.js?

Choose the most accurate description of server-only modules in Next.js.

AServer-only modules are deprecated and replaced by client components in Next.js.
BServer-only modules can be imported by both client and server components without restrictions.
CServer-only modules run on the client but fetch data from the server asynchronously.
DServer-only modules run exclusively on the server and cannot be imported by client components.
Attempts:
2 left
💡 Hint

Think about where server-only modules execute and their import limitations.