Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions example/__tests__/fetch-api-compliance.harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,17 @@ describe('Fetch - cache option', () => {
expect(cc).toContain('no-store');
});

it('"no-store" applies to streaming requests', async () => {
const res = await nitroFetch(`${BASE}/headers`, {
cache: 'no-store',
stream: true,
} as any);
const body = await res.json();
const cc =
body.headers['Cache-Control'] || body.headers['cache-control'] || '';
expect(cc).toContain('no-store');
});

it('"no-cache" sends Cache-Control: no-cache header', async () => {
const res = await nitroFetch(`${BASE}/headers`, {
cache: 'no-cache',
Expand Down
25 changes: 16 additions & 9 deletions packages/react-native-nitro-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ function applyDefaultContentType(
headers.push({ key: 'Content-Type', value: contentType });
}

function applyCacheHeaders(
headers: NitroHeader[],
cache: RequestCache | undefined
): void {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we add. worklets

if (cache === 'no-store') {
headers.push({ key: 'Cache-Control', value: 'no-store' });
} else if (cache === 'no-cache') {
headers.push({ key: 'Cache-Control', value: 'no-cache' });
} else if (cache === 'reload') {
headers.push({ key: 'Cache-Control', value: 'no-cache' });
headers.push({ key: 'Pragma', value: 'no-cache' });
}
}

function headersToPairs(headers?: HeadersInit): NitroHeader[] | undefined {
'worklet';
if (!headers) return undefined;
Expand Down Expand Up @@ -231,15 +245,7 @@ function buildNitroRequest(
const normalized = normalizeBody(body);
applyDefaultContentType(headers, normalized?.contentType);

// Inject cache-control headers based on cache option
if (cacheOption === 'no-store') {
headers.push({ key: 'Cache-Control', value: 'no-store' });
} else if (cacheOption === 'no-cache') {
headers.push({ key: 'Cache-Control', value: 'no-cache' });
} else if (cacheOption === 'reload') {
headers.push({ key: 'Cache-Control', value: 'no-cache' });
headers.push({ key: 'Pragma', value: 'no-cache' });
}
applyCacheHeaders(headers, cacheOption);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe we can also wire buildNitroRequestPure


// Determine followRedirects based on redirect option
const followRedirects = redirectOption === 'follow';
Expand Down Expand Up @@ -773,6 +779,7 @@ async function nitroStreamFetch(
const headers = headersToPairs(init?.headers ?? src?.headers) ?? [];
const normalized = normalizeBody(init?.body);
applyDefaultContentType(headers, normalized?.contentType);
applyCacheHeaders(headers, init?.cache as RequestCache | undefined);

// Inspector: record start
let inspectorId: string | undefined;
Expand Down
Loading