Skip to content
Myr

Fix `searchParams` dropping request options inherited from a `Request` input

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When `searchParams` is set, the request is rebuilt with the new URL from the options alone, so `credentials`, `cache`, `redirect`, `integrity`, `referrer`, and `referrerPolicy` set on a `Request` input were reset to their defaults. In browsers this silently stopped sending cookies for `credentials: 'include'` requests. The rebuilt request now reads those options back from the request that was already constructed, so inherited values survive and explicit options still win.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent fa9f6a7 · Browse files · 2 files+70 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+70 added−1 removed

  • tests:+52 added−0 removed
  • other:+18 added−1 removed

2 files+70 added−1 removed

modified:source/core/Ky.ts+18 added-1 removed
492 unmodified lines
493 }494 }495496 // Recreate request with the updated URL. We already have all options in this.#options, including duplex.497 // Request options are read back from the current request so values inherited from a `Request` input (like `credentials`) survive, while explicit options already won when that request was built.498 // A `Request` input's body is only available as a stream, so keeping it requires request stream support.498 const {body: inheritedBody, keepalive, mode} = this.request;499 const {500 body: inheritedBody,501 cache,502 credentials,503 integrity,504 keepalive,505 mode,506 redirect,507 referrer,508 referrerPolicy,509 } = this.request;510 const canUseInheritedBody = supportsRequestStreams && !keepalive && mode !== 'no-cors';511 this.request = new globalThis.Request(url, {512 ...this.#options,513 cache,514 credentials,515 integrity,516 keepalive,517 mode,518 redirect,519 referrer,520 referrerPolicy,521 body: this.#options.body ?? (canUseInheritedBody ? inheritedBody : undefined),522 } as RequestInit);523 }524
729 unmodified lines
modified:test/main.ts+52 added
1636 unmodified lines
1637 },1638 });1639});16401641test('searchParams preserves request options inherited from a Request input', async t => {1642 const request = new Request('https://example.com', {1643 credentials: 'include',1644 cache: 'no-store',1645 redirect: 'manual',1646 integrity: 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=',1647 });16481649 await ky(request, {1650 searchParams: {foo: '1'},1651 async fetch(request) {1652 t.is(request.url, 'https://example.com/?foo=1');1653 t.is(request.credentials, 'include');1654 t.is(request.cache, 'no-store');1655 t.is(request.redirect, 'manual');1656 t.is(request.integrity, 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=');1657 return new Response();1658 },1659 });1660});16611662test('searchParams lets options override request options inherited from a Request input', async t => {1663 const request = new Request('https://example.com', {credentials: 'include', cache: 'no-store'});16641665 await ky(request, {1666 searchParams: {foo: '1'},1667 credentials: 'omit',1668 async fetch(request) {1669 t.is(request.url, 'https://example.com/?foo=1');1670 t.is(request.credentials, 'omit');1671 t.is(request.cache, 'no-store');1672 return new Response();1673 },1674 });1675});16761677test('searchParams keeps request options from a Request input across retries', async t => {1678 const request = new Request('https://example.com', {credentials: 'include', redirect: 'manual'});1679 const seen: Array<[RequestCredentials, RequestRedirect]> = [];16801681 await ky(request, {1682 searchParams: {foo: '1'},1683 retry: {limit: 1, delay: () => 0},1684 async fetch(request) {1685 seen.push([request.credentials, request.redirect]);1686 return new Response(null, {status: seen.length === 1 ? 500 : 200});1687 },1688 });16891690 t.deepEqual(seen, [['include', 'manual'], ['include', 'manual']]);1691});16921693test('init hook deletion over merged defaults and input URL', async t => {1694 const server = await createHttpTestServer(t);16951696 server.get('/', (request, response) => {
1783 unmodified lines