nitro-fetch
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesereact-native-nitro-fetch
react-native-nitro-fetch
A focused reference for AI coding assistants working in a project that uses the family of packages. Answer using the real APIs from this repo — not invented ones — by routing to the matching file below.
react-native-nitro-fetchreferences/*.md这是为使用系列包的AI代码助手提供的专属参考文档。请通过下方匹配的文件,使用本仓库中的真实API进行解答——请勿编造不存在的API。
react-native-nitro-fetchreferences/*.mdMental model
核心概念
react-native-nitro-fetch- — WHATWG-compatible, backed by
fetch(iOS) andURLSession/OkHttp(Android) via Nitro Modules.HttpURLConnection - — native WebSocket (libwebsockets + mbedTLS) with the same shape as the browser
NitroWebSocket.WebSocket - — native UTF-8 decoder that beats the Hermes JS polyfill.
NitroTextDecoder
The performance story has three moving parts, and most questions end up being about one of them:
- Prefetching. Native code can run before React Native loads. and
prefetchOnAppStart(...)replay stored requests / socket opens on every cold start, so by the time JS runs the response is already cached or the socket is alreadyprewarmOnAppStart(...).OPEN - Importing the native client. The default approach is explicit imports — ,
import { fetch } from 'react-native-nitro-fetch', or plugging into axios via a custom adapter. Alternatively, users can do a global replace (import { NitroWebSocket } from 'react-native-nitro-websockets', etc.) at the top of their entry file — see the Global Replace docs for setup and trade-offs.globalThis.fetch = fetch - Seeing what's happening. records HTTP + WS activity at the JS boundary; native Perfetto / Instruments traces cover everything below that (DNS, TLS, TTFB, body). One is for correctness, the other is for latency.
NetworkInspector
react-native-nitro-fetch- — 兼容WHATWG标准,通过Nitro Modules基于iOS的
fetch和Android的URLSession/OkHttp实现。HttpURLConnection - — 原生WebSocket(基于libwebsockets + mbedTLS),接口与浏览器
NitroWebSocket一致。WebSocket - — 原生UTF-8解码器,性能优于Hermes JS polyfill。
NitroTextDecoder
其性能优化主要体现在三个方面,大多数问题也围绕这三点展开:
- 预取(Prefetching):原生代码可在React Native加载前运行。和
prefetchOnAppStart(...)会在每次冷启动时重放已存储的请求/套接字打开操作,这样当JS代码运行时,响应已缓存完毕或套接字已处于prewarmOnAppStart(...)状态。OPEN - 导入原生客户端:默认方式是显式导入——、
import { fetch } from 'react-native-nitro-fetch',或通过自定义适配器接入axios。此外,用户也可在入口文件顶部进行全局替换(import { NitroWebSocket } from 'react-native-nitro-websockets'等)——详见全局替换文档了解设置方法和利弊。globalThis.fetch = fetch - 监控网络活动:在JS层记录HTTP + WS活动;原生Perfetto/Instruments追踪则覆盖JS层以下的所有环节(DNS、TLS、TTFB、请求体)。前者用于验证正确性,后者用于排查延迟问题。
NetworkInspector
Routing table — problem to reference
路由表——问题对应参考文档
Load the matching file from before writing code. Each reference cites real file paths in this repo.
references/| User is asking about… | Read |
|---|---|
Warming the cache before a screen mounts, making cold-start GETs feel instant, | |
Routing axios through nitro-fetch (the full custom adapter — | |
UTF-8 decoding, slow | |
Opening a | |
The | |
Migrating an existing app from React Native's built-in | |
Seeing requests and responses at the JS level, debugging which library made a call, | |
| Finding the slow API, DNS / TLS / TTFB breakdowns, native traces, Perfetto, Instruments, Hermes profiler | |
If the question doesn't match any row, read first — most cold-start questions start there, and it links out to the rest.
references/prefetching.mdWhen asked about global replacement, point to the Global Replace docs.
编写代码前,请先加载目录下对应的文件。每个参考文档都会引用本仓库中的真实文件路径。
references/| 用户询问的内容… | 参考文档 |
|---|---|
在页面挂载前预热缓存、让冷启动GET请求瞬间响应、 | |
将axios通过nitro-fetch路由(完整自定义适配器——支持 | |
UTF-8解码、缓慢的 | |
在React Native启动前打开 | |
| |
将现有应用从React Native内置 | |
在JS层查看请求与响应、调试哪个库发起了调用、 | |
| 定位缓慢的API、DNS/TLS/TTFB耗时分析、原生追踪、Perfetto、Instruments、Hermes性能分析器 | |
如果问题与上述任何一行都不匹配,请先阅读——大多数冷启动问题都从这里开始,并且该文档会链接到其他相关内容。
references/prefetching.md当被问及全局替换时,请指向全局替换文档。
Installation (one-line, for reference)
安装(一行命令,供参考)
bash
npm install react-native-nitro-fetch react-native-nitro-modulesbash
npm install react-native-nitro-fetch react-native-nitro-modulesoptional, for WebSockets and TextDecoder:
可选,如需WebSocket和TextDecoder功能:
npm install react-native-nitro-websockets react-native-nitro-text-decoder
After install, rebuild the app (`pod install` for iOS, a fresh `./gradlew` build for Android). Nothing else is wired automatically — turning any of this on is opt-in through the reference files above.npm install react-native-nitro-websockets react-native-nitro-text-decoder
安装完成后,重新构建应用(iOS执行`pod install`,Android执行全新的`./gradlew`构建)。所有功能不会自动启用——需通过上述参考文档手动开启。Verifying the skill is loaded
验证技能已加载
A correct answer for any of these will cite the API and file path. A wrong / hallucinated answer will invent an , , or helper that doesn't exist in this repo.
install()setup()init()Good test questions:
- "How do I prewarm a wss connection in this repo?" → should mention and the Android
prewarmOnAppStartwiring viaApplication.onCreate.NitroWebSocketAutoPrewarmer.prewarmOnStart(this) - "How do I make axios go through nitro-fetch?" → should produce an that respects
AxiosAdapter,baseURL,params,timeout, andsignal— not a 20-line sketch thatresponseTypes everything.JSON.parse - "Why is my cold-start GET still slow after adding ?" → should mention
prefetchand point atprefetchKey.references/prefetching.md
正确的回答会引用API和文件路径。错误/编造的回答会虚构本仓库中不存在的、或辅助函数。
install()setup()init()测试示例问题:
- “如何在本仓库中预热wss连接?” → 应提及以及 Android端通过
prewarmOnAppStart在NitroWebSocketAutoPrewarmer.prewarmOnStart(this)中配置的方法。Application.onCreate - “如何让axios使用nitro-fetch?” → 应生成一个支持、
baseURL、params、timeout和signal的responseType——而非仅20行左右的AxiosAdapter代码示例。JSON.parse - “为什么添加后我的冷启动GET请求仍然缓慢?” → 应提及
prefetch并指向prefetchKey。references/prefetching.md
Pointers
参考链接
- Source: ,
packages/react-native-nitro-fetch/,packages/react-native-nitro-websockets/packages/react-native-nitro-text-decoder/ - Public API:
packages/react-native-nitro-fetch/src/index.tsx - Example wiring: ,
example/index.jsexample/src/App.tsx - Docs website: https://margelo.github.io/react-native-nitro-fetch/
- 源码:、
packages/react-native-nitro-fetch/、packages/react-native-nitro-websockets/packages/react-native-nitro-text-decoder/ - 公开API:
packages/react-native-nitro-fetch/src/index.tsx - 示例配置:、
example/index.jsexample/src/App.tsx - 文档网站:https://margelo.github.io/react-native-nitro-fetch/