0
0
NextJSframework~20 mins

Redirect and rewrite in middleware 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
What happens when this Next.js middleware redirects?

Consider this Next.js middleware code snippet:

import { NextResponse } from 'next/server';

export function middleware(request) {
  const url = request.nextUrl.clone();
  if (url.pathname === '/old-path') {
    url.pathname = '/new-path';
    return NextResponse.redirect(url);
  }
  return NextResponse.next();
}

What will the user see if they visit /old-path?

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

export function middleware(request) {
  const url = request.nextUrl.clone();
  if (url.pathname === '/old-path') {
    url.pathname = '/new-path';
    return NextResponse.redirect(url);
  }
  return NextResponse.next();
}
AThe browser URL stays '/old-path' and the content of '/old-path' is shown.
BThe browser URL stays '/old-path' but the content of '/new-path' is shown.
CThe browser URL changes to '/new-path' but the content of '/old-path' is shown.
DThe browser URL changes to '/new-path' and the content of '/new-path' is shown.
Attempts:
2 left
💡 Hint

Redirect changes the URL in the browser and sends a new request.

component_behavior
intermediate
2:00remaining
What is the effect of this rewrite in Next.js middleware?

Look at this middleware code:

import { NextResponse } from 'next/server';

export function middleware(request) {
  const url = request.nextUrl.clone();
  if (url.pathname === '/about') {
    url.pathname = '/company-info';
    return NextResponse.rewrite(url);
  }
  return NextResponse.next();
}

If a user visits /about, what will happen?

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

export function middleware(request) {
  const url = request.nextUrl.clone();
  if (url.pathname === '/about') {
    url.pathname = '/company-info';
    return NextResponse.rewrite(url);
  }
  return NextResponse.next();
}
AThe browser URL changes to '/company-info' but the content from '/about' is shown.
BThe browser URL stays '/about' but the content from '/company-info' is shown.
CThe browser URL stays '/about' and the content from '/about' is shown.
DThe browser URL changes to '/company-info' and the content from '/company-info' is shown.
Attempts:
2 left
💡 Hint

Rewrite changes the resource served without changing the browser URL.

📝 Syntax
advanced
2:30remaining
Which middleware code correctly performs a conditional redirect in Next.js?

Choose the code snippet that correctly redirects users from '/login' to '/dashboard' only if they are authenticated.

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

export function middleware(request) {
  const url = request.nextUrl.clone();
  const isAuth = request.cookies.get('auth')?.value === true;
  if (url.pathname === '/login' && isAuth) {
    url.pathname = '/dashboard';
    return NextResponse.redirect(url);
  }
  return NextResponse.next();
}
B
import { NextResponse } from 'next/server';

export function middleware(request) {
  const url = request.nextUrl;
  const isAuth = request.cookies.get('auth') === 'true';
  if (url.pathname === '/login' && isAuth) {
    url.pathname = '/dashboard';
    return NextResponse.redirect(url);
  }
  return NextResponse.next();
}
C
import { NextResponse } from 'next/server';

export function middleware(request) {
  const url = request.nextUrl.clone();
  const isAuth = request.cookies.get('auth')?.value === 'true';
  if (url.pathname === '/login' && isAuth) {
    url.pathname = '/dashboard';
    return NextResponse.redirect(url);
  }
  return NextResponse.next();
}
D
import { NextResponse } from 'next/server';

export function middleware(request) {
  const url = request.nextUrl.clone();
  const isAuth = request.cookies.get('auth')?.value === 'true';
  if (url.pathname === '/login' || isAuth) {
    url.pathname = '/dashboard';
    return NextResponse.redirect(url);
  }
  return NextResponse.next();
}
Attempts:
2 left
💡 Hint

Check how cookies are accessed and the condition logic.

🔧 Debug
advanced
2:30remaining
Why does this Next.js middleware cause a runtime error?

Examine this middleware code:

import { NextResponse } from 'next/server';

export function middleware(request) {
  const url = request.nextUrl.clone();
  if (url.pathname === '/profile') {
    url.pathname = '/user/profile';
    return NextResponse.rewrite(url);
  }
  if (url.pathname === '/dashboard') {
    return NextResponse.redirect('/home');
  }
  return NextResponse.next();
}

When visiting '/dashboard', the app crashes with an error. What is the cause?

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

export function middleware(request) {
  const url = request.nextUrl.clone();
  if (url.pathname === '/profile') {
    url.pathname = '/user/profile';
    return NextResponse.rewrite(url);
  }
  if (url.pathname === '/dashboard') {
    return NextResponse.redirect('/home');
  }
  return NextResponse.next();
}
ANextResponse.redirect() requires a URL object, not a string, causing a runtime error.
BThe middleware does not handle '/dashboard' correctly because '/home' does not exist.
CNextResponse.rewrite() cannot be used with '/user/profile' path.
DCloning the URL causes the error when redirecting from '/dashboard'.
Attempts:
2 left
💡 Hint

Check the argument type passed to NextResponse.redirect().

state_output
expert
3:00remaining
What is the final URL and content served after this complex middleware runs?

Given this Next.js middleware:

import { NextResponse } from 'next/server';

export function middleware(request) {
  const url = request.nextUrl.clone();
  if (url.pathname.startsWith('/blog')) {
    url.pathname = url.pathname.replace('/blog', '/news');
    return NextResponse.rewrite(url);
  }
  if (url.pathname === '/old-home') {
    url.pathname = '/home';
    return NextResponse.redirect(url);
  }
  return NextResponse.next();
}

If a user visits /blog/post-1 and then visits /old-home, what URLs will the browser show and what content will be served in each case?

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

export function middleware(request) {
  const url = request.nextUrl.clone();
  if (url.pathname.startsWith('/blog')) {
    url.pathname = url.pathname.replace('/blog', '/news');
    return NextResponse.rewrite(url);
  }
  if (url.pathname === '/old-home') {
    url.pathname = '/home';
    return NextResponse.redirect(url);
  }
  return NextResponse.next();
}
AVisiting '/blog/post-1' shows URL '/blog/post-1' with content from '/news/post-1'. Visiting '/old-home' changes URL to '/home' and shows '/home' content.
BVisiting '/blog/post-1' changes URL to '/news/post-1' and shows '/news/post-1' content. Visiting '/old-home' keeps URL '/old-home' but shows '/home' content.
CVisiting '/blog/post-1' changes URL to '/news/post-1' but shows '/blog/post-1' content. Visiting '/old-home' changes URL to '/home' but shows '/old-home' content.
DVisiting '/blog/post-1' keeps URL '/blog/post-1' and shows '/blog/post-1' content. Visiting '/old-home' keeps URL '/old-home' and shows '/old-home' content.
Attempts:
2 left
💡 Hint

Remember rewrite keeps URL, redirect changes URL.