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

Exclude paths from html minification #200 #292

Merged
merged 2 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,26 @@ 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')
// optionally defined the html-minifier options
const minifierOpts = {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true
Eomm marked this conversation as resolved.
Show resolved Hide resolved
}
// in template engine options configure the use of html-minifier
options: {
Eomm marked this conversation as resolved.
Show resolved Hide resolved
useHtmlMinifier: minifier,
htmlMinifierOptions: minifierOpts,
pathsToExcludeHtmlMinifier: ['/test']
Eomm marked this conversation as resolved.
Show resolved Hide resolved
}
```



Expand Down
58 changes: 36 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,18 @@ function fastifyView (fastify, opts, next) {
return viewExt ? `.${viewExt}` : (includeViewExtension ? `.${extension}` : filextension)
}

function isPathExcludedMinification (currentPath, pathsToExclude) {
return pathsToExclude.includes(currentPath)
}

function useHtmlMinification (globalOpts, requestedPath) {
return globalOptions.useHtmlMinifier &&
(typeof globalOptions.useHtmlMinifier.minify === 'function') &&
!isPathExcludedMinification(requestedPath, globalOptions.pathsToExcludeHtmlMinifier || [])
Eomm marked this conversation as resolved.
Show resolved Hide resolved
}
// Gets template as string (or precompiled for Handlebars)
// from LRU cache or filesystem.
const getTemplate = function (file, callback) {
const getTemplate = (file, callback, requestedPath) => {
Eomm marked this conversation as resolved.
Show resolved Hide resolved
const data = lru.get(file)
if (data && prod) {
callback(null, data)
Expand All @@ -161,7 +168,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 +182,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 +215,8 @@ function fastifyView (fastify, opts, next) {

function readCallback (that, page, data) {
return function _readCallback (err, html) {
const requestedPath = that?.request?.context?.config?.url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax is not supported by the actual node10 CI

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for feedback. I removed optional chaining. Removing check on request existence causes errors on tests execution, in order to avoid these I added manual check.


if (err) {
that.send(err)
return
Expand Down Expand Up @@ -241,7 +248,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 +325,7 @@ function fastifyView (fastify, opts, next) {
data = Object.assign({}, defaultCtx, this.locals, data)
// append view extension
page = getPage(page, type)
const requestedPath = this?.request?.context?.config?.url
getTemplate(page, (err, template) => {
if (err) {
this.send(err)
Expand All @@ -332,7 +340,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 +390,9 @@ function fastifyView (fastify, opts, next) {
// Append view extension.
page = getPage(page, 'njk')
env.render(join(templatesDir, page), data, (err, html) => {
const requestedPath = this?.request?.context?.config?.url
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 +421,7 @@ function fastifyView (fastify, opts, next) {
data = Object.assign({}, defaultCtx, this.locals, data)
// append view extension
page = getPage(page, 'hbs')
const requestedPath = this?.request?.context?.config?.url
getTemplate(page, (err, template) => {
if (err) {
this.send(err)
Expand All @@ -429,7 +439,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 +461,7 @@ function fastifyView (fastify, opts, next) {
}
})
}
})
}, requestedPath)
}

function viewMustache (page, data, opts) {
Expand All @@ -464,12 +474,13 @@ function fastifyView (fastify, opts, next) {
data = Object.assign({}, defaultCtx, this.locals, data)
// append view extension
page = getPage(page, 'mustache')
const requestedPath = this?.request?.context?.config?.url
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 +492,7 @@ function fastifyView (fastify, opts, next) {
}
this.send(html)
})
})
}, requestedPath)
}

function viewTwig (page, data, opts) {
Expand All @@ -494,11 +505,11 @@ function fastifyView (fastify, opts, next) {
// Append view extension.
page = getPage(page, 'twig')
engine.renderFile(join(templatesDir, page), data, (err, html) => {
const requestedPath = this?.request?.context?.config?.url
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 +531,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 = this?.request?.context?.config?.url
if (useHtmlMinification(globalOptions, requestedPath)) {
html = globalOptions.useHtmlMinifier.minify(html, globalOptions.htmlMinifierOptions || {})
}
if (!this.getHeader('content-type')) {
Expand Down Expand Up @@ -551,7 +563,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 = this?.request?.context?.config?.url
if (useHtmlMinification(globalOptions, requestedPath)) {
html = globalOptions.useHtmlMinifier.minify(html, globalOptions.htmlMinifierOptions || {})
}
if (!this.getHeader('content-type')) {
Expand Down Expand Up @@ -596,7 +609,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(this?.request?.context?.config?.url, config.pathsToExcludeHtmlMinifier || [])
) {
html = config.useHtmlMinifier.minify(
html,
Expand All @@ -609,7 +623,7 @@ function fastifyView (fastify, opts, next) {
}

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