Limitations of Opaque Responses in the Fetch API
Opaque responses, defined in the Fetch API, stem from cross-origin requests made without CORS (Cross-Origin Resource Sharing) enabled, imposing certain limitations on their usage.
Access Restrictions:
Firstly,opaque responses cannot reveal meaningful details via Response properties such as headers or Body methods due to their black-box nature.
Use as Page Resources:
Opaque responses serve as valid resources for specific elements on a web page where non-CORS cross-origin resources are permissible. According to Mozilla Developer Network documentation, compatible elements include:
However, they are not appropriate for font resources. Consult relevant specifications to determine compatibility for each resource type.
Cache Storage API Interactions:
A potential pitfall in using opaque responses with the Cache Storage API is their status code always being set to 0, regardless of request outcome. Since the Cache Storage API rejects responses with non-2XX status codes, opaque responses cannot be added directly via add() or addAll().
A workaround is to explicitly fetch and use the put() method with the opaque response, acknowledging the risk that the cached response may represent an error from the server.
navigator.storage API Implications:
To prevent cross-domain information leakage, opaque responses incur significant padding in size when calculating storage quota limits. In browsers like Google Chrome, each cached opaque response contributes at least 7 megabytes towards the overall storage usage. This factor should be considered when managing the cache capacity for opaque responses.
The above is the detailed content of What are the Limitations and Considerations When Using Opaque Responses in the Fetch API?. For more information, please follow other related articles on the PHP Chinese website!