feat: add updater middleware for managing app version cookies
This commit is contained in:
parent
66a70b92cc
commit
e8957e6f3b
3 changed files with 37 additions and 2 deletions
|
|
@ -34,12 +34,12 @@ export const env = createEnv({
|
|||
client: {
|
||||
NEXT_PUBLIC_BASE_URL: z.string(),
|
||||
NEXT_PUBLIC_STRIPE_PUBLIC_KEY: z.string(),
|
||||
//NEXT_PUBLIC_TEST: z.string(),
|
||||
},
|
||||
|
||||
// If you're using Next.js < 13.4.4, you'll need to specify the runtimeEnv manually
|
||||
experimental__runtimeEnv: {
|
||||
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
|
||||
NEXT_PUBLIC_STRIPE_PUBLIC_KEY: process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY,
|
||||
//NEXT_PUBLIC_TEST: process.env.NEXT_PUBLIC_TEST,
|
||||
},
|
||||
skipValidation: process.env.SKIP_ENV_VALIDATION === "1" || false,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ export async function middleware(request: NextRequest) {
|
|||
//console.log("Middleware triggered for:", request.nextUrl.pathname);
|
||||
|
||||
return await chainMiddlewares(request, [
|
||||
//updaterMiddleware,
|
||||
apisMiddleware,
|
||||
pswChangeMiddleware,
|
||||
authMiddleware,
|
||||
|
|
|
|||
34
apps/infoalloggi/src/middlewares/updater.ts
Normal file
34
apps/infoalloggi/src/middlewares/updater.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import type { MiddlewareFn } from "~/middleware";
|
||||
|
||||
const UPDATE_COODKIE_NAME = "app_version";
|
||||
|
||||
export const updaterMiddleware: MiddlewareFn = async (req: NextRequest) => {
|
||||
const url = req.nextUrl.clone();
|
||||
|
||||
const updateToken = req.cookies.get(UPDATE_COODKIE_NAME)?.value;
|
||||
if (!process.env.NEXT_PUBLIC_TEST) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!updateToken) {
|
||||
// First time visitor or cookie not set, set the cookie
|
||||
const res = NextResponse.rewrite(url);
|
||||
res.cookies.set(UPDATE_COODKIE_NAME, process.env.NEXT_PUBLIC_TEST, {
|
||||
maxAge: 60 * 60 * 24 * 30, // 30 days
|
||||
path: "/",
|
||||
});
|
||||
return res;
|
||||
}
|
||||
if (updateToken !== process.env.NEXT_PUBLIC_TEST) {
|
||||
// Version has changed, perform a hard refresh and update the cookie
|
||||
const res = NextResponse.rewrite(url);
|
||||
res.cookies.set(UPDATE_COODKIE_NAME, process.env.NEXT_PUBLIC_TEST, {
|
||||
maxAge: 60 * 60 * 24 * 30, // 30 days
|
||||
path: "/",
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue