Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Add version check and API endpoint for app version (#838)

Goldflag66a64f97ee2f
diff --git a/client/src/app/layout.tsx b/client/src/app/layout.tsx
index f51226dbe..e6a3770bb 100644
--- a/client/src/app/layout.tsx
+++ b/client/src/app/layout.tsx
@@ -10,6 +10,7 @@ import { NuqsAdapter } from "nuqs/adapters/next/app";
import { AuthenticationGuard } from "../components/AuthenticationGuard";
import { OrganizationInitializer } from "../components/OrganizationInitializer";
import { Toaster } from "../components/ui/sonner";
+import { VersionCheck } from "../components/VersionCheck";
import { TooltipProvider } from "../components/ui/tooltip";
import { cn } from "../lib/utils";
import "./globals.css";
@@ -35,6 +36,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<AuthenticationGuard />
{children}
</QueryProvider>
+ <VersionCheck />
<Toaster />
</TooltipProvider>
</ThemeProvider>
diff --git a/client/src/components/VersionCheck.tsx b/client/src/components/VersionCheck.tsx
new file mode 100644
index 000000000..a31479cab
--- /dev/null
+++ b/client/src/components/VersionCheck.tsx
@@ -0,0 +1,54 @@
+"use client";
+
+import { useEffect } from "react";
+import { toast } from "sonner";
+
+import { IS_CLOUD } from "../lib/const";
+import packageJson from "../../package.json";
+
+export function VersionCheck() {
+ useEffect(() => {
+ if (IS_CLOUD) return;
+ if (sessionStorage.getItem("version-check-done")) return;
+
+ sessionStorage.setItem("version-check-done", "1");
+
+ fetch("https://app.rybbit.io/api/version")
+ .then((res) => res.json())
+ .then((data: { version: string }) => {
+ const latest = data.version;
+ const current = packageJson.version;
+
+ console.log("latest", latest);
+ console.log("current", current);
+ if (latest && latest !== current && isNewer(latest, current)) {
+ toast.info(`Rybbit v${latest} is available (you're on v${current})`, {
+ duration: Infinity,
+ action: {
+ label: "Upgrade",
+ onClick: () =>
+ window.open(
+ "https://www.rybbit.io/docs/self-hosting",
+ "_blank"
+ ),
+ },
+ });
+ }
+ })
+ .catch(() => {
+ // Silently ignore - user may be offline or app.rybbit.io unreachable
+ });
+ }, []);
+
+ return null;
+}
+
+function isNewer(latest: string, current: string): boolean {
+ const l = latest.split(".").map(Number);
+ const c = current.split(".").map(Number);
+ for (let i = 0; i < 3; i++) {
+ if ((l[i] ?? 0) > (c[i] ?? 0)) return true;
+ if ((l[i] ?? 0) < (c[i] ?? 0)) return false;
+ }
+ return false;
+}
diff --git a/server/src/api/getConfig.ts b/server/src/api/getConfig.ts
index 602ff9935..3f5e8377b 100644
--- a/server/src/api/getConfig.ts
+++ b/server/src/api/getConfig.ts
@@ -1,9 +1,17 @@
import { FastifyRequest, FastifyReply } from "fastify";
+import { createRequire } from "module";
import { DISABLE_SIGNUP, MAPBOX_TOKEN } from "../lib/const.js";
+const require = createRequire(import.meta.url);
+const { version } = require("../../package.json");
+
export async function getConfig(_: FastifyRequest, reply: FastifyReply) {
return reply.send({
disableSignup: DISABLE_SIGNUP,
mapboxToken: MAPBOX_TOKEN,
});
}
+
+export async function getVersion(_: FastifyRequest, reply: FastifyReply) {
+ return reply.send({ version });
+}
diff --git a/server/src/index.ts b/server/src/index.ts
index 8a8f1bbb4..05de776e0 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -49,7 +49,7 @@ import {
getUsers,
updateGoal,
} from "./api/analytics/index.js";
-import { getConfig } from "./api/getConfig.js";
+import { getConfig, getVersion } from "./api/getConfig.js";
import {
connectGSC,
disconnectGSC,
@@ -316,6 +316,7 @@ async function organizationsRoutes(fastify: FastifyInstance) {
async function userRoutes(fastify: FastifyInstance) {
// User
fastify.get("/config", getConfig); // Public - returns app config
+ fastify.get("/version", getVersion); // Public - returns app version
fastify.get("/user/organizations", authOnly, getUserOrganizations);
fastify.post("/user/account-settings", authOnly, updateAccountSettings);
fastify.post("/user/unsubscribe-marketing", authOnly, unsubscribeMarketing);