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?
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(); }
Redirect changes the URL in the browser and sends a new request.
Using NextResponse.redirect() sends a redirect response to the browser, which updates the URL to the new path and loads that page's content.
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?
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(); }
Rewrite changes the resource served without changing the browser URL.
NextResponse.rewrite() changes the path the server uses to fetch content but keeps the browser URL unchanged.
Choose the code snippet that correctly redirects users from '/login' to '/dashboard' only if they are authenticated.
Check how cookies are accessed and the condition logic.
Option C correctly clones the URL, accesses the cookie value as a string, and uses && to check both conditions. Other options have errors in cookie access or logic.
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?
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(); }
Check the argument type passed to NextResponse.redirect().
NextResponse.redirect() expects a URL object or absolute URL string. Passing a relative string like '/home' causes a runtime error.
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?
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(); }
Remember rewrite keeps URL, redirect changes URL.
Rewrite changes the resource served but keeps the URL. Redirect changes the URL and resource. So '/blog/post-1' URL stays but content is from '/news/post-1'. '/old-home' redirects to '/home' so URL and content change.