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

Update docs and examples for Next.js #2242

Open
1 task
splicebison opened this issue Apr 3, 2025 · 0 comments
Open
1 task

Update docs and examples for Next.js #2242

splicebison opened this issue Apr 3, 2025 · 0 comments
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library

Comments

@splicebison
Copy link

Description

Per the docs, since v15 Next has changed the default caching strategy on fetch requests. Now, fetch requests are uncached by default so cache: 'force-cache' must be added. This can be done during createClient instantiation.

Moreover, Nextjs is not including openapi-fetch requests in the patched lifecycle so regardless of setting the above, openapi-fetch requests will always be uncached in Next. This can be verified by enable fetch logging.

Due to the nature of the lifecycle fetch request patching in Next, one can simply not pass in global fetch during createClient instantiation as follows:

export const client = createClient<paths>({
  baseUrl: '',
  fetch,
})

This will not work as expected because the Nextjs fetch patching has not yet occurred at this point.

Proposal

I propose to update the docs and Nextjs example to show an alternative approach that is both clean and reusable and ensures that the patched fetch method and cache controls do not need to be passed with every API client request as this is error prone and verbose. Instead, a simple config like the following works as expected and enables caching by default. Moreover, I would recommend to note in the docs that individual requests can override the caching behavior on a per-request basis.

export const client = createClient<paths>({
  baseUrl: '',
  cache: 'force-cache', // Add cache by default
  // This is needed to inject Next's patched native fetch during the request lifecycle
  fetch: (input) => fetch(input),
})

and per request override:

const res = await client.GET('/users', {
  cache: 'no-cache',
})

Extra

@splicebison splicebison added enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library labels Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

1 participant