Skip to content

Minify HTML in production #3666

Answered by tomgobich
galaczi asked this question in Help
Apr 7, 2022 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

You can apply a global middleware, check to ensure it matches your requests with HTML output, and minify it within there. Place all your logic after await next() and it'll run at the end of your request instead of at the start.

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
const minify = require('html-minifier').minify;

export default class HtmlMinify {
  public async handle({ response, request }: HttpContextContract, next: () => Promise<void>) {
    // code for middleware goes here. ABOVE THE NEXT CALL
    await next()

    const method = request.method()
    const accepts = request.accepts([]) ?? [] as string[]

    if (method === 'GET' && accepts.includes('text/html'

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@galaczi
Comment options

Answer selected by galaczi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants