Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support fetch for Next.js app-router #249

Merged
merged 2 commits into from
Jul 26, 2023
Merged

feat: support fetch for Next.js app-router #249

merged 2 commits into from
Jul 26, 2023

Conversation

stipsan
Copy link
Member

@stipsan stipsan commented Jun 28, 2023

Testable using:

npm i @sanity/client@fetch --save-exact

It allows opt-in to using native fetch in the nodejs runtime in Next.js:

import {createClient} from '@sanity/client'

const client = createClient({
  ...config,
  // use `fetch`, opts-in to automatic fetch deduping and other benefits in app-router
  fetch: true,
})

And in both nodejs, and now edge, you can set certain useful options like cache and next on the request level:

const data = await client.fetch(query, params, {
  cache: 'force-cache',
  next: {revalidate: 30, tags: ['page']}
})

This allows us to get rid of workarounds such as: https://github.com/sanity-io/next-sanity#app-router-react-server-components-and-caching
And the ability to use features that are only available to data fetching that use fetch
under the hood.

@stipsan stipsan requested a review from rexxars June 28, 2023 02:16
Copy link

@gksander gksander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Tried this on my end in a production app running Next.js 13 (using fetch cache tags etc etc), and it appears to Just Work ™️

@stipsan stipsan force-pushed the fetch branch 2 times, most recently from 9b1f316 to e1e7dbb Compare June 30, 2023 11:45
@indeox
Copy link

indeox commented Jul 4, 2023

Tested this as well, and works beautifully. Very much looking forward to when it gets merged! 🎉 It will solve so many issues

Signed-off-by: Cody Olsen <81981+stipsan@users.noreply.github.com>
@stipsan stipsan merged commit 0aa4c6d into main Jul 26, 2023
12 checks passed
@stipsan stipsan deleted the fetch branch July 26, 2023 01:19
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants