0
0
NextJSframework~20 mins

Response modification in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Next.js API route return?

Consider this Next.js API route handler that modifies the response before sending:

export async function GET() {
  const res = new Response(JSON.stringify({ message: 'Hello' }), {
    status: 200,
    headers: { 'Content-Type': 'application/json' }
  });
  res.headers.set('X-Custom-Header', 'TestValue');
  return res;
}

What will the client receive in the response headers?

NextJS
export async function GET() {
  const res = new Response(JSON.stringify({ message: 'Hello' }), {
    status: 200,
    headers: { 'Content-Type': 'application/json' }
  });
  res.headers.set('X-Custom-Header', 'TestValue');
  return res;
}
AResponse with status 200, Content-Type 'application/json', and header 'X-Custom-Header' set to 'TestValue'
BResponse with status 200 and Content-Type 'application/json' only, no 'X-Custom-Header'
CResponse with status 200 and header 'X-Custom-Header' only, no Content-Type
DResponse with status 500 due to header modification error
Attempts:
2 left
💡 Hint

Think about how the Response object headers can be modified before returning.

📝 Syntax
intermediate
2:00remaining
Which option correctly modifies the response status in Next.js API route?

Given this Next.js API route snippet, which option correctly changes the response status to 404?

export async function GET() {
  const res = new Response('Not Found');
  // Modify status here
  return res;
}
NextJS
export async function GET() {
  const res = new Response('Not Found');
  // Modify status here
  return res;
}
Ares.setStatus(404);
Bres.statusCode = 404;
Cres = new Response('Not Found', { status: 404 });
Dres.status = 404;
Attempts:
2 left
💡 Hint

Remember that Response objects are immutable for status after creation.

🔧 Debug
advanced
2:00remaining
Why does this Next.js API route fail to set a cookie?

Examine this Next.js API route code:

export async function GET() {
  const res = new Response('OK');
  res.headers.append('Set-Cookie', 'token=abc123; HttpOnly');
  return res;
}

Why might the cookie not be set in the browser?

NextJS
export async function GET() {
  const res = new Response('OK');
  res.headers.append('Set-Cookie', 'token=abc123; HttpOnly');
  return res;
}
ABecause the Response object headers are immutable and cannot be appended after creation
BBecause the Response body must be JSON to set cookies
CBecause 'HttpOnly' cookies cannot be set from Next.js API routes
DBecause the 'Set-Cookie' header must be set during Response creation, not appended later
Attempts:
2 left
💡 Hint

Consider when headers can be set on the Response object.

state_output
advanced
2:00remaining
What is the output of this Next.js middleware modifying response headers?

Given this Next.js middleware code:

import { NextResponse } from 'next/server';

export function middleware(request) {
  const response = NextResponse.next();
  response.headers.set('X-Middleware', 'Active');
  return response;
}

What will the client receive in the response headers?

NextJS
import { NextResponse } from 'next/server';

export function middleware(request) {
  const response = NextResponse.next();
  response.headers.set('X-Middleware', 'Active');
  return response;
}
AResponse without 'X-Middleware' header because headers cannot be set in middleware
BResponse with header 'X-Middleware' set to 'Active'
CResponse with status 500 due to header modification error
DResponse with header 'X-Middleware' set to 'next'
Attempts:
2 left
💡 Hint

Think about how NextResponse.next() allows header modification.

🧠 Conceptual
expert
2:00remaining
Which statement about response modification in Next.js App Router is true?

Choose the correct statement about modifying responses in Next.js App Router server actions or route handlers.

AResponse objects are immutable; to change status or headers, create a new Response with desired options
BYou can modify the response headers after returning the Response object by mutating it asynchronously
CNext.js automatically merges headers set in multiple middleware and route handlers without conflicts
DYou must always use the <code>res</code> object from Express to modify responses in Next.js App Router
Attempts:
2 left
💡 Hint

Think about immutability of Response objects in web standards.