0
0
NextJSframework~20 mins

Why middleware intercepts requests in NextJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does Next.js middleware intercept requests?

In Next.js, middleware runs before a request reaches a page or API route. What is the main reason middleware intercepts requests?

ATo delay the request processing until the user confirms an action
BTo modify the request or response, like redirecting or adding headers, before reaching the final handler
CTo automatically cache all requests on the client side
DTo convert all requests into static HTML pages
Attempts:
2 left
💡 Hint

Think about what middleware can do before the page or API handles the request.

component_behavior
intermediate
2:00remaining
What happens when middleware redirects a request?

Consider this Next.js middleware snippet that redirects users based on a condition. What will the user see if the condition is true?

export function middleware(request) {
  if (!request.cookies.get('token')) {
    return Response.redirect(new URL('/login', request.url))
  }
  return Response.next()
}
AThe user is sent to the /login page before the original page loads
BThe user sees the original page but with a warning message
CThe middleware blocks the request and shows a blank page
DThe user stays on the original page but the URL changes to /login
Attempts:
2 left
💡 Hint

Redirect means sending the user to a different page before loading the original one.

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

Given this middleware code, what header will the final response include?

export function middleware(request) {
  const response = Response.next()
  response.headers.set('X-Custom-Header', 'HelloWorld')
  return response
}
AThe response will not include any custom headers
BThe code will throw a runtime error because headers cannot be set this way
CThe response will include the header 'X-Custom-Header' with value 'HelloWorld'
DThe response will include 'X-Custom-Header' but with an empty value
Attempts:
2 left
💡 Hint

Think about how middleware can add headers to responses.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this middleware code

Which option correctly fixes the syntax error in this Next.js middleware?

export function middleware(request) {
  if request.cookies.get('user') {
    return Response.next()
  }
  return Response.redirect('/login')
}
AAdd a semicolon after the if statement condition
BChange 'if' to 'when' to fix the syntax
CRemove the curly braces after if statement
DAdd parentheses around the if condition: if (request.cookies.get('user')) { ... }
Attempts:
2 left
💡 Hint

JavaScript requires parentheses around conditions in if statements.

🔧 Debug
expert
3:00remaining
Why does this middleware cause a runtime error?

Examine this middleware code. Why does it cause a runtime error?

export function middleware(request) {
  if (!request.cookies.get('session')) {
    return Response.redirect('/signin')
  }
  const response = Response.next()
  response.headers.append('Set-Cookie', 'visited=true')
  return response
}
AResponse.next() returns a Response object whose headers are immutable, so append() causes an error
BThe redirect URL '/signin' is invalid and causes the error
Crequest.cookies.get() is not a function and causes a TypeError
DThe middleware must always return a string, not a Response object
Attempts:
2 left
💡 Hint

Consider if the headers of the Response object can be changed after Response.next()