Skip to content

Commit

Permalink
Changelog 3.1.0 :
Browse files Browse the repository at this point in the history
- Changed html-minifer to html-minifier-terser
- Add html cache
- Update config
- Update server
  • Loading branch information
aalfiann committed Nov 22, 2020
1 parent 1b864ee commit 13bed6d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 63 deletions.
10 changes: 8 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,20 @@ const config = {
// - Not all shared hosting will work.
useHTMLMinifier: false,
// Options for html minifier (this will work if you set useHTMLMinifier to true)
// For more detail, see https://github.com/kangax/html-minifier#options-quick-reference
// For more detail, see https://github.com/terser/html-minifier-terser#options-quick-reference
minifierOptions: {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true
removeEmptyAttributes: true,
minifyJS: true,
minifyCSS: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true
},
// this autoEtag will work if isProduction set to true.
autoEtagAfterHour: 12, // This will generate etag for every 12hours in a day. (max 24)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-fullstack-skeleton",
"version": "3.0.12",
"version": "3.1.0",
"description": "Just simple Fastify fullstack skeleton",
"main": "server.js",
"scripts": {
Expand Down Expand Up @@ -29,7 +29,7 @@
"fastify-cors": "^4.1.0",
"fastify-nodemailer": "^5.0.0",
"fastify-static": "^3.3.0",
"html-minifier": "^4.0.0",
"html-minifier-terser": "^5.1.1",
"make-promises-safe": "^5.1.0",
"md5": "^2.3.0",
"moment": "^2.29.1",
Expand Down
31 changes: 1 addition & 30 deletions routes/api.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
'use strict'

const config = require('../config.js')
const moment = require('moment')
const helper = require('../lib/helper.js')
const md5 = require('md5')

/**
* Inject Response Header for EJS only
* We need etag if your website sitting behind proxy server like nginx, haproxy, etc.
* @param {string} etag this is the etag value in string
* @returns {object}
*/
function injectResponseHeader (etag) {
return {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'public, max-age=' + 86400,
Expires: moment().add(86400, 'seconds').utc().format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT',
Pragma: 'public',
Etag: etag
}
}

async function apiRoute (server, options) {
server.get('/api/routes', async (request, reply) => {
// EJS view doesn't have browser cache, so we must inject it manually each routes
if (config.isProduction) {
const etag = '"' + md5(request.url + helper.autoEtag(config.autoEtagAfterHour)) + '"'
if (request.headers['if-none-match'] === etag) {
return reply.code(304).send('')
}
reply.headers(injectResponseHeader(etag))
}
server.get('/api/routes', { onRequest: server.useHtmlCache }, async (request, reply) => {
await reply.code(200).send({
message: 'Get data routes success',
statusCode: 200,
Expand Down
29 changes: 1 addition & 28 deletions routes/page.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
'use strict'

const config = require('../config.js')
const moment = require('moment')
const helper = require('../lib/helper.js')
const md5 = require('md5')
const pjson = require('../package.json')

/**
* Inject Response Header for EJS only
* We need etag if your website sitting behind proxy server like nginx, haproxy, etc.
* @param {string} etag this is the etag value in string
* @returns {object}
*/
function injectResponseHeader (etag) {
return {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'public, max-age=' + 86400,
Expires: moment().add(86400, 'seconds').utc().format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT',
Pragma: 'public',
Etag: etag
}
}

async function pageRoute (server, options) {
server.get('/', async (request, reply) => {
// EJS view doesn't have browser cache, so we must inject it manually each routes.
if (config.isProduction) {
const etag = '"' + md5(request.url + helper.autoEtag(config.autoEtagAfterHour)) + '"'
if (request.headers['if-none-match'] === etag) {
return reply.code(304).send('')
}
reply.headers(injectResponseHeader(etag))
}

server.get('/', { onRequest: server.useHtmlCache }, async (request, reply) => {
const html = await server.view('index', {
baseUrl: config.baseUrl,
baseAssetsUrl: config.baseAssetsUrl,
Expand Down
26 changes: 25 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,37 @@ const config = require('./config.js')
const cluster = require('cluster')
const numCPUs = require('os').cpus().length
const nodeMailer = require('fastify-nodemailer')
const htmlMinifier = require('html-minifier')
const htmlMinifier = require('html-minifier-terser')
const moment = require('moment')
const helper = require('./lib/helper')
const md5 = require('md5')
const server = require('fastify')({
logger: config.logger,
maxParamLength: config.maxParamLength
})

const App = async () => {
// Html Cache
server.decorate('useHtmlCache', async function (request, reply) {
function injectResponseHeader (etag) {
return {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'public, max-age=' + 86400,
Expires: moment().add(86400, 'seconds').utc().format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT',
Pragma: 'public',
Etag: etag
}
}
// Template Engine View doesn't have browser cache, so we must inject it manually each routes.
if (config.isProduction) {
const etag = '"' + md5(request.url + helper.autoEtag(config.autoEtagAfterHour)) + '"'
if (request.headers['if-none-match'] === etag) {
return reply.code(304).send('')
}
reply.headers(injectResponseHeader(etag))
}
})

// Routes
server.decorate('dataRoutes', [])
server.addHook('onRoute', (routeOptions) => {
Expand Down

0 comments on commit 13bed6d

Please sign in to comment.