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

Custom fetch options are stripped from the request. Next.js cache doesn't work properly. #1569

Closed
petercsaki opened this issue Feb 27, 2024 · 8 comments
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue! stale

Comments

@petercsaki
Copy link

Description

Openapi-fetch uses a request object for its fetch. https://github.com/drwpow/openapi-typescript/blob/9c277fb0a10c3513de46765a4381ccb722a72af4/packages/openapi-fetch/src/index.js#L101
The request object created by openapi-fetch strips away non standard fetch options, i.e. how Next.js passes its cache related params:

{ next: { revalidate: 3600 } }

More info on Next.js fetching data + caching

Reproduction

Try to pass next cache params in https://github.com/drwpow/openapi-typescript/blob/main/packages/openapi-fetch/examples/nextjs/app/page.tsx
It won't change its behaviour, it always falls back to defaults, because the non standard ones are stripped away.

I've tried

I changed this line
https://github.com/drwpow/openapi-typescript/blob/9c277fb0a10c3513de46765a4381ccb722a72af4/packages/openapi-fetch/src/index.js#L101

to

let response = await fetch(request, requestInit);

and it works, but I don't know if it would cause other problems.

@petercsaki petercsaki added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Feb 27, 2024
@drwpow drwpow added PRs welcome PRs are welcome to solve this issue! good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project labels Feb 27, 2024
@drwpow
Copy link
Contributor

drwpow commented Feb 27, 2024

Ah, hm. I was somewhat afraid this would happen, and did see it come up in one other test failure, but had thought it was fixed.

Would gladly welcome any PRs for this! This should be a fairly-straightforward issue to write a failing test for, and find a fix.

@petercsaki
Copy link
Author

PR created.

@JE-lee
Copy link
Contributor

JE-lee commented Mar 7, 2024

For the workaround, this could be ok as well as the cache mechanism, regardless a little wired.

  const res = await client.GET("/api/v1/space", {
    fetch: (request: unknown) => {
      return fetch(request as Request, { next: { revalidate: 40 } } )
    }
  })

I have tested it in Next.js 14

@FreeAoi
Copy link
Contributor

FreeAoi commented May 9, 2024

Hi, a pull request fixing this was merged #1653 recently

@sazzouz
Copy link

sazzouz commented Jun 5, 2024

Hi, i'm still facing this issue on some Next.js 14 routes after using the latest openapi-fetch release, especially when using with Next-Auth v5 which might be meddling with the edge runtime, where some features are limited. Either way, the issue i am facing in particular is when mutating (POST, PATCH) on my server, the body seems to be stripped, but GET is working fine. This #1569 (comment) is the only way the i've been able to fix it and have data received by the server, thanks @JE-lee ! Note: each of these mutation API calls are currently working fine with pure fetch, i'm in the process of migrating to this package, currently running into this obscure issue.

I'm just looking for clues here and I assume the problem is linked to this issue, whereby something Next-related isn't being or can't be passed through. Any help or guidance of what to investigate further would be much appreciated.

In the interim, is there a way to apply the custom fetch argument in a DRY manner, similar to how we can apply middleware? In the event that i have to add this to all my mutations

@EricCline
Copy link

My $0.02 having been bit by and consequently moved away from Next.js. This is a problem with Next.js' API design rather than openapi-ts. Overloading and modifying the native fetch API is an utterly insane way to implement core features of a framework. My suggestion would be to file this issue with Next.js.

Copy link
Contributor

This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.

@github-actions github-actions bot added the stale label Oct 11, 2024
Copy link
Contributor

This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-fetch Relevant to the openapi-fetch library PRs welcome PRs are welcome to solve this issue! stale
Projects
None yet
Development

No branches or pull requests

6 participants