luo3house / react-native-reverse-proxy

Rnrp is an embedded reverse proxy.
MIT License
1 stars 0 forks source link

Feature Request: Request Interceptor #1

Open im-alihashmi14 opened 4 days ago

im-alihashmi14 commented 4 days ago

The feature request is to add an interceptor functionality to the react-native-reverse-proxy package, which would enable conditional request forwarding based on the availability of cached content. The goal is to check if a requested video already exists in cache, and if so, to serve it directly from the cache instead of fetching it from the original source.

Proposed Functionality:

  1. Request Interception with Cache Check: The interceptor should examine each request and determine if the requested video file exists in the cache.

  2. Conditional Forwarding: If the video is found in cache, serve it from there; otherwise, forward the request to the original source.

  3. Response Caching: Optionally, provide support for caching responses of specific requests to speed up subsequent accesses.

Use Case:

Optimized Video Playback: If the video exists in cache, serve it directly from the device, reducing bandwidth usage and load times. This is particularly useful for applications dealing with large video files or frequent video replays.

This functionality would improve app performance by leveraging local cache for repeated requests, enhancing user experience with faster load times and reduced data usage.

luo3house commented 1 day ago

Thanks for the proposal.

Because React Native natively provides caching functionality both on Android & iOS. What about serving with additive caching headers (e.g. "E-Tag") from the original server then the proxy will just forward them through. Popular video libraries (AVKit, ExoPlayer) should respect to them.

im-alihashmi14 commented 1 day ago

Thank you for the suggestion,

My requirement, however, involves intercepting the request at the reverse proxy level before it’s forwarded. I’d like to introduce a layer that checks if the requested data (e.g., video files) is already available locally. If the content is found in the device cache, the interceptor would serve it directly from there. If the content isn’t available locally, the request would proceed to the original network source.

This approach prioritizes checking local availability, allowing the app to benefit from faster load times and reduced data usage without waiting for network responses or relying solely on cache headers. Adding this functionality to react-native-reverse-proxy would greatly enhance performance, especially for apps where repeated video access is common.

luo3house commented 1 day ago

Do you expect there are possibly some files presented in local directories? I think you may feed the video component with local file URL ahead of remote one if local file exists, or feed the proxy URL otherwise.

Intercepting JS procedures within native codes needs a mechanism to handle asynchronous cases and significantly costs performance, so that most native packages prefer setting serializable parameters before running exact native codes.