Caching assets only for production environment

Summary:
**Motivation**

In the context of a webview, one may extract assets (javascript or any types really), and relates to them through the html.

The packager `Server` serves this files correctly but also applies a cache based on time (a year). During development, this cache is actually bad as we need to re-iterate the process of editing/testing quickly.

I don't believe it is necessary to cache, and still wanted to make sure we would if process.env.NODE_ENV is 'production'.

**Test plan**

Run jest on impacted files:
```
node_modules/.bin/jest packager/react-packager/src/Server/__tests__/Server-test.js
```
Closes https://github.com/facebook/react-native/pull/10919

Differential Revision: D4226350

Pulled By: davidaurelio

fbshipit-source-id: d4bbff5b1a5b691aab197bcddb8fa9d2e43caa16
This commit is contained in:
alex 2016-11-23 04:44:07 -08:00 committed by Facebook Github Bot
parent 7d998e6f58
commit 719126bae2
2 changed files with 3 additions and 5 deletions

View File

@ -354,7 +354,6 @@ describe('processRequest', () => {
server.processRequest(req, res); server.processRequest(req, res);
jest.runAllTimers(); jest.runAllTimers();
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
expect(res.end).toBeCalledWith('i am image'); expect(res.end).toBeCalledWith('i am image');
}); });
@ -367,7 +366,6 @@ describe('processRequest', () => {
server.processRequest(req, res); server.processRequest(req, res);
jest.runAllTimers(); jest.runAllTimers();
expect(AssetServer.prototype.get).toBeCalledWith('imgs/a.png', 'ios'); expect(AssetServer.prototype.get).toBeCalledWith('imgs/a.png', 'ios');
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
expect(res.end).toBeCalledWith('i am image'); expect(res.end).toBeCalledWith('i am image');
}); });
@ -381,7 +379,6 @@ describe('processRequest', () => {
server.processRequest(req, res); server.processRequest(req, res);
jest.runAllTimers(); jest.runAllTimers();
expect(AssetServer.prototype.get).toBeCalledWith('imgs/a.png', 'ios'); expect(AssetServer.prototype.get).toBeCalledWith('imgs/a.png', 'ios');
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
expect(res.end).toBeCalledWith(mockData.slice(0, 4)); expect(res.end).toBeCalledWith(mockData.slice(0, 4));
}); });
@ -397,7 +394,6 @@ describe('processRequest', () => {
'imgs/\u{4E3B}\u{9875}/logo.png', 'imgs/\u{4E3B}\u{9875}/logo.png',
undefined undefined
); );
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
expect(res.end).toBeCalledWith('i am image'); expect(res.end).toBeCalledWith('i am image');
}); });
}); });

View File

@ -473,7 +473,9 @@ class Server {
data => { data => {
// Tell clients to cache this for 1 year. // Tell clients to cache this for 1 year.
// This is safe as the asset url contains a hash of the asset. // This is safe as the asset url contains a hash of the asset.
if (process.env.REACT_NATIVE_ENABLE_ASSET_CACHING === true) {
res.setHeader('Cache-Control', 'max-age=31536000'); res.setHeader('Cache-Control', 'max-age=31536000');
}
res.end(this._rangeRequestMiddleware(req, res, data, assetPath)); res.end(this._rangeRequestMiddleware(req, res, data, assetPath));
process.nextTick(() => { process.nextTick(() => {
print(log(createActionEndEntry(processingAssetRequestLogEntry)), ['asset']); print(log(createActionEndEntry(processingAssetRequestLogEntry)), ['asset']);