Skip to content

Open-source status page, incident response and uptime monitoring

chore: react-query v5 + trpc v11 (#1029)

Maximilian Kaske34b0eded87f4
diff --git a/apps/web/package.json b/apps/web/package.json
index 693889db0..4d64a6ba2 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -23,7 +23,6 @@
"@openstatus/emails": "workspace:*",
"@openstatus/error": "workspace:*",
"@openstatus/header-analysis": "workspace:*",
- "@openstatus/next-monitoring": "0.0.4",
"@openstatus/notification-discord": "workspace:*",
"@openstatus/notification-emails": "workspace:*",
"@openstatus/notification-pagerduty": "workspace:*",
@@ -41,12 +40,14 @@
"@t3-oss/env-nextjs": "0.7.0",
"@tailwindcss/container-queries": "0.1.1",
"@tailwindcss/typography": "0.5.10",
+ "@tanstack/react-query": "^5.59.0",
+ "@tanstack/react-query-devtools": "^5.59.0",
"@tanstack/react-table": "8.10.3",
"@tremor/react": "3.17.4",
- "@trpc/client": "10.45.2",
- "@trpc/next": "10.45.2",
- "@trpc/react-query": "10.45.2",
- "@trpc/server": "10.45.2",
+ "@trpc/client": "11.0.0-rc.553",
+ "@trpc/next": "11.0.0-rc.553",
+ "@trpc/react-query": "11.0.0-rc.553",
+ "@trpc/server": "11.0.0-rc.553",
"@unkey/api": "0.23.0",
"@upstash/qstash": "2.6.2",
"@upstash/redis": "1.22.1",
diff --git a/apps/web/src/app/layout.tsx b/apps/web/src/app/layout.tsx
index 04b7daa57..ac27ed9c8 100644
--- a/apps/web/src/app/layout.tsx
+++ b/apps/web/src/app/layout.tsx
@@ -5,7 +5,6 @@ import { Inter } from "next/font/google";
import LocalFont from "next/font/local";
import { Toaster } from "@/components/ui/sonner";
-import { OpenStatusProvider } from "@openstatus/next-monitoring";
import {
defaultMetadata,
@@ -14,6 +13,7 @@ import {
} from "@/app/shared-metadata";
import { TailwindIndicator } from "@/components/tailwind-indicator";
import { ThemeProvider } from "@/components/theme-provider";
+import { TRPCReactQueryProvider } from "@/trpc/rq-client";
import Background from "./_components/background";
const inter = Inter({ subsets: ["latin"] });
@@ -47,9 +47,11 @@ export default function RootLayout({
} ${calSans.variable}`}
>
<ThemeProvider attribute="class" defaultTheme="light" enableSystem>
- <Background>{children}</Background>
- <Toaster richColors closeButton />
- <TailwindIndicator />
+ <TRPCReactQueryProvider>
+ <Background>{children}</Background>
+ <Toaster richColors closeButton />
+ <TailwindIndicator />
+ </TRPCReactQueryProvider>
</ThemeProvider>
</body>
</html>
diff --git a/apps/web/src/trpc/client.ts b/apps/web/src/trpc/client.ts
index ecb254c02..3fd47a009 100644
--- a/apps/web/src/trpc/client.ts
+++ b/apps/web/src/trpc/client.ts
@@ -1,12 +1,10 @@
-import { createTRPCProxyClient, loggerLink } from "@trpc/client";
-import superjson from "superjson";
+import { createTRPCClient, loggerLink } from "@trpc/client";
import type { AppRouter } from "@openstatus/api";
import { endingLink } from "./shared";
-export const api = createTRPCProxyClient<AppRouter>({
- transformer: superjson,
+export const api = createTRPCClient<AppRouter>({
links: [
loggerLink({
enabled: (opts) =>
diff --git a/apps/web/src/trpc/query-client.ts b/apps/web/src/trpc/query-client.ts
new file mode 100644
index 000000000..4637b84ce
--- /dev/null
+++ b/apps/web/src/trpc/query-client.ts
@@ -0,0 +1,24 @@
+import {
+ QueryClient,
+ defaultShouldDehydrateQuery,
+} from "@tanstack/react-query";
+import superjson from "superjson";
+
+export function makeQueryClient() {
+ return new QueryClient({
+ defaultOptions: {
+ queries: {
+ staleTime: 30 * 1000,
+ },
+ dehydrate: {
+ serializeData: superjson.serialize,
+ shouldDehydrateQuery: (query) =>
+ defaultShouldDehydrateQuery(query) ||
+ query.state.status === "pending",
+ },
+ hydrate: {
+ deserializeData: superjson.deserialize,
+ },
+ },
+ });
+}
diff --git a/apps/web/src/trpc/rq-client.tsx b/apps/web/src/trpc/rq-client.tsx
new file mode 100644
index 000000000..ea6175fcd
--- /dev/null
+++ b/apps/web/src/trpc/rq-client.tsx
@@ -0,0 +1,53 @@
+"use client";
+
+import type { AppRouter } from "@openstatus/api";
+import type { QueryClient } from "@tanstack/react-query";
+import { QueryClientProvider } from "@tanstack/react-query";
+import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
+import { createTRPCReact } from "@trpc/react-query";
+import { useState } from "react";
+import { makeQueryClient } from "./query-client";
+import { endingLink } from "./shared";
+
+export const api = createTRPCReact<AppRouter>();
+let clientQueryClientSingleton: QueryClient;
+function getQueryClient() {
+ if (typeof window === "undefined") {
+ // Server: always make a new query client
+ return makeQueryClient();
+ }
+ // Browser: use singleton pattern to keep the same query client
+ // biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
+ return (clientQueryClientSingleton ??= makeQueryClient());
+}
+
+export function TRPCReactQueryProvider(
+ props: Readonly<{
+ children: React.ReactNode;
+ }>,
+) {
+ // NOTE: Avoid useState when initializing the query client if you don't
+ // have a suspense boundary between this and the code that may
+ // suspend because React will throw away the client on the initial
+ // render if it suspends and there is no boundary
+ const queryClient = getQueryClient();
+ const [trpcClient] = useState(() =>
+ api.createClient({
+ links: [
+ endingLink({
+ headers: {
+ "x-trpc-source": "client",
+ },
+ }),
+ ],
+ }),
+ );
+ return (
+ <api.Provider client={trpcClient} queryClient={queryClient}>
+ <QueryClientProvider client={queryClient}>
+ {props.children}
+ <ReactQueryDevtools initialIsOpen={false} />
+ </QueryClientProvider>
+ </api.Provider>
+ );
+}
diff --git a/apps/web/src/trpc/rq-server.ts b/apps/web/src/trpc/rq-server.ts
new file mode 100644
index 000000000..9021f5297
--- /dev/null
+++ b/apps/web/src/trpc/rq-server.ts
@@ -0,0 +1,30 @@
+import "server-only";
+
+import { auth } from "@/lib/auth";
+import { type AppRouter, appRouter, t } from "@openstatus/api";
+import type { Context } from "@openstatus/api/src/trpc";
+import { db } from "@openstatus/db";
+import { createHydrationHelpers } from "@trpc/react-query/rsc";
+import { cache } from "react";
+import { makeQueryClient } from "./query-client";
+
+const createContextCached = cache(
+ async (...args: unknown[]): Promise<Context> => {
+ const session = await auth();
+
+ return {
+ req: undefined,
+ db,
+ session,
+ };
+ },
+);
+
+// IMPORTANT: Create a stable getter for the query client that
+// will return the same client during the same request.
+export const getQueryClient = cache(makeQueryClient);
+const caller = t.createCallerFactory(appRouter)(createContextCached);
+export const { trpc: api, HydrateClient } = createHydrationHelpers<AppRouter>(
+ caller,
+ getQueryClient,
+);
diff --git a/apps/web/src/trpc/server.ts b/apps/web/src/trpc/server.ts
index e10a2a76d..69d2e3e6e 100644
--- a/apps/web/src/trpc/server.ts
+++ b/apps/web/src/trpc/server.ts
@@ -1,13 +1,11 @@
-import { createTRPCProxyClient, loggerLink } from "@trpc/client";
+import { createTRPCClient, loggerLink } from "@trpc/client";
import { headers } from "next/headers";
-import superjson from "superjson";
import type { AppRouter } from "@openstatus/api";
import { endingLink } from "./shared";
-export const api = createTRPCProxyClient<AppRouter>({
- transformer: superjson,
+export const api = createTRPCClient<AppRouter>({
links: [
loggerLink({
enabled: (opts) =>
diff --git a/apps/web/src/trpc/shared.ts b/apps/web/src/trpc/shared.ts
index bf0fc407c..099b5a3da 100644
--- a/apps/web/src/trpc/shared.ts
+++ b/apps/web/src/trpc/shared.ts
@@ -2,6 +2,7 @@ import type { HTTPBatchLinkOptions, HTTPHeaders, TRPCLink } from "@trpc/client";
import { httpBatchLink } from "@trpc/client";
import type { AppRouter } from "@openstatus/api";
+import superjson from "superjson";
const getBaseUrl = () => {
if (typeof window !== "undefined") return "";
@@ -18,7 +19,9 @@ export const endingLink = (opts?: {
((runtime) => {
const sharedOpts = {
headers: opts?.headers, // REMINDER: fails when trying to `getTotalActiveMonitors()`
- } satisfies Partial<HTTPBatchLinkOptions>;
+ transformer: superjson,
+ // biome-ignore lint/suspicious/noExplicitAny: FIXME: remove any
+ } satisfies Partial<HTTPBatchLinkOptions<any>>;
const edgeLink = httpBatchLink({
...sharedOpts,
diff --git a/packages/api/index.ts b/packages/api/index.ts
index a2a043101..10ea19830 100644
--- a/packages/api/index.ts
+++ b/packages/api/index.ts
@@ -8,6 +8,7 @@ export { createTRPCContext, createInnerTRPCContext } from "./src/trpc";
export { t } from "./src/trpc";
export type { AppRouter } from "./src/root";
+export { appRouter } from "./src/root";
/**
* Inference helpers for input types
diff --git a/packages/api/package.json b/packages/api/package.json
index 3a1763dc3..a0520de6f 100644
--- a/packages/api/package.json
+++ b/packages/api/package.json
@@ -14,8 +14,8 @@
"@openstatus/error": "workspace:*",
"@openstatus/tinybird": "workspace:*",
"@t3-oss/env-core": "0.7.0",
- "@trpc/client": "10.45.2",
- "@trpc/server": "10.45.2",
+ "@trpc/client": "11.0.0-rc.553",
+ "@trpc/server": "11.0.0-rc.553",
"nanoid": "5.0.7",
"nanoid-dictionary": "5.0.0-beta.1",
"next": "14.2.4",
diff --git a/packages/api/src/root.ts b/packages/api/src/root.ts
index bd660b84f..5baf239fc 100644
--- a/packages/api/src/root.ts
+++ b/packages/api/src/root.ts
@@ -2,5 +2,5 @@ import { edgeRouter } from "./edge";
import { lambdaRouter } from "./lambda";
import { mergeRouters } from "./trpc";
-const appRouter = mergeRouters(edgeRouter, lambdaRouter);
+export const appRouter = mergeRouters(edgeRouter, lambdaRouter);
export type AppRouter = typeof appRouter;
diff --git a/packages/api/src/trpc.ts b/packages/api/src/trpc.ts
index 94ba8f4b1..32b7e3dc7 100644
--- a/packages/api/src/trpc.ts
+++ b/packages/api/src/trpc.ts
@@ -1,4 +1,4 @@
-import { TRPCError, type inferAsyncReturnType, initTRPC } from "@trpc/server";
+import { TRPCError, initTRPC } from "@trpc/server";
import type { NextRequest } from "next/server";
import superjson from "superjson";
import { ZodError } from "zod";
@@ -65,7 +65,7 @@ export const createTRPCContext = async (opts: {
});
};
-export type Context = inferAsyncReturnType<typeof createTRPCContext>;
+export type Context = Awaited<ReturnType<typeof createTRPCContext>>;
/**
* 2. INITIALIZATION
@@ -190,7 +190,7 @@ export const formdataMiddleware = t.middleware(async (opts) => {
if (!formData) throw new TRPCError({ code: "BAD_REQUEST" });
return opts.next({
- rawInput: formData,
+ input: formData,
});
});
/**
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index df37176c7..36e915f4c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -204,9 +204,6 @@ importers:
'@openstatus/header-analysis':
specifier: workspace:*
version: link:../../packages/header-analysis
- '@openstatus/next-monitoring':
- specifier: 0.0.4
'@openstatus/notification-discord':
specifier: workspace:*
version: link:../../packages/notifications/discord
@@ -258,6 +255,12 @@ importers:
'@tailwindcss/typography':
specifier: 0.5.10
+ '@tanstack/react-query':
+ specifier: ^5.59.0
+ version: 5.59.0([email protected])
+ '@tanstack/react-query-devtools':
+ specifier: ^5.59.0
'@tanstack/react-table':
specifier: 8.10.3
@@ -265,17 +268,17 @@ importers:
specifier: 3.17.4
'@trpc/client':
- specifier: 10.45.2
- version: 10.45.2(@trpc/[email protected])
+ specifier: 11.0.0-rc.553
+ version: 11.0.0-rc.553(@trpc/[email protected])
'@trpc/next':
- specifier: 10.45.2
+ specifier: 11.0.0-rc.553
'@trpc/react-query':
- specifier: 10.45.2
+ specifier: 11.0.0-rc.553
'@trpc/server':
- specifier: 10.45.2
- version: 10.45.2
+ specifier: 11.0.0-rc.553
+ version: 11.0.0-rc.553
'@unkey/api':
specifier: 0.23.0
version: 0.23.0
@@ -492,11 +495,11 @@ importers:
specifier: 0.7.0
'@trpc/client':
- specifier: 10.45.2
- version: 10.45.2(@trpc/[email protected])
+ specifier: 11.0.0-rc.553
+ version: 11.0.0-rc.553(@trpc/[email protected])
'@trpc/server':
- specifier: 10.45.2
- version: 10.45.2
+ specifier: 11.0.0-rc.553
+ version: 11.0.0-rc.553
nanoid:
specifier: 5.0.7
version: 5.0.7
@@ -3254,12 +3257,6 @@ packages:
'@one-ini/[email protected]':
resolution: {integrity: sha512-XuySG1E38YScSJoMlqovLru4KTUNSjgVTIjyh7qMX6aNN5HY5Ct5LhRJdxO79JtTzKfzV/bnWpz+zquYrISsvw==}
- '@openstatus/[email protected]':
- resolution: {integrity: sha512-/UXWRt6hKfgGtuqvO9umBgBHDMIgs/Pkrrlt/3SWPGywkW7N5j7GGSCBwMzjs4SthPWBQRL1++lERZvaGkgEyA==}
- peerDependencies:
- next: '>=14.0.0'
- react: '>=18.2.0'
-
'@opentelemetry/[email protected]':
resolution: {integrity: sha512-9BJ8lMcOzEN0lu+Qji801y707oFO4xT3db6cosPvl+k7ItUHKN5ofWqtSbM9gbt1H4JJ/4/2TVrqI9Rq7hNv6Q==}
engines: {node: '>=14'}
@@ -4718,13 +4715,22 @@ packages:
peerDependencies:
tailwindcss: '>=3.0.0 || insiders'
- '@tanstack/[email protected]':
- resolution: {integrity: sha512-BteWYEPUcucEu3NBcDAgKuI4U25R9aPrHSP6YSf2NvaD2pSlIQTdqOfLRsxH9WdRYg7k0Uom35Uacb6nvbIMJg==}
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-WGD8uIhX6/deH/tkZqPNcRyAhDUqs729bWKoByYHSogcshXfFbppOdTER5+qY7mFvu8KEFJwT0nxr8RfPTVh0Q==}
+
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-iFdQEFXaYYxqgrv63ots+65FGI+tNp5ZS5PdMU1DWisxk3fez5HG3FyVlbUva+RdYS5hSLbxZ9aw3yEs97GNTw==}
- '@tanstack/[email protected]':
- resolution: {integrity: sha512-EhtBNA8GL3XFeSx6VYUjXQ96n44xe3JGKZCzBINrCYlxbZP6UwBafv7ti4eSRWc2Fy+fybQre0w17gR6lMzULA==}
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-Kz7577FQGU8qmJxROIT/aOwmkTcxfBqgTP6r1AIvuJxVMVHPkp8eQxWQ7BnfBsy/KTJHiV9vMtRVo1+R1tB3vg==}
peerDependencies:
- react: ^18.0.0
+ '@tanstack/react-query': ^5.59.0
+ react: ^18 || ^19
+
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-YDXp3OORbYR+8HNQx+lf4F73NoiCmCcSvZvgxE29OifmQFk0sBlO26NWLHpcNERo92tVk3w+JQ53/vkcRUY1hA==}
+ peerDependencies:
+ react: ^18 || ^19
'@tanstack/[email protected]':
resolution: {integrity: sha512-Qya1cJ+91arAlW7IRDWksRDnYw28O446jJ/ljkRSc663EaftJoBCAU10M+VV1K6MpCBLrXq1BD5IQc1zj/ZEjA==}
@@ -4798,33 +4804,38 @@ packages:
react: ^18.0.0
react-dom: '>=16.6.0'
- resolution: {integrity: sha512-ykALM5kYWTLn1zYuUOZ2cPWlVfrXhc18HzBDyRhoPYN0jey4iQHEFSEowfnhg1RvYnrAVjNBgHNeSAXjrDbGwg==}
+ resolution: {integrity: sha512-ICgziilbuAslRgHIVJZ162KaHQRpHXA1C1LPRsNpxPa9aStPF7eThAjn4V3JmDyJsuDLQgAmqLVRImlLPMYR5Q==}
peerDependencies:
- '@trpc/server': 10.45.2
+ '@trpc/server': 11.0.0-rc.553+9b629cc67
- resolution: {integrity: sha512-RSORmfC+/nXdmRY1pQ0AalsVgSzwNAFbZLYHiTvPM5QQ8wmMEHilseCYMXpu0se/TbPt9zVR6Ka2d7O6zxKkXg==}
+ resolution: {integrity: sha512-1bIuG4nKYmTuVFqgsqSbOGgIz2LsUcb1wXrK1zlfikRQHhssSMmpXSLSANfZffs/6lbFLVs057XaS9LX5HmCsw==}
peerDependencies:
- '@tanstack/react-query': ^4.18.0
- '@trpc/client': 10.45.2
- '@trpc/react-query': 10.45.2
- '@trpc/server': 10.45.2
+ '@tanstack/react-query': ^5.54.1
+ '@trpc/client': 11.0.0-rc.553+9b629cc67
+ '@trpc/react-query': 11.0.0-rc.553+9b629cc67
+ '@trpc/server': 11.0.0-rc.553+9b629cc67
next: '*'
react: '>=16.8.0'
react-dom: '>=16.8.0'
+ peerDependenciesMeta:
+ '@tanstack/react-query':
+ optional: true
+ '@trpc/react-query':
+ optional: true
- resolution: {integrity: sha512-BAqb9bGZIscroradlNx+Cc9522R+idY3BOSf5z0jHUtkxdMbjeGKxSSMxxu7JzoLqSIEC+LVzL3VvF8sdDWaZQ==}
+ resolution: {integrity: sha512-m88HBhpfIDJccJCXycHtkGS82RG2TVPsZUFIWswR8m6ip5eFNmqWASanu7DiiFbY6sufLT1S8ee28towrPUgoA==}
peerDependencies:
- '@tanstack/react-query': ^4.18.0
- '@trpc/client': 10.45.2
- '@trpc/server': 10.45.2
- react: '>=16.8.0'
- react-dom: '>=16.8.0'
+ '@tanstack/react-query': ^5.54.1
+ '@trpc/client': 11.0.0-rc.553+9b629cc67
+ '@trpc/server': 11.0.0-rc.553+9b629cc67
+ react: '>=18.2.0'
+ react-dom: '>=18.2.0'
- resolution: {integrity: sha512-wOrSThNNE4HUnuhJG6PfDRp4L2009KDVxsd+2VYH8ro6o/7/jwYZ8Uu5j+VaW+mOmc8EHerHzGcdbGNQSAUPgg==}
+ resolution: {integrity: sha512-pYUhxV3QU834jP1ugJk1uhtPwwaY1ymIuupb4DiWOhNCl68syuXPlUdD8IYsYKd3BjLkmnjlsOUMoyd/aoOF/Q==}
'@tsconfig/[email protected]':
resolution: {integrity: sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==}
@@ -12533,11 +12544,6 @@ snapshots:
'@one-ini/[email protected]': {}
- dependencies:
- react: 18.3.1
-
'@opentelemetry/[email protected]':
dependencies:
'@opentelemetry/api': 1.8.0
@@ -14492,11 +14498,19 @@ snapshots:
postcss-selector-parser: 6.0.10
- '@tanstack/[email protected]': {}
+ '@tanstack/[email protected]': {}
+
+ '@tanstack/[email protected]': {}
+
+ dependencies:
+ '@tanstack/query-devtools': 5.58.0
+ '@tanstack/react-query': 5.59.0([email protected])
+ react: 18.3.1
dependencies:
- '@tanstack/query-core': 5.36.1
+ '@tanstack/query-core': 5.59.0
react: 18.3.1
@@ -14567,29 +14581,30 @@ snapshots:
transitivePeerDependencies:
- tailwindcss
dependencies:
- '@trpc/server': 10.45.2
+ '@trpc/server': 11.0.0-rc.553
dependencies:
- '@tanstack/react-query': 5.37.1([email protected])
- '@trpc/client': 10.45.2(@trpc/[email protected])
- '@trpc/server': 10.45.2
+ '@trpc/client': 11.0.0-rc.553(@trpc/[email protected])
+ '@trpc/server': 11.0.0-rc.553
react: 18.3.1
react-dom: 18.3.1([email protected])
+ optionalDependencies:
+ '@tanstack/react-query': 5.59.0([email protected])
dependencies:
- '@tanstack/react-query': 5.37.1([email protected])
- '@trpc/client': 10.45.2(@trpc/[email protected])
- '@trpc/server': 10.45.2
+ '@tanstack/react-query': 5.59.0([email protected])
+ '@trpc/client': 11.0.0-rc.553(@trpc/[email protected])
+ '@trpc/server': 11.0.0-rc.553
react: 18.3.1
react-dom: 18.3.1([email protected])
- '@trpc/[email protected]': {}
+ '@trpc/[email protected]': {}
'@tsconfig/[email protected]': {}