SurfSense/surfsense_web/lib/sunset.ts
Parman Mohammadalizadeh ba12e6b4e1 fix(sunset): gate the web redirect on DEPLOYMENT_MODE
shouldRedirectToSunset() now takes the deployment mode and returns false
unless it is cloud, mirroring is_sunset_mode(). proxy.ts resolves the mode
the way the runtime config does, so a stray SUNSET_MODE in a self-hosted
.env no longer sends every app route to /sunset.

Fixes #2003
2026-09-29 09:08:25 +02:00

49 lines
1.9 KiB
TypeScript

/**
* Sunset mode for the web app.
*
* At T-0 the hosted service becomes export-only, so the app itself has nothing
* left to do: every signed-in route sends the user to `/sunset`, which carries
* the export button, the download links and the import steps.
*
* Deliberately **not** `NEXT_PUBLIC_SUNSET_MODE`. `NEXT_PUBLIC_*` values are
* inlined at build time, which would mean rebuilding and redeploying the image
* to sunset -- while the backend reads its flag per request precisely so the
* flip needs no deploy. This is read in middleware, which runs per request, so
* both halves are thrown the same way: one variable, set in two places.
*/
import { isPublicRoute } from "@/lib/public-routes";
// The same spellings the backend accepts, for the same reason: the switch is
// thrown once, under time pressure, and a value that quietly reads as false
// would leave the app running as normal with nothing to show it had failed.
const TRUTHY = new Set(["1", "true", "yes", "on"]);
export function isSunsetMode(value: string | undefined | null): boolean {
return TRUTHY.has(
String(value ?? "")
.trim()
.toLowerCase()
);
}
/**
* Whether this request should be sent to `/sunset`.
*
* "Public" here means the wind-down portal itself -- `/sunset`, `/license`,
* `/pricing`, the landing page, the blog. Those have to stay reachable, since
* they are where a sunset user is being sent. Everything else is the app,
* which has no hosted service left behind it.
*
* Like the backend's `is_sunset_mode()`, the flag only counts on a cloud
* deployment, so a stray `SUNSET_MODE` in a self-hosted `.env` is a no-op.
*/
export function shouldRedirectToSunset(
pathname: string,
flag: string | undefined | null,
deploymentMode: string | undefined | null
): boolean {
if (deploymentMode !== "cloud") return false;
if (!isSunsetMode(flag)) return false;
return !isPublicRoute(pathname);
}