Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(mobile): prevent app hanging behind reverse proxies and fix Hermes compat (#2559)

mrInvincible293c70a57af70d
diff --git a/apps/cli/src/lib/trpc.ts b/apps/cli/src/lib/trpc.ts
index deb5c392..e96fed95 100644
--- a/apps/cli/src/lib/trpc.ts
+++ b/apps/cli/src/lib/trpc.ts
@@ -3,6 +3,7 @@ import { createTRPCClient, httpBatchLink } from "@trpc/client";
import superjson from "superjson";
import type { AppRouter } from "@karakeep/trpc/routers/_app";
+import { TRPC_MAX_URL_LENGTH_INTERNAL } from "@karakeep/shared/trpc";
export function getAPIClient() {
const globals = getGlobalOptions();
@@ -10,7 +11,7 @@ export function getAPIClient() {
links: [
httpBatchLink({
url: `${globals.serverAddr}/api/trpc`,
- maxURLLength: 14000,
+ maxURLLength: TRPC_MAX_URL_LENGTH_INTERNAL,
transformer: superjson,
headers() {
return {
@@ -27,7 +28,7 @@ export function getAPIClientFor(opts: { serverAddr: string; apiKey: string }) {
links: [
httpBatchLink({
url: `${opts.serverAddr}/api/trpc`,
- maxURLLength: 14000,
+ maxURLLength: TRPC_MAX_URL_LENGTH_INTERNAL,
transformer: superjson,
headers() {
return {
diff --git a/apps/web/lib/providers.tsx b/apps/web/lib/providers.tsx
index 8e247f6f..c794d996 100644
--- a/apps/web/lib/providers.tsx
+++ b/apps/web/lib/providers.tsx
@@ -12,7 +12,10 @@ import superjson from "superjson";
import type { ClientConfig } from "@karakeep/shared/config";
import type { AppRouter } from "@karakeep/trpc/routers/_app";
-import { TRPCProvider } from "@karakeep/shared-react/trpc";
+import {
+ TRPC_MAX_URL_LENGTH_INTERNAL,
+ TRPCProvider,
+} from "@karakeep/shared-react/trpc";
import { ClientConfigCtx } from "./clientConfig";
import CustomI18nextProvider from "./i18n/provider";
@@ -69,7 +72,7 @@ export default function Providers({
httpBatchLink({
// TODO: Change this to be a full URL exposed as a client side setting
url: `/api/trpc`,
- maxURLLength: 14000,
+ maxURLLength: TRPC_MAX_URL_LENGTH_INTERNAL,
transformer: superjson,
}),
],
diff --git a/packages/shared-react/providers/trpc-provider.tsx b/packages/shared-react/providers/trpc-provider.tsx
index 5728fca5..c5b4e8b9 100644
--- a/packages/shared-react/providers/trpc-provider.tsx
+++ b/packages/shared-react/providers/trpc-provider.tsx
@@ -5,7 +5,7 @@ import superjson from "superjson";
import type { AppRouter } from "@karakeep/trpc/routers/_app";
-import { TRPCProvider } from "../trpc";
+import { TRPC_MAX_URL_LENGTH_EXTERNAL, TRPCProvider } from "../trpc";
interface Settings {
apiKey?: string;
@@ -15,16 +15,26 @@ interface Settings {
let browserQueryClient: QueryClient | undefined = undefined;
+function makeQueryClient() {
+ return new QueryClient({
+ defaultOptions: {
+ queries: {
+ staleTime: 60_000,
+ },
+ },
+ });
+}
+
function getQueryClient() {
if (typeof window === "undefined") {
// Server: always make a new query client
- return new QueryClient();
+ return makeQueryClient();
} else {
// Browser: make a new query client if we don't already have one
// This is very important, so we don't re-make a new client if React
// suspends during the initial render. This may not be needed if we
// have a suspense boundary BELOW the creation of the query client
- if (!browserQueryClient) browserQueryClient = new QueryClient();
+ if (!browserQueryClient) browserQueryClient = makeQueryClient();
return browserQueryClient;
}
}
@@ -34,7 +44,41 @@ function getTRPCClient(settings: Settings) {
links: [
httpBatchLink({
url: `${settings.address}/api/trpc`,
- maxURLLength: 14000,
+ maxURLLength: TRPC_MAX_URL_LENGTH_EXTERNAL,
+ fetch: (url, options) => {
+ const controller = new AbortController();
+ const timeout = setTimeout(() => controller.abort(), 30_000);
+
+ // Forward any existing abort signal from tRPC / React Query
+ const externalSignal = options?.signal as AbortSignal | undefined;
+ let onAbort: (() => void) | undefined;
+ if (externalSignal) {
+ if (externalSignal.aborted) {
+ controller.abort(externalSignal.reason);
+ } else {
+ onAbort = () => controller.abort(externalSignal.reason);
+ externalSignal.addEventListener("abort", onAbort);
+ }
+ }
+
+ return fetch(url, {
+ ...options,
+ signal: controller.signal,
+ }).then(
+ (response) => {
+ clearTimeout(timeout);
+ if (onAbort)
+ externalSignal!.removeEventListener("abort", onAbort);
+ return response;
+ },
+ (error) => {
+ clearTimeout(timeout);
+ if (onAbort)
+ externalSignal!.removeEventListener("abort", onAbort);
+ throw error;
+ },
+ );
+ },
headers() {
return {
Authorization: settings.apiKey
diff --git a/packages/shared-react/trpc.ts b/packages/shared-react/trpc.ts
index 2a36b257..1b332b19 100644
--- a/packages/shared-react/trpc.ts
+++ b/packages/shared-react/trpc.ts
@@ -5,3 +5,8 @@ import { createTRPCContext } from "@trpc/tanstack-react-query";
import type { AppRouter } from "@karakeep/trpc/routers/_app";
export const { TRPCProvider, useTRPC } = createTRPCContext<AppRouter>();
+
+export {
+ TRPC_MAX_URL_LENGTH_EXTERNAL,
+ TRPC_MAX_URL_LENGTH_INTERNAL,
+} from "@karakeep/shared/trpc";
diff --git a/packages/shared/trpc.ts b/packages/shared/trpc.ts
new file mode 100644
index 00000000..38a2fdc0
--- /dev/null
+++ b/packages/shared/trpc.ts
@@ -0,0 +1,25 @@
+/**
+ * Maximum URL length for tRPC httpBatchLink requests.
+ *
+ * The web app uses a higher limit to support bulk operations (e.g.
+ * drag-and-drop bookmark import from the browser) that produce large
+ * batched request URLs. The trade-off is that users behind a reverse
+ * proxy with strict default header limits may hit 431 errors on very
+ * large batches.
+ *
+ * The mobile app and browser extension use a lower limit because they
+ * don't need bulk operations and their requests always travel through
+ * the user's server, which typically sits behind a reverse proxy.
+ * nginx — the most common reverse proxy for self-hosted deployments —
+ * defaults `large_client_header_buffers` to 4 × 8 KB, meaning a single
+ * request line (including the URL) must fit within ~8 KB. 4,000
+ * characters stays safely under that limit after accounting for the
+ * method, HTTP version, and any URL-encoding overhead.
+ *
+ * Also see:
+ * * https://github.com/karakeep-app/karakeep/issues/281
+ * * https://github.com/karakeep-app/karakeep/issues/1619
+ * * https://nginx.org/en/docs/http/ngx_http_core_module.html#large_client_header_buffers
+ */
+export const TRPC_MAX_URL_LENGTH_INTERNAL = 14000;
+export const TRPC_MAX_URL_LENGTH_EXTERNAL = 4000;