Skip to content

Server Side Caching Error #579

@pbrissaud

Description

@pbrissaud

Describe the bug
Using @supabase-cache-helpers/postgrest-server in Next and react-query with either cache.swr(query) or cache.query(query) results in a runtime error, both:

  • On the server when used with prefetchQuery(...)
  • On the client when used with useQuery(...)

The error thrown is:

Error: Key is not a PostgrestBuilder
    at Generator.next (<anonymous>)
    at new Promise (<anonymous>)

To Reproduce

  1. Set up the cache as described in the documentation.
  2. Create a query using Supabase's and wrap it with cache.swr(...) or cache.query(...).
  3. Use this wrapped query in:
    • A server component with prefetchQuery(...) from @supabase-cache-helpers/postgrest-react-query
    • A client component with useQuery(...)
// lib/cache.ts
export const cache = new QueryCache(new DefaultStatefulContext(), {
  stores: [new MemoryStore({ persistentMap: new Map() })],
  fresh: 1000,
  stale: 2000,
});
// lib/queries/challenges.ts
function getChallenges(client: TypedSupabaseClient, opts: { searchQuery: string }) {
  let query = client
    .from("challenges")
    .select("*")
    .order("created_at", { ascending: false });

  return cache.swr(query); // or cache.query(query)
}

On the server:

// app/challenges/page.tsx
await prefetchQuery(
  queryClient,
  queries.challenges.list(supabase, { searchQuery: "" })
);

On the client:

// components/challenges/challenges-list.tsx
const { data, isLoading } = useQuery(
  queries.challenges.list(supabase, { searchQuery: debouncedSearchTerm })
);

Expected behavior
The result of cache.swr(...) or cache.query(...) should be compatible with both useQuery(...) and prefetchQuery(...) without throwing runtime errors.

Additional context

  • The same queries work perfectly fine without caching.
  • The error seems to indicate that the object returned is not a valid PostgrestBuilder or doesn't match the expected shape.
  • Happens even with simple .select("*") queries.
  • Using:
    • @supabase-cache-helpers/postgrest-server@0.0.10
    • @supabase-cache-helpers/postgrest-react-query@1.12.1
    • @tanstack/react-query@5
    • Next.js 15 (App Router)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions