Networking docs appear outdated: credentials: 'omit' works on RN 0.86
还没有人认领这个 Issue。
- 主要语言
- C++
- 星标
- 127k
- 派生
- 25.3k
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 4
描述
Summary
The Networking docs list credentials: 'omit' as currently not working with fetch:
https://reactnative.dev/docs/network#known-issues-with-fetch-and-cookie-based-authentication
On React Native 0.86, the built-in global fetch respects credentials: 'omit' in all cookie scenarios tested on both iOS and Android:
- existing cookies are not sent on direct requests;
- existing cookies are not sent after an automatically followed
302; Set-Cookiefrom a direct response is not persisted;Set-Cookiefrom a302response is neither sent to the redirected request nor persisted.
Equivalent credentials: 'include' controls all behave in the opposite way, confirming that the cookie store and test server are working.
The other documented limitation, redirect: 'manual', is outside the scope of this report.
Environment
react-nativepackage: 0.86.2- Native runtime reported by
Platform.constants.reactNativeVersion: 0.86.0 - Expo: 57.0.9, Expo Go
- iOS: 26.5 simulator
- Android: API 37 emulator
- Fetch implementation: React Native built-in global
fetchEXPO_PUBLIC_USE_RN_FETCH=1- confirmed
global fetch !== expo/fetch
Expo SDK 57 documents EXPO_PUBLIC_USE_RN_FETCH=1 as the switch that keeps React Native's built-in fetch as the global implementation:
https://docs.expo.dev/versions/v57.0.0/sdk/expo/#expofetch-api
Reproduction
The test server provides these endpoints:
/api/set-cookie— responds withSet-Cookie: session=abc123andtheme=dark;/api/cookies— returns the receivedCookieheader;/api/redirect-to-cookies—302to/api/cookies;/api/redirect-set-cookie— responds with both302andSet-Cookie, redirecting to/api/cookies;/api/clear-test-cookies— expires all cookies used by the test.
The relevant client checks are equivalent to:
// Existing cookies: direct requests and automatic 302 follow
await clearTestCookies();
await fetch(`${API}/api/set-cookie`, { credentials: 'include' });
assert((await readCookies('include')).cookieHeader ===
'session=abc123; theme=dark');
assert((await readCookies('omit')).cookieHeader === null);
assert((await fetchRedirect('include')).cookieHeader ===
'session=abc123; theme=dark');
assert((await fetchRedirect('omit')).cookieHeader === null);
// A direct Set-Cookie response must be ignored with omit
await clearTestCookies();
await fetch(`${API}/api/set-cookie?scenario=direct-omit-response`, {
credentials: 'omit',
});
assert((await readCookies('include')).cookieHeader === null);
// Control: the same operation with include persists the cookie
await clearTestCookies();
await fetch(`${API}/api/set-cookie?scenario=direct-include-response`, {
credentials: 'include',
});
assert((await readCookies('include')).cookieHeader ===
'direct_include_response=should_persist');
// Set-Cookie received on a 302 must also be ignored with omit
await clearTestCookies();
const redirectedOmit = await fetch(
`${API}/api/redirect-set-cookie?scenario=redirect-omit-response`,
{ credentials: 'omit' },
);
assert((await redirectedOmit.json()).cookieHeader === null);
assert((await readCookies('include')).cookieHeader === null);
// Control: include sends the new cookie on the redirected request and persists it
await clearTestCookies();
const redirectedInclude = await fetch(
`${API}/api/redirect-set-cookie?scenario=redirect-include-response`,
{ credentials: 'include' },
);
assert((await redirectedInclude.json()).cookieHeader ===
'redirect_include_response=should_persist');
assert((await readCookies('include')).cookieHeader ===
'redirect_include_response=should_persist');
Results
All 10/10 checks passed independently on each platform.
| Behavior | Expected include control |
Expected omit behavior |
iOS | Android |
|---|---|---|---|---|
| Existing cookies, direct request | sent | not sent | PASS | PASS |
Existing cookies, followed 302 |
sent | not sent | PASS | PASS |
Direct response Set-Cookie |
persisted | ignored | PASS | PASS |
302 response Set-Cookie on redirected request |
sent | not sent | PASS | PASS |
302 response Set-Cookie after the request |
persisted | ignored | PASS | PASS |
Implementation context
The current request path also appears to implement this behavior explicitly:
whatwg-fetchmapscredentials: 'omit'toxhr.withCredentials = false:
https://github.com/JakeChampion/fetch/blob/v3.6.20/fetch.js#L587-L591- Android replaces the request client's cookie jar with
CookieJar.NO_COOKIESwhenwithCredentialsis false:
https://github.com/facebook/react-native/blob/v0.86.0/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.kt#L367-L373 - iOS sets
HTTPShouldHandleCookiesfromwithCredentials:
https://github.com/facebook/react-native/blob/v0.86.0/packages/react-native/Libraries/Network/RCTNetworking.mm#L316-L324
The native withCredentials wiring landed in 2017 (iOS, Android); the current Known Issues text was added later in react-native-website#769 in 2019. This history alone does not establish which edge cases were broken in 2019, but the unconditional wording does not match the current tested behavior.
Related older reports describe omit failing rather than the documentation being stale: #12956 and #30885. Neither documents a current remaining failure mode.
Request
Please remove credentials: 'omit' from the list of unsupported options in the current / 0.86 Networking docs. If a specific remaining edge case is known, please narrow the warning to that platform, version, or scenario and link the corresponding report.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从报告中链接的 Networking 文档部分开始,尤其是其中关于 fetch 和基于 cookie 的身份验证的已知问题的内容。更新关于凭据:'omit' 的表述,使其符合已报告的 React Native 0.86 行为,同时将 redirect: 'manual' 的限制留在范围之外;移除或适当缩小过时的警告后,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- react-native
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 1/5
- 预计耗时
- 1-3 小时
- 活跃度
- 冷清
- 描述清晰度
- 描述清楚
- 新手友好度
- 78/100