Skip to content

Commit cd6aaa7

Browse files
committed
Cache prebuilt iOS binaries in ~/Library/Caches/ReactNative
Currently, Hermes, ReactNativeDependencies, and ReactNativeCore tarballs are cached only inside the Pods/ directory. This means every clean `pod install` (or deleting the Pods folder) triggers a full re-download from Maven, even if the same version was already downloaded before. This adds a shared cache layer at ~/Library/Caches/ReactNative/. On first download, tarballs are saved to the shared cache. On subsequent pod installs, if the local Pods cache is empty but the shared cache has the tarball, it is copied locally instead of re-downloaded. This benefits: - Clean installs after deleting Pods/ - Multiple projects using the same React Native version - CI environments with a persistent home directory Added descriptive log messages for each scenario (local hit, shared cache hit, cache miss + download) to aid debugging.
1 parent 5c49006 commit cd6aaa7

3 files changed

Lines changed: 79 additions & 15 deletions

File tree

packages/react-native/scripts/cocoapods/rncore.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,36 @@ def self.podspec_source_download_prebuilt_nightly_tarball(version)
173173
return {:http => url}
174174
end
175175

176+
def self.shared_cache_dir()
177+
return File.join(Dir.home, "Library", "Caches", "ReactNative")
178+
end
179+
176180
def self.download_rncore_tarball(react_native_path, tarball_url, version, configuration)
177-
destination_path = configuration == nil ?
178-
"#{artifacts_dir()}/reactnative-core-#{version}.tar.gz" :
179-
"#{artifacts_dir()}/reactnative-core-#{version}-#{configuration}.tar.gz"
181+
filename = configuration == nil ?
182+
"reactnative-core-#{version}.tar.gz" :
183+
"reactnative-core-#{version}-#{configuration}.tar.gz"
184+
destination_path = "#{artifacts_dir()}/#{filename}"
185+
186+
if File.exist?(destination_path)
187+
rncore_log("Tarball #{filename} already exists in Pods. Skipping download.")
188+
return destination_path
189+
end
180190

181-
unless File.exist?(destination_path)
191+
`mkdir -p "#{artifacts_dir()}"`
192+
193+
cached_path = File.join(shared_cache_dir(), filename)
194+
if File.exist?(cached_path)
195+
rncore_log("Cache hit: copying #{filename} from shared cache (#{shared_cache_dir()})")
196+
FileUtils.cp(cached_path, destination_path)
197+
else
198+
rncore_log("Cache miss: downloading #{filename} from #{tarball_url}")
182199
# Download to a temporary file first so we don't cache incomplete downloads.
183200
tmp_file = "#{artifacts_dir()}/reactnative-core.download"
184-
`mkdir -p "#{artifacts_dir()}" && curl "#{tarball_url}" -Lo "#{tmp_file}" && mv "#{tmp_file}" "#{destination_path}"`
201+
`curl "#{tarball_url}" -Lo "#{tmp_file}" && mv "#{tmp_file}" "#{destination_path}"`
202+
# Save to shared cache for future use
203+
`mkdir -p "#{shared_cache_dir()}"`
204+
FileUtils.cp(destination_path, cached_path)
205+
rncore_log("Saved #{filename} to shared cache (#{shared_cache_dir()})")
185206
end
186207

187208
return destination_path

packages/react-native/scripts/cocoapods/rndependencies.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,36 @@ def self.podspec_source_download_prebuilt_nightly_tarball(version)
208208
return {:http => url}
209209
end
210210

211+
def self.shared_cache_dir()
212+
return File.join(Dir.home, "Library", "Caches", "ReactNative")
213+
end
214+
211215
def self.download_rndeps_tarball(react_native_path, tarball_url, version, configuration)
212-
destination_path = configuration == nil ?
213-
"#{artifacts_dir()}/reactnative-dependencies-#{version}.tar.gz" :
214-
"#{artifacts_dir()}/reactnative-dependencies-#{version}-#{configuration}.tar.gz"
216+
filename = configuration == nil ?
217+
"reactnative-dependencies-#{version}.tar.gz" :
218+
"reactnative-dependencies-#{version}-#{configuration}.tar.gz"
219+
destination_path = "#{artifacts_dir()}/#{filename}"
220+
221+
if File.exist?(destination_path)
222+
rndeps_log("Tarball #{filename} already exists in Pods. Skipping download.")
223+
return destination_path
224+
end
215225

