From d196ca70db5d05a40faf292dc19bebd31620b86e Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 23 Nov 2016 04:44:07 -0800 Subject: [PATCH] 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 --- packager/react-packager/src/Server/__tests__/Server-test.js | 4 ---- packager/react-packager/src/Server/index.js | 4 +++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packager/react-packager/src/Server/__tests__/Server-test.js b/packager/react-packager/src/Server/__tests__/Server-test.js index 44f0cddf2..660bac887 100644 --- a/packager/react-packager/src/Server/__tests__/Server-test.js +++ b/packager/react-packager/src/Server/__tests__/Server-test.js @@ -354,7 +354,6 @@ describe('processRequest', () => { server.processRequest(req, res); jest.runAllTimers(); - expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000'); expect(res.end).toBeCalledWith('i am image'); }); @@ -367,7 +366,6 @@ describe('processRequest', () => { server.processRequest(req, res); jest.runAllTimers(); 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'); }); @@ -381,7 +379,6 @@ describe('processRequest', () => { server.processRequest(req, res); jest.runAllTimers(); 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)); }); @@ -397,7 +394,6 @@ describe('processRequest', () => { 'imgs/\u{4E3B}\u{9875}/logo.png', undefined ); - expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000'); expect(res.end).toBeCalledWith('i am image'); }); }); diff --git a/packager/react-packager/src/Server/index.js b/packager/react-packager/src/Server/index.js index a704ae8be..39d4a7504 100644 --- a/packager/react-packager/src/Server/index.js +++ b/packager/react-packager/src/Server/index.js @@ -473,7 +473,9 @@ class Server { data => { // Tell clients to cache this for 1 year. // This is safe as the asset url contains a hash of the asset. - res.setHeader('Cache-Control', 'max-age=31536000'); + if (process.env.REACT_NATIVE_ENABLE_ASSET_CACHING === true) { + res.setHeader('Cache-Control', 'max-age=31536000'); + } res.end(this._rangeRequestMiddleware(req, res, data, assetPath)); process.nextTick(() => { print(log(createActionEndEntry(processingAssetRequestLogEntry)), ['asset']);