Skip to content

Commit

Permalink
Cache key for mobile version
Browse files Browse the repository at this point in the history
  • Loading branch information
kodermax committed Apr 22, 2019
1 parent 23b5186 commit 21af4bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"bracketSpacing": true,
"jsxBracketSameLine": false,
"printWidth": 100,
"semi": false,
"singleQuote": true
}
31 changes: 19 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ const getEtag = require('etag')
const { URL } = require('url')
const Keyv = require('keyv')

const getKey = url => {
const getKey = (url, isMobile) => {
const { origin } = new URL(url)
const baseKey = normalizeUrl(url, {
removeQueryParameters: [/^utm_\w+/i, 'force', 'filter', 'ref']
})
return baseKey.replace(origin, '').replace('/?', '')
return `isMoblie=${isMobile}&` + baseKey.replace(origin, '').replace('/?', '')
}

const toSeconds = ms => Math.floor(ms / 1000)
Expand All @@ -31,7 +31,7 @@ const createSetHeaders = ({ revalidate }) => {
'Cache-Control',
`public, must-revalidate, max-age=${maxAge}, s-maxage=${maxAge}, stale-while-revalidate=${
hasForce ? 0 : toSeconds(revalidate(ttl))
}`
}`
)

res.setHeader('X-Cache-Status', isHit ? 'HIT' : 'MISS')
Expand All @@ -41,14 +41,14 @@ const createSetHeaders = ({ revalidate }) => {
}

module.exports = ({
cache = new Keyv({ namespace: 'ssr' }),
compress: enableCompression = false,
get,
send,
revalidate = ttl => ttl / 24,
ttl: defaultTtl = 7200000,
...compressOpts
} = {}) => {
cache = new Keyv({ namespace: 'ssr' }),
compress: enableCompression = false,
get,
send,
revalidate = ttl => ttl / 24,
ttl: defaultTtl = 7200000,
...compressOpts
} = {}) => {
assert(get, '.get required')
assert(send, '.send required')

Expand All @@ -65,7 +65,14 @@ module.exports = ({
const hasForce = Boolean(
req.query ? req.query.force : parse(req.url.split('?')[1]).force
)
const key = getKey(urlResolve('http://localhost', req.url))
let isMobile = false
if (req.header('CloudFront-Is-Mobile-Viewer')) {
isMobile = req.header('CloudFront-Is-Mobile-Viewer') === 'true'
} else {
const mobileRE = /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series[46]0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i
isMobile = mobileRE.test(req.header('User-agent'))
}
const key = getKey(urlResolve('http://localhost', req.url), isMobile)
const cachedResult = await decompress(await cache.get(key))
const isHit = !hasForce && cachedResult !== undefined

Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{
"name": "cacheable-response",
"name": "aws-cacheable-response",
"description": "An HTTP compliant route path middleware for serving cache response with invalidation support.",
"homepage": "https://nicedoc.io/Kikobeats/cacheable-response",
"version": "1.5.2",
"version": "0.0.1",
"main": "index.js",
"author": {
"email": "josefrancisco.verdu@gmail.com",
"name": "Kiko Beats",
"url": "https://kikobeats.com"
"email": "kodermax@gmail.com",
"name": "Maksim Karpychev"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Kikobeats/cacheable-response.git"
"url": "git+https://github.com/kodermax/aws-cacheable-response.git"
},
"bugs": {
"url": "https://github.com/Kikobeats/cacheable-response/issues"
"url": "https://github.com/kodermax/aws-cacheable-response/issues"
},
"keywords": [
"cache",
Expand Down

0 comments on commit 21af4bd

Please sign in to comment.