Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include dynamic parts of page routes when determining cachability #401

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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