forked from ampproject/amp.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement caching strategy (ampproject#1366)
- Loading branch information
1 parent
3a0356c
commit 2286587
Showing
6 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright 2018 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const mime = require('mime-types'); | ||
const ms = require('ms'); | ||
const {setMaxAge, setImmutable} = require('./cacheHelpers.js'); | ||
|
||
const IMMUTABLE = 'immutable'; | ||
|
||
const maxAgePerMimeType = [ | ||
[/text\/.+$/i, maxAge('1h')], | ||
[/image\/.+$/i, maxAge('1d')], | ||
[/video\/.+$/i, maxAge('1w')], | ||
[/application\/json$/i, maxAge('1h')], | ||
[/application\/javascript$/i, maxAge('1h')], | ||
[/font\/.+$/i, IMMUTABLE], | ||
]; | ||
|
||
const defaultMaxAge = maxAge('1m'); | ||
|
||
function defaultStrategy(request, response, next) { | ||
const mimeType = mime.lookup(request.path) || | ||
extractMimeFromAcceptHeader(request.headers.accept); | ||
if (!mimeType) { | ||
setMaxAge(response, defaultMaxAge); | ||
next(); | ||
return; | ||
} | ||
const maxAgeMapping = maxAgePerMimeType.find((mapping) => mapping[0].test(mimeType)); | ||
const maxAge = maxAgeMapping ? maxAgeMapping[1] : defaultMaxAge; | ||
if (maxAge === IMMUTABLE) { | ||
setImmutable(response); | ||
} else { | ||
setMaxAge(response, maxAge); | ||
} | ||
next(); | ||
} | ||
|
||
function extractMimeFromAcceptHeader(string) { | ||
if (!string) { | ||
return ''; | ||
} | ||
return string.split(',')[0]; | ||
} | ||
|
||
function maxAge(string) { | ||
return Math.floor(ms(string) / 1000); | ||
} | ||
|
||
module.exports = { | ||
defaultStrategy, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright 2018 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
function setNoCache(response) { | ||
response.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate'); | ||
} | ||
|
||
function setMaxAge(response, maxAge) { | ||
response.setHeader('Cache-Control', `public, max-age=${maxAge}`); | ||
} | ||
|
||
function setImmutable(response) { | ||
response.setHeader('Cache-Control', `max-age=365000000, immutable`); | ||
} | ||
|
||
function setNoSniff(response) { | ||
response.setHeader('x-content-type-options', 'nosniff'); | ||
} | ||
|
||
function setHsts(response) { | ||
response.setHeader('strict-transport-security', 'max-age=31536000; includeSubDomains; preload'); | ||
} | ||
|
||
function setXssProtection(response) { | ||
response.setHeader('x-xss-protection', '1; mode=block'); | ||
} | ||
|
||
function setAmpCSP(response) { | ||
response.setHeader('content-security-policy', `default-src * blob: data:; script-src blob: https://cdn.ampproject.org/esm/ https://cdn.ampproject.org/mp/ https://cdn.ampproject.org/rtv/ https://cdn.ampproject.org/sp/ https://cdn.ampproject.org/sw/ https://cdn.ampproject.org/v0.js https://cdn.ampproject.org/v0/ https://cdn.ampproject.org/viewer/; object-src 'none'; style-src 'unsafe-inline' https://cdn.ampproject.org/rtv/ https://cdn.materialdesignicons.com https://cloud.typography.com https://fast.fonts.net https://fonts.googleapis.com https://maxcdn.bootstrapcdn.com https://p.typekit.net https://pro.fontawesome.com https://use.fontawesome.com https://use.typekit.net; report-uri https://csp-collector.appspot.com/csp/amp`) | ||
} | ||
|
||
module.exports = { | ||
setNoCache, | ||
setMaxAge, | ||
setImmutable, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters