Using React Query with Next.js App Router and Supabase Cache Helpers
React Query can be paired with supabase-js in a Next.js App Router app by wrapping the root layout in a client-side QueryClientProvider and creating typed browser and server Supabase clients via @supabase/ssr. Supabase Cache Helpers generate query keys automatically from supabase-js queries, so one reusable query function can be prefetched in a server component and hydrated through HydrationBoundary, letting the client render without a loading state, or fetched purely client-side. The recommendation is to reach for React Query with Server Components only when migrating an existing React Query app or when it covers needs the framework doesn't; otherwise start with the framework's own data fetching.
Related posts
- Fetching and caching Supabase data in Next.js 13 Server Components
Supabase mediumAdeptNext.js 13's app directory lets async Server Components query Supabase directly, with caching controlled by an exported `revalidate` value instead of separate getStaticProps/getServerSideProps functions. The walkthrough covers four strategies: static generation with `generateStaticParams`, incremental revalidation (e.g. `revalidate = 60`), fully dynamic fetching (`revalidate = 0`), and client components using `use client`. It closes with a hybrid pattern that loads initial data in a Server Component and passes it to a Client Component subscribed to Supabase Realtime Postgres changes, which separates data fetching from caching policy.
- Supabase is now compatible with Next.js 14
Supabase mediumAdeptSupabase works across every Next.js 14 rendering context, including App Router Client and Server Components, Route Handlers, Server Actions and Middleware, plus the Pages Router data-fetching functions. Now that Server Actions are stable, auth and mutations can run entirely server-side next to the Server Components that fetch data. supabase-js stores sessions in localStorage by default, which doesn't exist on the server, so it has to be configured with cookie-based storage on both server and browser. The new @supabase/ssr package and a create-next-app starter template (`-e with-supabase`) wrap that boilerplate.
- Introducing @supabase/server
Supabase mediumAdept@supabase/server, now in public beta, replaces the repeated setup code found across 25,000 analyzed Edge Functions: client creation, JWT verification, claims parsing, CORS and auth wiring. A withSupabase wrapper (or createSupabaseContext for custom error handling) takes a declarative auth mode such as 'user', 'none', 'secret', 'publishable' or a combination. It rejects unauthorized requests before the handler runs and supplies a context with a user-scoped RLS client, a service-role admin client and verified claims. It handles the new asymmetric JWT signing keys and API keys internally, so migrating needs no jose or JWKS setup. It returns a standard Request-to-Response handler that runs on Edge Functions, Vercel, Cloudflare Workers, Bun and Hono, and exposes lower-level primitives for per-route auth.
- Infinite scroll with Next.js, Framer Motion, and Supabase
Supabase mediumAdeptInfinite scroll can be built in Next.js by server-rendering the first 20 rows via getServerSideProps, then loading further pages from Supabase with offset-based .range() queries as the user nears the bottom of the list. A lodash-debounced window scroll listener (200 ms) compares the container's getBoundingClientRect bottom against innerHeight to trigger fetches, and the listener stops once a page returns fewer than PAGE_COUNT rows. Framer Motion wraps each item to fade and slide it in, with a staggered delay recalculated so newly appended batches animate independently of earlier ones.
- Using Supabase in Replit
Supabase mediumAdeptSupabase can serve as a hosted Postgres database for Node.js repls on Replit, queried directly through supabase-js. The setup is short: create a Node.js repl, require createClient, plug in the project URL and anon key from Settings > API, and load the Countries sample dataset from the SQL tab. Example queries use both .then() and async/await syntax, along with filter methods such as or, is, in and neq, and practice challenges for learning the query interface.