Skip to content

Commit d196ca7

Browse files
grabcodeFacebook Github Bot
authored and
Facebook Github Bot
committed
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 #10919 Differential Revision: D4226350 Pulled By: davidaurelio fbshipit-source-id: d4bbff5b1a5b691aab197bcddb8fa9d2e43caa16
1 parent 35e75c8 commit d196ca7

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

packager/react-packager/src/Server/__tests__/Server-test.js

-4
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ describe('processRequest', () => {
354354

355355
server.processRequest(req, res);
356356
jest.runAllTimers();
357-
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
358357
expect(res.end).toBeCalledWith('i am image');
359358
});
360359

@@ -367,7 +366,6 @@ describe('processRequest', () => {
367366
server.processRequest(req, res);
368367
jest.runAllTimers();
369368
expect(AssetServer.prototype.get).toBeCalledWith('imgs/a.png', 'ios');
370-
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
371369
expect(res.end).toBeCalledWith('i am image');
372370
});
373371

@@ -381,7 +379,6 @@ describe('processRequest', () => {
381379
server.processRequest(req, res);
382380
jest.runAllTimers();
383381
expect(AssetServer.prototype.get).toBeCalledWith('imgs/a.png', 'ios');
384-
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
385382
expect(res.end).toBeCalledWith(mockData.slice(0, 4));
386383
});
387384

@@ -397,7 +394,6 @@ describe('processRequest', () => {
397394
'imgs/\u{4E3B}\u{9875}/logo.png',
398395
undefined
399396
);
400-
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
401397
expect(res.end).toBeCalledWith('i am image');
402398
});
403399
});

packager/react-packager/src/Server/index.js

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

0 commit comments

Comments
 (0)