Skip to content

Commit

Permalink
Merge pull request #401 from dadi/patch/use-params-when-locating-endp…
Browse files Browse the repository at this point in the history
…oint

Include dynamic parts of page routes when determining cachability
  • Loading branch information
jimlambie authored May 30, 2018
2 parents bf5316b + 14322e2 commit 218e3ad
Show file tree
Hide file tree
Showing 3 changed files with 2,258 additions and 2,250 deletions.
32 changes: 20 additions & 12 deletions dadi/lib/cache/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* @module Cache
*/
const compressible = require('compressible')
const crypto = require('crypto')
const debug = require('debug')('web:cache')
const path = require('path')
const url = require('url')
const etag = require('etag')
const fs = require('fs')

const compressible = require('compressible')
const mime = require('mime-types')
const etag = require('etag')
const path = require('path')
const pathToRegexp = require('path-to-regexp')
const url = require('url')

const config = require(path.join(__dirname, '/../../../config.js'))
const help = require(path.join(__dirname, '/../help'))
Expand Down Expand Up @@ -79,21 +79,29 @@ Cache.prototype.cachingEnabled = function (req) {
* @returns {object}
*/
Cache.prototype.getEndpointMatchingRequest = function (req) {
const endpoints = this.server.components || {}
const requestUrl = url.parse(req.url, true).pathname.replace(/\/+$/, '')
let endpoints = this.server.components || {}
let requestUrl = url.parse(req.url, true).pathname.replace(/\/+$/, '')

if (requestUrl === '') requestUrl = '/'

// get the host key that matches the request's host header
const virtualHosts = config.get('virtualHosts')
let virtualHosts = config.get('virtualHosts')

const host =
let host =
Object.keys(virtualHosts).find(key => {
return virtualHosts.hostnames.includes(req.headers.host)
}) || ''

const matchKey = Object.keys(endpoints).find(key => {
const paths = endpoints[key].page.routes.map(route => route.path)
let matchKey = Object.keys(endpoints).find(key => {
let paths = endpoints[key].page.routes.map(route => route.path)

let matchPath = path => {
let keys = []
let regex = pathToRegexp(path, keys)
return regex.exec(requestUrl)
}

if (!paths.includes(requestUrl)) {
if (!paths.some(matchPath) && !paths.includes(requestUrl)) {
return false
}

Expand Down
12 changes: 6 additions & 6 deletions dadi/lib/debug/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ module.exports = function (req, res, next, view, page) {
return function (err, result, unprocessed) {
if (err) return next(err)

const mode = view.data.debugView
let mode = view.data.debugView
delete view.data.debugView

switch (mode) {
case 'json':
Send.json(200, res, next)(null, view.data)
break
case 'stats':
const stats = Object.assign(
let stats = Object.assign(
{},
help.timer.getStats(),
{
'Data size': help.formatBytes(
Buffer.byteLength(view.data, 'utf8'),
Buffer.byteLength(JSON.stringify(view.data), 'utf8'),
3
)
},
Expand Down Expand Up @@ -53,14 +53,14 @@ module.exports = function (req, res, next, view, page) {

headers.request = req.headers

const options = {
let options = {
host: view.data.url.hostname,
port: view.data.url.port,
path: view.data.url.pathname,
method: 'GET'
}

const newReq = http.request(options, newRes => {
let newReq = http.request(options, newRes => {
headers.response = newRes.headers

res.setHeader('Content-Type', 'text/html')
Expand Down Expand Up @@ -168,7 +168,7 @@ module.exports = function (req, res, next, view, page) {
case 'route':
let router = fs.readFileSync(require.resolve('path-to-regexp'), 'utf8')

const re = new RegExp(/module\.exports(.*)?/, 'g')
let re = new RegExp(/module\.exports(.*)?/, 'g')
router = router.replace(re, '')

res.setHeader('Content-Type', 'text/html')
Expand Down
Loading

0 comments on commit 218e3ad

Please sign in to comment.