Skip to content

Commit

Permalink
Merge branch 'main' into pulse-ga
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur-arch authored Mar 6, 2024
2 parents 7b9c87f + 98b7634 commit e5325ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: 'Best practices for using Prisma Client with Next.js'
metaTitle: 'Best practices for using Prisma Client with Next.js'
metaDescription: 'Best practices for using Prisma Client with Next.js'
title: 'Best practice for instantiating Prisma Client with Next.js'
metaTitle: 'Best practice for instantiating Prisma Client with Next.js'
metaDescription: 'Best practice for instantiating Prisma Client with Next.js'
---

## Problem
Expand Down Expand Up @@ -71,20 +71,3 @@ export const getServerSideProps = async () => {
return { props: { posts } }
}
```

## Next.js caching

When using Next.js, you may run into an issue where [only stale data is returned from certain routes](https://github.com/prisma/prisma/discussions/22934#discussioncomment-8374081). In these cases, it's important to [set the dynamic behavior of the route](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic) to `"force-dynamic"` so data is correctly returned from your Prisma Client queries.

You can add `"force-dynamic"` like this:

```ts
import { NextRequest, NextResponse } from 'next/server'
import prisma from './db'

export const dynamic = 'force-dynamic'

export async function GET(request: NextRequest, response: NextResponse) {
// your code would go here
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: 'Best practices for using Prisma Client with Next.js and Next.js Data Cache'
metaTitle: 'Best practices for using Prisma Client with Next.js and Next.js Data Cache'
metaDescription: 'Learn best practices to avoid issues with route caching in Next.js'
---

## Problem

When deploying a Next.js app, you may run into an issue where your queries are not updating or displaying the correct content. Or, you may find that it takes a long time for newly created objects to show up in your queries.

In these cases, you are most likely seeing data persisted in the [Next.js Data Cache](https://nextjs.org/docs/app/building-your-application/caching#data-cache). Any `fetch` request has its result cached by default, leading to possibly unwanted results as Prisma uses `fetch` internally.

## Solution

To opt-out of the Next.js Data Cache, you can [disable caching for a specific route](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic) using the `"force-dynamic"` config option so that up to date data is always returned and no caching happens.

You can add `"force-dynamic"` like this:

```ts
import { NextRequest, NextResponse } from 'next/server'
import prisma from './db'

export const dynamic = 'force-dynamic'

export async function GET(request: NextRequest, response: NextResponse) {
// your code would go here
}
```
2 changes: 1 addition & 1 deletion content/400-pulse/200-getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ toc: true
To use Prisma Pulse, you need to meet the following prerequisites:

- A GitHub account.
- Prisma Pulse requires [Prisma Client](/orm/prisma-client) version `4.16.1` or higher and [`@prisma/extension-pulse`](https://www.npmjs.com/package/@prisma/extension-pulse) version `v0.2.2` or higher.
- Pulse requires [Prisma Client](/orm/prisma-client) version `4.16.1` or higher and [`@prisma/extension-pulse`](https://www.npmjs.com/package/@prisma/extension-pulse) version `v0.2.3` or higher.
- A publicly accessible PostgreSQL database.
- For the [**Starter Plan**](https://www.prisma.io/pricing): Ability to use the superuser account of the database instance.

Expand Down

0 comments on commit e5325ec

Please sign in to comment.