I'm currently working on a Nuxt 3 project with the following configuration:
export default defineNuxtConfig({
srcDir: "src/",
devtools: { enabled: true },
ssr: true,
modules: [ ... ],
imports: { ...},
build: { ...},
routeRules: {...},
devServer: {https: true},
nitro: {
compressPublicAssets: {
gzip: true,
brotli: true
}
},
I have compressPublicAssets
enabled with both gzip and brotli, but I've noticed this only compresses static assets in the public directory, not the dynamically generated HTML pages.
What's the proper way to configure Nuxt 3 to compress the actual HTML responses with Brotli compression? Do I need additional configuration in my nuxt.config.ts file, or is there another approach I should take?
I've tried looking through the documentation but haven't found clear instructions for compressing server-rendered HTML specifically.
I'm currently working on a Nuxt 3 project with the following configuration:
export default defineNuxtConfig({
srcDir: "src/",
devtools: { enabled: true },
ssr: true,
modules: [ ... ],
imports: { ...},
build: { ...},
routeRules: {...},
devServer: {https: true},
nitro: {
compressPublicAssets: {
gzip: true,
brotli: true
}
},
I have compressPublicAssets
enabled with both gzip and brotli, but I've noticed this only compresses static assets in the public directory, not the dynamically generated HTML pages.
What's the proper way to configure Nuxt 3 to compress the actual HTML responses with Brotli compression? Do I need additional configuration in my nuxt.config.ts file, or is there another approach I should take?
I've tried looking through the documentation but haven't found clear instructions for compressing server-rendered HTML specifically.
- Compressing is before server response, not during build. – typed-sigterm Commented Mar 9 at 15:02
- Consider using a service like cloudflare for compression. Would probably do the trick. – Clout Hack Commented Mar 10 at 9:42
1 Answer
Reset to default 0I've managed to compress HTML files using nitro plugin and h3-compression library.
import { useCompression } from "h3-compression";
export default defineNitroPlugin(nitroApp => {
nitroApp.hooks.hook("render:response", async (response, { event }) => {
try {
const contentType = response.headers?.["content-type"];
if (contentType && contentType?.includes("text/html")) {
await useCompression(event, response);
}
} catch (e) {
logger.error("Compressing error:", e);
}
});
});
However, this add around 100ms of loading time per file, while achieving 60% reduction on html size.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744910108a4600495.html
评论列表(0条)