mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: This PR follows up on https://github.com/facebook/react-native/issues/47182 and https://github.com/facebook/react-native/issues/47348 by adding `force-cache`, the final missing option to align caching controls with the existing behavior on iOS. Local caching behavior remains unchanged: if a cached image is available locally, it will be returned; otherwise, a network request will be made. When an image request is sent over the network, the `force-cache` option sent from the sent fJS side will now use the `okhttp3.CacheControl.FORCE_CACHE` directive. ## Changelog: [ANDROID] [ADDED] - Image `force-cache` caching control option Pull Request resolved: https://github.com/facebook/react-native/pull/47426 Test Plan: New example added to the RNTester under the cache policy examples. Then inspecting that the cache control is set correctly before sending it in the `okhttp3.Request` builder. ```kt FLog.w("ReactNative", "fetching uri: %s, with cacheControl: %s", uri, cacheControlBuilder.build().toString()) // fetching uri: https:...png?cacheBust=force-cache, with cacheControl: no-store, max-stale=2147483647, only-if-cached ``` This case was a bit more tricky to test in terms of e2e as it would involve some caching in the server as well, I'm open to suggestions to make this more complete. Reviewed By: javache Differential Revision: D65490360 Pulled By: Abbondanzo fbshipit-source-id: f807a9793f85caea39c59a370d057b9a1d450a78
75 lines
2.8 KiB
TypeScript
75 lines
2.8 KiB
TypeScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
/*
|
|
* @see https://github.com/facebook/react-native/blob/master/Libraries/Image/ImageSource.js
|
|
*/
|
|
export interface ImageURISource {
|
|
/**
|
|
* `uri` is a string representing the resource identifier for the image, which
|
|
* could be an http address, a local file path, or the name of a static image
|
|
* resource (which should be wrapped in the `require('./path/to/image.png')`
|
|
* function).
|
|
*/
|
|
uri?: string | undefined;
|
|
/**
|
|
* `bundle` is the iOS asset bundle which the image is included in. This
|
|
* will default to [NSBundle mainBundle] if not set.
|
|
* @platform ios
|
|
*/
|
|
bundle?: string | undefined;
|
|
/**
|
|
* `method` is the HTTP Method to use. Defaults to GET if not specified.
|
|
*/
|
|
method?: string | undefined;
|
|
/**
|
|
* `headers` is an object representing the HTTP headers to send along with the
|
|
* request for a remote image.
|
|
*/
|
|
headers?: {[key: string]: string} | undefined;
|
|
/**
|
|
* `cache` determines how the requests handles potentially cached
|
|
* responses.
|
|
*
|
|
* - `default`: Use the native platforms default strategy. `useProtocolCachePolicy` on iOS.
|
|
*
|
|
* - `reload`: The data for the URL will be loaded from the originating source.
|
|
* No existing cache data should be used to satisfy a URL load request.
|
|
*
|
|
* - `force-cache`: The existing cached data will be used to satisfy the request,
|
|
* regardless of its age or expiration date. If there is no existing data in the cache
|
|
* corresponding the request, the data is loaded from the originating source.
|
|
*
|
|
* - `only-if-cached`: The existing cache data will be used to satisfy a request, regardless of
|
|
* its age or expiration date. If there is no existing data in the cache corresponding
|
|
* to a URL load request, no attempt is made to load the data from the originating source,
|
|
* and the load is considered to have failed.
|
|
*/
|
|
cache?: 'default' | 'reload' | 'force-cache' | 'only-if-cached' | undefined;
|
|
/**
|
|
* `body` is the HTTP body to send with the request. This must be a valid
|
|
* UTF-8 string, and will be sent exactly as specified, with no
|
|
* additional encoding (e.g. URL-escaping or base64) applied.
|
|
*/
|
|
body?: string | undefined;
|
|
/**
|
|
* `width` and `height` can be specified if known at build time, in which case
|
|
* these will be used to set the default `<Image/>` component dimensions.
|
|
*/
|
|
width?: number | undefined;
|
|
height?: number | undefined;
|
|
/**
|
|
* `scale` is used to indicate the scale factor of the image. Defaults to 1.0 if
|
|
* unspecified, meaning that one image pixel equates to one display point / DIP.
|
|
*/
|
|
scale?: number | undefined;
|
|
}
|
|
|
|
export type ImageRequireSource = number;
|