infoalloggi-monorepo/apps/infoalloggi/src/proxies/updater.ts
Marco Pedone 2111535d08 feat: refactor middleware and proxy structure for improved organization and functionality
- Removed old middleware files and replaced them with new proxy implementations for auth, API, password change, test redirection, and updater functionalities.
- Introduced a new proxy handler to streamline request processing through a chain of proxies.
- Updated package.json and package-lock.json to reflect dependency changes and ensure compatibility with the latest versions of Next.js, React, and related packages.
- Enhanced error handling and response management in the new proxy implementations.
2026-01-14 14:51:43 +01:00

34 lines
963 B
TypeScript

import { type NextRequest, NextResponse } from "next/server";
import type { ProxyFn } from "~/proxy";
const UPDATE_COODKIE_NAME = "app_version";
export const updaterProxy: ProxyFn = 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;
};