Complete the code to import NextResponse from Next.js.
import { [1] } from 'next/server';
The NextResponse is imported from 'next/server' to handle redirects and rewrites in Next.js middleware.
Complete the code to redirect users to '/login' if they are not authenticated.
import { NextResponse } from 'next/server'; export function middleware(request) { const isAuth = false; // example check if (!isAuth) { return NextResponse.[1]('/login'); } return NextResponse.next(); }
Use NextResponse.redirect() to send users to a different URL.
Fix the error in the middleware to rewrite the URL to '/dashboard' without changing the browser URL.
import { NextResponse } from 'next/server'; export function middleware(request) { return NextResponse.[1]('/dashboard'); }
Use NextResponse.rewrite() to change the destination internally without changing the URL shown in the browser.
Fill both blanks to check if the request pathname starts with '/admin' and redirect to '/login' if true.
import { NextResponse } from 'next/server'; export function middleware(request) { const { pathname } = request.nextUrl; if (pathname.[1]('/admin')) { return NextResponse.[2]('/login'); } return NextResponse.next(); }
Use startsWith to check the beginning of the pathname and redirect to send the user to '/login'.
Fill all three blanks to rewrite requests from '/blog/:slug' to '/news/:slug' preserving the slug parameter.
import { NextResponse } from 'next/server'; export function middleware(request) { const url = request.nextUrl.clone(); if (url.pathname.[1]('/blog/')) { url.pathname = url.pathname.[2]('/blog', '/news'); return NextResponse.[3](url); } return NextResponse.next(); }
Check if the path starts with '/blog/', replace '/blog' with '/news' in the path, then rewrite the request to the new URL.