Skip to content

Commit

Permalink
feat: Exclude paths from html minification #200 (#292)
Browse files Browse the repository at this point in the history
* feat: start path excluded from html minification

* fix: CI, remove empty array init, doc
  • Loading branch information
Adibla committed Jan 29, 2022
1 parent 609cc8f commit 8a4aa97
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 22 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,29 @@ const minifierOpts = {
htmlMinifierOptions: minifierOpts
}
```
To filter some paths from minification, you can add the option `pathsToExcludeHtmlMinifier` with list of paths
```js
// get a reference to html-minifier
const minifier = require('html-minifier')
// in options configure the use of html-minifier and set paths to exclude from minification
const options = {
useHtmlMinifier: minifier,
pathsToExcludeHtmlMinifier: ['/test']
}

fastify.register(require("point-of-view"), {
engine: {
ejs: require('ejs')
},
options
});

// This path is excluded from minification
fastify.get("/test", (req, reply) => {
reply.view("./template/index.ejs", { text: "text" });
});

```



Expand Down
62 changes: 40 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ function fastifyView (fastify, opts, next) {
next(new Error('Missing engine'))
return
}

const type = Object.keys(opts.engine)[0]
if (supportedEngines.indexOf(type) === -1) {
next(new Error(`'${type}' not yet supported, PR? :)`))
return
}

const charset = opts.charset || 'utf-8'
const propertyName = opts.propertyName || 'view'
const engine = opts.engine[type]
Expand Down Expand Up @@ -149,9 +147,22 @@ function fastifyView (fastify, opts, next) {
return viewExt ? `.${viewExt}` : (includeViewExtension ? `.${extension}` : filextension)
}

function isPathExcludedMinification (currentPath, pathsToExclude) {
return (pathsToExclude && Array.isArray(pathsToExclude)) ? pathsToExclude.includes(currentPath) : false
}

function useHtmlMinification (globalOpts, requestedPath) {
return globalOptions.useHtmlMinifier &&
(typeof globalOptions.useHtmlMinifier.minify === 'function') &&
!isPathExcludedMinification(requestedPath, globalOptions.pathsToExcludeHtmlMinifier)
}

function getRequestedPath (fastify) {
return (fastify && fastify.request) ? fastify.request.context.config.url : null
}
// Gets template as string (or precompiled for Handlebars)
// from LRU cache or filesystem.
const getTemplate = function (file, callback) {
const getTemplate = function (file, callback, requestedPath) {
const data = lru.get(file)
if (data && prod) {
callback(null, data)
Expand All @@ -161,7 +172,8 @@ function fastifyView (fastify, opts, next) {
callback(err, null)
return
}
if (globalOptions.useHtmlMinifier && (typeof globalOptions.useHtmlMinifier.minify === 'function')) {

if (useHtmlMinification(globalOptions, requestedPath)) {
data = globalOptions.useHtmlMinifier.minify(data, globalOptions.htmlMinifierOptions || {})
}
if (type === 'handlebars') {
Expand All @@ -174,27 +186,24 @@ function fastifyView (fastify, opts, next) {
}

// Gets partials as collection of strings from LRU cache or filesystem.
const getPartials = function (page, partials, callback) {
const getPartials = function (page, { partials, requestedPath }, callback) {
const partialsObj = lru.get(`${page}-Partials`)

if (partialsObj && prod) {
callback(null, partialsObj)
} else {
let filesToLoad = Object.keys(partials).length

if (filesToLoad === 0) {
callback(null, {})
return
}

let error = null
const partialsHtml = {}
Object.keys(partials).forEach((key, index) => {
readFile(join(templatesDir, partials[key]), 'utf-8', (err, data) => {
if (err) {
error = err
}
if (globalOptions.useHtmlMinifier && (typeof globalOptions.useHtmlMinifier.minify === 'function')) {
if (useHtmlMinification(globalOptions, requestedPath)) {
data = globalOptions.useHtmlMinifier.minify(data, globalOptions.htmlMinifierOptions || {})
}

Expand All @@ -210,6 +219,8 @@ function fastifyView (fastify, opts, next) {

function readCallback (that, page, data) {
return function _readCallback (err, html) {
const requestedPath = getRequestedPath(that)

if (err) {
that.send(err)
return
Expand Down Expand Up @@ -241,7 +252,7 @@ function fastifyView (fastify, opts, next) {
} catch (error) {
cachedPage = error
}
if (globalOptions.useHtmlMinifier && (typeof globalOptions.useHtmlMinifier.minify === 'function')) {
if (useHtmlMinification(globalOptions, requestedPath)) {
cachedPage = globalOptions.useHtmlMinifier.minify(cachedPage, globalOptions.htmlMinifierOptions || {})
}
that.send(cachedPage)
Expand Down Expand Up @@ -318,6 +329,7 @@ function fastifyView (fastify, opts, next) {
data = Object.assign({}, defaultCtx, this.locals, data)
// append view extension
page = getPage(page, type)
const requestedPath = getRequestedPath(this)
getTemplate(page, (err, template) => {
if (err) {
this.send(err)
Expand All @@ -332,7 +344,7 @@ function fastifyView (fastify, opts, next) {
return
}
readFile(join(templatesDir, page), 'utf8', readCallback(this, page, data))
})
}, requestedPath)
}

function viewArtTemplate (page, data) {
Expand Down Expand Up @@ -382,8 +394,9 @@ function fastifyView (fastify, opts, next) {
// Append view extension.
page = getPage(page, 'njk')
env.render(join(templatesDir, page), data, (err, html) => {
const requestedPath = getRequestedPath(this)
if (err) return this.send(err)
if (globalOptions.useHtmlMinifier && (typeof globalOptions.useHtmlMinifier.minify === 'function')) {
if (useHtmlMinification(globalOptions, requestedPath)) {
html = globalOptions.useHtmlMinifier.minify(html, globalOptions.htmlMinifierOptions || {})
}
this.header('Content-Type', 'text/html; charset=' + charset)
Expand Down Expand Up @@ -412,6 +425,7 @@ function fastifyView (fastify, opts, next) {
data = Object.assign({}, defaultCtx, this.locals, data)
// append view extension
page = getPage(page, 'hbs')
const requestedPath = getRequestedPath(this)
getTemplate(page, (err, template) => {
if (err) {
this.send(err)
Expand All @@ -429,7 +443,7 @@ function fastifyView (fastify, opts, next) {
this.send(e)
}
} else {
getPartials(type, options.partials || {}, (err, partialsObject) => {
getPartials(type, { partials: options.partials || {}, requestedPath: requestedPath }, (err, partialsObject) => {
if (err) {
this.send(err)
return
Expand All @@ -451,7 +465,7 @@ function fastifyView (fastify, opts, next) {
}
})
}
})
}, requestedPath)
}

function viewMustache (page, data, opts) {
Expand All @@ -464,12 +478,13 @@ function fastifyView (fastify, opts, next) {
data = Object.assign({}, defaultCtx, this.locals, data)
// append view extension
page = getPage(page, 'mustache')
const requestedPath = getRequestedPath(this)
getTemplate(page, (err, templateString) => {
if (err) {
this.send(err)
return
}
getPartials(page, options.partials || {}, (err, partialsObject) => {
getPartials(page, { partials: options.partials || {}, requestedPath: requestedPath }, (err, partialsObject) => {
if (err) {
this.send(err)
return
Expand All @@ -481,7 +496,7 @@ function fastifyView (fastify, opts, next) {
}
this.send(html)
})
})
}, requestedPath)
}

function viewTwig (page, data, opts) {
Expand All @@ -494,11 +509,11 @@ function fastifyView (fastify, opts, next) {
// Append view extension.
page = getPage(page, 'twig')
engine.renderFile(join(templatesDir, page), data, (err, html) => {
const requestedPath = getRequestedPath(this)
if (err) {
return this.send(err)
}

if (globalOptions.useHtmlMinifier && (typeof globalOptions.useHtmlMinifier.minify === 'function')) {
if (useHtmlMinification(globalOptions, requestedPath)) {
html = globalOptions.useHtmlMinifier.minify(html, globalOptions.htmlMinifierOptions || {})
}
if (!this.getHeader('content-type')) {
Expand All @@ -520,7 +535,8 @@ function fastifyView (fastify, opts, next) {

engine.renderFile(join(templatesDir, page), data, opts)
.then((html) => {
if (globalOptions.useHtmlMinifier && (typeof globalOptions.useHtmlMinifier.minify === 'function')) {
const requestedPath = getRequestedPath(this)
if (useHtmlMinification(globalOptions, requestedPath)) {
html = globalOptions.useHtmlMinifier.minify(html, globalOptions.htmlMinifierOptions || {})
}
if (!this.getHeader('content-type')) {
Expand Down Expand Up @@ -551,7 +567,8 @@ function fastifyView (fastify, opts, next) {
}
data = Object.assign({}, defaultCtx, this.locals, data)
let html = renderModule[page](data)
if (globalOptions.useHtmlMinifier && (typeof globalOptions.useHtmlMinifier.minify === 'function')) {
const requestedPath = getRequestedPath(this)
if (useHtmlMinification(globalOptions, requestedPath)) {
html = globalOptions.useHtmlMinifier.minify(html, globalOptions.htmlMinifierOptions || {})
}
if (!this.getHeader('content-type')) {
Expand Down Expand Up @@ -596,7 +613,8 @@ function fastifyView (fastify, opts, next) {
if (err) return this.send(err)
if (
config.useHtmlMinifier &&
typeof config.useHtmlMinifier.minify === 'function'
typeof config.useHtmlMinifier.minify === 'function' &&
!isPathExcludedMinification(getRequestedPath(this), config.pathsToExcludeHtmlMinifier)
) {
html = config.useHtmlMinifier.minify(
html,
Expand All @@ -609,7 +627,7 @@ function fastifyView (fastify, opts, next) {
}

if (prod && type === 'handlebars' && globalOptions.partials) {
getPartials(type, globalOptions.partials, (err, partialsObject) => {
getPartials(type, { partials: globalOptions.partials || {}, requestedPath: getRequestedPath(this) }, (err, partialsObject) => {
if (err) {
next(err)
return
Expand Down

0 comments on commit 8a4aa97

Please sign in to comment.