216-
unless File.exist?(destination_path)
226+
`mkdir -p "#{artifacts_dir()}"`
227+
228+
cached_path = File.join(shared_cache_dir(), filename)
229+
if File.exist?(cached_path)
230+
rndeps_log("Cache hit: copying #{filename} from shared cache (#{shared_cache_dir()})")
231+
FileUtils.cp(cached_path, destination_path)
232+
else
233+
rndeps_log("Cache miss: downloading #{filename} from #{tarball_url}")
217234
# Download to a temporary file first so we don't cache incomplete downloads.
218235
tmp_file = "#{artifacts_dir()}/reactnative-dependencies.download"
219-
`mkdir -p "#{artifacts_dir()}" && curl "#{tarball_url}" -Lo "#{tmp_file}" && mv "#{tmp_file}" "#{destination_path}"`
236+
`curl "#{tarball_url}" -Lo "#{tmp_file}" && mv "#{tmp_file}" "#{destination_path}"`
237+
# Save to shared cache for future use
238+
`mkdir -p "#{shared_cache_dir()}"`
239+
FileUtils.cp(destination_path, cached_path)
240+
rndeps_log("Saved #{filename} to shared cache (#{shared_cache_dir()})")
220241
end
221242

222243
return destination_path

packages/react-native/sdks/hermes-engine/hermes-utils.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,38 @@ def download_stable_hermes(react_native_path, version, configuration)
221221
download_hermes_tarball(react_native_path, tarball_url, version, configuration)
222222
end
223223

224+
def shared_cache_dir()
225+
return File.join(Dir.home, "Library", "Caches", "ReactNative")
226+
end
227+
224228
def download_hermes_tarball(react_native_path, tarball_url, version, configuration)
225-
destination_path = configuration == nil ?
226-
"#{artifacts_dir()}/hermes-ios-#{version}.tar.gz" :
227-
"#{artifacts_dir()}/hermes-ios-#{version}-#{configuration}.tar.gz"
229+
filename = configuration == nil ?
230+
"hermes-ios-#{version}.tar.gz" :
231+
"hermes-ios-#{version}-#{configuration}.tar.gz"
232+
destination_path = "#{artifacts_dir()}/#{filename}"
233+
234+
if File.exist?(destination_path)
235+
hermes_log("Tarball #{filename} already exists in Pods. Skipping download.", :info)
236+
return destination_path
237+
end
228238

229-
unless File.exist?(destination_path)
239+
`mkdir -p "#{artifacts_dir()}"`
240+
241+
cached_path = File.join(shared_cache_dir(), filename)
242+
if File.exist?(cached_path)
243+
hermes_log("Cache hit: copying #{filename} from shared cache (#{shared_cache_dir()})", :info)
244+
FileUtils.cp(cached_path, destination_path)
245+
else
246+
hermes_log("Cache miss: downloading #{filename} from #{tarball_url}", :info)
230247
# Download to a temporary file first so we don't cache incomplete downloads.
231248
tmp_file = "#{artifacts_dir()}/hermes-ios.download"
232-
`mkdir -p "#{artifacts_dir()}" && curl "#{tarball_url}" -Lo "#{tmp_file}" && mv "#{tmp_file}" "#{destination_path}"`
249+
`curl "#{tarball_url}" -Lo "#{tmp_file}" && mv "#{tmp_file}" "#{destination_path}"`
250+
# Save to shared cache for future use
251+
`mkdir -p "#{shared_cache_dir()}"`
252+
FileUtils.cp(destination_path, cached_path)
253+
hermes_log("Saved #{filename} to shared cache (#{shared_cache_dir()})", :info)
233254
end
255+
234256
return destination_path
235257
end
236258

0 commit comments

Comments
 (0)