Skip to content
QuiznExam

Middleware in Next.js

Test your knowledge of Middleware in Next.js, including request interception, authentication, redirects, rewrites, cookies, headers, matchers, and middleware configuration.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the primary purpose of Middleware in Next.js?

    • A. To intercept and process requests before they reach a route
    • B. To create database tables
    • C. To compile CSS files
    • D. To replace React components
    Show answer

    A. To intercept and process requests before they reach a route

    Middleware can run logic on incoming requests before the request is handled by the destination route.

  2. 2. Which function is commonly used to create a Next.js middleware response?

    • A. NextResponse
    • B. NextMiddleware
    • C. ResponseNext
    • D. MiddlewareResponse
    Show answer

    A. NextResponse

    NextResponse provides helpers for creating responses, redirects, rewrites, and other middleware behavior.

  3. 3. Which object provides information about the incoming request in Next.js Middleware?

    • A. NextRequest
    • B. NextRoute
    • C. RequestContext
    • D. NextContext
    Show answer

    A. NextRequest

    NextRequest extends the standard Request API with Next.js-specific functionality useful in Middleware.

  4. 4. Which function can be used to allow a request to continue to its destination?

    • A. NextResponse.next()
    • B. NextResponse.continue()
    • C. NextResponse.forward()
    • D. NextResponse.allow()
    Show answer

    A. NextResponse.next()

    NextResponse.next() indicates that processing should continue to the next stage of the request.

  5. 5. Which Next.js Middleware feature is commonly used to send a user to another URL?

    • A. redirect
    • B. forward
    • C. navigate
    • D. transfer
    Show answer

    A. redirect

    NextResponse.redirect() can be used in Middleware to redirect a request to another URL.

  6. 6. What is a common use case for Next.js Middleware?

    • A. Authentication and authorization checks
    • B. Creating React components
    • C. Compiling TypeScript manually
    • D. Installing npm packages
    Show answer

    A. Authentication and authorization checks

    Middleware is often used to inspect requests and apply authentication or authorization logic before allowing access.

  7. 7. Which Middleware feature allows a request to be internally served by a different route without changing the browser URL?

    • A. Redirect
    • B. Rewrite
    • C. Refresh
    • D. ForwardURL
    Show answer

    B. Rewrite

    A rewrite changes the destination used to handle a request while keeping the original URL visible to the browser.

  8. 8. Which configuration option can be used to specify which routes Middleware should run on?

    • A. routes
    • B. matcher
    • C. paths
    • D. filter
    Show answer

    B. matcher

    The matcher configuration allows Middleware to be applied selectively to specified URL patterns.

  9. 9. What can Middleware inspect from an incoming request?

    • A. Cookies and headers
    • B. Only React state
    • C. Only database records
    • D. Only CSS variables
    Show answer

    A. Cookies and headers

    NextRequest provides access to request information such as headers, cookies, URL details, and other request properties.

  10. 10. Which statement about Middleware execution is correct?

    • A. It runs only after the page is rendered
    • B. It can run before a request reaches the destination route
    • C. It runs only in the browser
    • D. It is executed only when a database changes
    Show answer

    B. It can run before a request reaches the destination route

    Middleware operates in the request-processing pipeline and can modify or respond to a request before the destination is reached.

  11. 11. Which file convention has traditionally been used for defining Next.js Middleware?

    • A. middleware.js
    • B. request.js
    • C. server-middleware.js
    • D. next-middleware.js
    Show answer

    A. middleware.js

    The traditional Next.js Middleware convention uses a middleware.js or middleware.ts file at the appropriate project root location.

  12. 12. Which HTTP request property can be used in Middleware to inspect the requested URL?

    • A. request.nextUrl
    • B. request.pageUrl
    • C. request.routeUrl
    • D. request.currentPage
    Show answer

    A. request.nextUrl

    NextRequest provides nextUrl, which exposes parsed URL information and Next.js-specific URL functionality.

  13. 13. How can Middleware commonly check whether a user has an authentication cookie?

    • A. Using request.cookies
    • B. Using request.authOnly
    • C. Using request.sessionOnly
    • D. Using request.storage
    Show answer

    A. Using request.cookies

    Middleware can inspect cookies on the incoming request through the cookies API available on NextRequest.

  14. 14. What should Middleware generally do after determining that a request is allowed to continue?

    • A. Call NextResponse.next()
    • B. Always redirect the request
    • C. Terminate the server
    • D. Delete the request
    Show answer

    A. Call NextResponse.next()

    When no redirect, rewrite, or other response is required, NextResponse.next() allows request processing to continue.

  15. 15. Which statement best describes the role of Next.js Middleware?

    • A. It is mainly a database management layer
    • B. It provides request-time logic that can influence routing and responses
    • C. It replaces all API routes
    • D. It is only used for styling pages
    Show answer

    B. It provides request-time logic that can influence routing and responses

    Middleware enables request-time processing such as authentication checks, redirects, rewrites, and request or response manipulation.