#How to minify page source code to one line in Nuxt 3

44 messages · Page 1 of 1 (latest)

runic summit
#

You can compress your files by adding the following to nuxt config. This way, the page will weigh a fraction of its original size compared to its raw html version. If the server doesn't support brotli, it should fall back to gzip.

nitro: {
compressPublicAssets: {
gzip: true, brotli: true 
}
}

https://nitro.unjs.io/config#compresspublicassets

Customize your Nitro app with a configuration file.

steady shoal
#

@runic summit only this in nuxt.config.ts doesn't work. Do i need to create a nitro.config.ts file;

runic summit
#

You don’t need a separate Nitro config. After running generate, are there any .br and .gz files in your .output directory? BTW, works with build too.

runic summit
#
export default defineNuxtConfig({
  nitro: {
    compressPublicAssets: {
      gzip: true, brotli: true 
    }
  }
})
steady shoal
#

@runic summit Thank you very much for your response and your time! But even in your own project when I press ctrl + U in the browser, it doesn't show me the code in one line.

south swallow
#

@steady shoal the "one line" really doesn't matter when it is compressed 🙂

#

it won't give a significant performance boost

steady shoal
#

@south swallow I agree with you but that's what they told me to do.

south swallow
weary yew
#

@south swallow possible feature request for nitro ? 🤔

south swallow
weary yew
#

I don't know either, maybe for lighthouse ?

south swallow
steady shoal
south swallow
#

@steady shoal create the plugin file in /server/plugins/yourPlugin.ts

#

because it is a nitro plugin, it lives in the server dir

steady shoal
#

You are right!! And in nuxt.config.ts;

weary yew
weary yew
steady shoal
#

Yes I know but @south swallow said me to hook into render:html

runic summit
#

@steady shoal Sorry, I mispoke since minifying and compression are not the same thing. I should've said Nitro will shrink your pages by compressing them. Still the greatest gain is the compression, not minimizing. After manually minifying my index HTML file it was still 46kb. By comparison the brotli version was 15kb. Minimizing only shaved off mere bytes while compression shaved off 31kb. With that said, my demo pages don't contain a lot of content so it's not a fair assessment.

steady shoal
#

I do this inside server/plugins/yourPlugin.ts but it doesn't work yet.```import { minify } from 'html-minifier';

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('render:html', (html, { event }) => {
const minifiedHtml = minify(html, {
collapseBooleanAttributes: true,
decodeEntities: true,
minifyCSS: true,
minifyJS: true,
processConditionalComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
trimCustomFragments: true,
useShortDoctype: true,
preserveLineBreaks: false,
collapseWhitespace: true,
})
html = minifiedHtml
})
})```

#

Do you have any idea?

south swallow
#

Try

import { minify } from 'html-minifier';

export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('render:response', (html) => {
    const minifiedHtml = minify(html.body, {
      collapseBooleanAttributes: true,
      decodeEntities: true,
      minifyCSS: true,
      minifyJS: true,
      processConditionalComments: true,
      removeEmptyAttributes: true,
      removeRedundantAttributes: true,
      trimCustomFragments: true,
      useShortDoctype: true,
      preserveLineBreaks: false,
      collapseWhitespace: true,
    })
    html.body = minifiedHtml
  })
})
steady shoal
#

It works! Thank you very much!!

south swallow
weary yew
#

I'm curious to know how much kb did you lose @steady shoal 🤔

steady shoal
#

@weary yew how can i check this;

weary yew
#

check the size of the html received before and afterusing html-minifier

steady shoal
#

It's the same..51kb. But the purpose is to be smaller with html-minifier...

weary yew
#

oh 😢 was it a big html ? .... guess you were right @south swallow

steady shoal
#

yes it's big

steady shoal
#

My mistake....without html-minifier was 43.5kB and with html-minifier was 42.7kB. The difference is small. Maybe, i should compress my files as well?

south swallow
#

Ideally you can use brotli

#

with GZIP fallback

steady shoal
#

Do I need to put it inside server/plugins/yourPlugin.ts?