planetscale/database-js

Serverless driver affecting Next.js App Router page caching

charlesfrisbee opened this issue · 3 comments

Versions

Library Version
@planetscale/database 1.11.0
next 13.4.19

I am trying to get data from a planetscale db using the serverless driver in a Next.js app router page, and then have this cached indefinitely (SSG). Essentially the same as getStaticProps from the pages router:

// getShows.ts
export const getShows = async () => {
  const conn = getConnection();

  const results = await conn.execute(
    "Select ..."
  );

  const data = results.rows;

  return data;
};
// app/page.tsx
import { getShows } from "@/lib/api/getShows";

export default async function Home() {
  const shows = await getShows();
  return (
    <div>
      {shows.map((id, title) => {
        return <div key={id}>{title}</div>;
      })}
    </div>
  );
}

When the page is built (both locally and on vercel) it is not cached but instead renders at request time SSR. I also tried all the dynamic route segment config options to no avail.

When I put the getShows function in a route segment and call it from the page.tsx by fetch instead of directly with the serverless driver it works fine. Other 3rd party libraries such as octokit also seem to work fine too as SSG out of the box (hence why I am asking here and not on the next repo) in case I am missing something simple

See #102 for more context. Caching is explicitly disallowed. The driver cannot guarantee any safety around this.

You can do something by overriding the fetch() implementation and dropping the cache attribute to allow it.

So kinda like this: #102 (comment)

thanks for the clarification @mattrobenolt