Next.js Discord

Discord Forum

Error: Invariant: static generation store missing in revalidatePath

Unanswered
Tan posted this in #help-forum
Open in Discord
TanOP
Hi I am trying to implement handeling unauthorized request from nextmiddleware.
What I am trying to do is when user has invalid token or dont have any I am redirecting the user to home page i.e Landing Page.

So my middleware looks like this :
import { NextRequest, NextResponse } from "next/server";
import { getLocale } from "./actions/locale.action";
import { handleUnauthorized, isPrivateRoute } from "./lib/middleware";
import { getSession } from "./actions/auth.action";

export async function middleware(request: NextRequest) {
  const pathname = request.nextUrl.pathname;
  if (/\.(png|svg|jpg|webp|mp3|geojson)$/.test(pathname)) return;
  let locale = getLocale(request);
  const newUrl = new URL(`/${locale}${pathname}`, request.nextUrl);

  if (isPrivateRoute(pathname)) {
    const sesson = await getSession();
    if (!sesson) {
      return handleUnauthorized(request);
    }

    /* TODO:
     * 1. Validate token and rewrite new url
     * 2. Use refresh token middleware
     * 3. .......
     * */
  }

  const response = NextResponse.rewrite(newUrl);
  response.headers.set("x-locale", locale);
  return response;
}

export const config = {
  matcher: ["/((?!_next|api|favicon.ico).*)"],
};

My unauthorizade handler is like this :

export const handleUnauthorized = async (req: NextRequest) => {
  const res = NextResponse.redirect(req.nextUrl.origin);
  res.cookies.delete("session");
  await revalidateHome();
  return res;
};


Where revalidate home is a server action that will revalidate the layout of "/" page. But I am getting Error: Invariant: static generation store missing in revalidatePath /.
What might be the reason.

0 Replies