0
0
NextJSframework~20 mins

Middleware.ts file convention in NextJS - Practice Problems & Coding Challenges

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!
component_behavior
intermediate
2:00remaining
Middleware.ts: What happens when a request matches the middleware?

Given a middleware.ts file in Next.js that intercepts requests, what is the expected behavior when a request URL matches the middleware's matcher pattern?

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

export function middleware(request) {
  if (request.nextUrl.pathname.startsWith('/admin')) {
    return NextResponse.redirect(new URL('/login', request.url));
  }
  return NextResponse.next();
}

export const config = {
  matcher: ['/admin/:path*'],
};
AThe middleware redirects the user to '/login' for any URL starting with '/admin'.
BThe middleware allows all requests to '/admin' to proceed without changes.
CThe middleware blocks all requests to '/admin' by returning a 404 response.
DThe middleware modifies the request headers but does not redirect.
Attempts:
2 left
💡 Hint

Look at the middleware function and what it returns when the path starts with '/admin'.

📝 Syntax
intermediate
2:00remaining
Middleware.ts: Identify the syntax error

Which option contains a syntax error in a Next.js middleware.ts file?

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

export function middleware(request) {
  if (request.nextUrl.pathname === '/home') {
    return NextResponse.rewrite('/dashboard');
  }
  return NextResponse.next();
}
Aexport function middleware(request) { return NextResponse.rewrite('/dashboard');
Bexport function middleware(request) { return NextResponse.redirect('/login'); }
Cexport function middleware(request) { return NextResponse.rewrite('/dashboard'); }
Dexport function middleware(request) { return NextResponse.next(); }
Attempts:
2 left
💡 Hint

Check for missing brackets or incomplete statements.

state_output
advanced
2:00remaining
Middleware.ts: What is the output URL after rewrite?

Given this middleware, what URL will the user see in the browser after accessing '/profile'?

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

export function middleware(request) {
  if (request.nextUrl.pathname === '/profile') {
    return NextResponse.rewrite('/user/profile');
  }
  return NextResponse.next();
}
AThe browser URL changes to '/user/profile'.
BThe browser URL changes to '/profile' and content from '/profile' is shown.
CThe browser URL remains '/profile' but content from '/user/profile' is shown.
DThe browser URL changes to '/user/profile' and content from '/profile' is shown.
Attempts:
2 left
💡 Hint

Consider how NextResponse.rewrite works compared to redirect.

🔧 Debug
advanced
2:00remaining
Middleware.ts: Why does this middleware cause an infinite redirect?

Examine this middleware code. Why does it cause an infinite redirect loop?

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

export function middleware(request) {
  if (request.nextUrl.pathname !== '/login') {
    return NextResponse.redirect(new URL('/login', request.url));
  }
  return NextResponse.next();
}
ABecause NextResponse.next() is missing parentheses.
BBecause the redirect URL is malformed and causes a server error.
CBecause the middleware does not return a response for '/login' paths.
DBecause it redirects all paths except '/login' to '/login', including requests already at '/login'.
Attempts:
2 left
💡 Hint

Think about what happens when the request is already at '/login'.

🧠 Conceptual
expert
2:00remaining
Middleware.ts: What is the purpose of the config.matcher property?

In a Next.js middleware.ts file, what does the config.matcher property control?

NextJS
export const config = {
  matcher: ['/dashboard/:path*', '/settings'],
};
AIt sets environment variables for the middleware execution.
BIt specifies which request paths the middleware should run on.
CIt defines the order in which multiple middleware files execute.
DIt controls the caching behavior of middleware responses.
Attempts:
2 left
💡 Hint

Think about when middleware runs for incoming requests.