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

Add retry.delay option to control the time between retries #533

Merged
merged 6 commits into from
Oct 17, 2023

Conversation

rclarey
Copy link
Contributor

@rclarey rclarey commented Sep 29, 2023

Related to option (2) to #509

Changes

  • add delay function to RetryOptions object
  • add test for using delay

@rclarey
Copy link
Contributor Author

rclarey commented Oct 6, 2023

@sindresorhus @szmarczak @sholladay
Sorry, can you please take a look when you have a chance

source/types/options.ts Outdated Show resolved Hide resolved
test/retry.ts Show resolved Hide resolved
@sindresorhus
Copy link
Owner

You need to document it in the readme too.

@sindresorhus sindresorhus merged commit c6181ef into sindresorhus:main Oct 17, 2023
1 check passed
@TommySorensen
Copy link

FYI: @sindresorhus
After the changes in this PR, then i'm seeing warnings in NextJS like this on all my endpoints.

 ⚠ fetch for https://myspeficisomeurl.com on /cart specified "cache: default" and "revalidate: 60", only one should be specified.

I'm using ky pretty much basic. But it seems like that KY internally is adding the cache to the fetcher with value of default.

const contentApi = ky.create({
  prefixUrl: `${process.env.CONTENT_API_URL}/api`,
  retry: 0,
});


contentApi
    .get(`getSomething/${someId}`, {
      next: { revalidate: 60 },
    })
    .json();

@rclarey
Copy link
Contributor Author

rclarey commented Dec 4, 2023

@TommySorensen this is because Ky internally constructs a Request, and the browser will automatically set request.cache to "default" if it's not specified.
Screenshot 2023-12-04 at 10 07 50
So this is an issue with Next's warning rather than an issue with Ky.

I just checked and it seems this was fixed in Next.js in vercel/next.js#58505, and released in https://github.com/vercel/next.js/releases/tag/v14.0.3

BTW if like me you're not using Next 14 yet, this is a workaround I've been using:

function fetchWith(override: RequestInit): typeof fetch {
  return (input, init) => {
    if (input instanceof Request) {
      return fetch(input.url, {
        // Copy all properties *except* for `cache` to prevent Next from warning that
        // we pass both `cache` and `revalidate`.
        // This happens because Ky internally constructs a `Request` object, so `cache`
        // will always be the set to `"default"` even when we don't explicitly set it.
        body: input.body,
        credentials: input.credentials,
        headers: input.headers,
        integrity: input.integrity,
        keepalive: input.keepalive,
        method: input.method,
        mode: input.mode,
        redirect: input.redirect,
        referrer: input.referrer,
        referrerPolicy: input.referrerPolicy,
        signal: input.signal,
        ...init,
        ...override,
      });
    }
    return fetch(input, { ...init, ...override });
  };
}

// then use it like
contentApi
  .get(`getSomething/${someId}`, {
    fetch: fetchWith({ next: { revalidate: 60 } }),
  })
  .json();

@sholladay sholladay mentioned this pull request Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants