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

Compressing html results in hydration errors #11

Closed
preetamslot opened this issue Aug 8, 2022 · 6 comments
Closed

Compressing html results in hydration errors #11

preetamslot opened this issue Aug 8, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@preetamslot
Copy link

Hi,
I am using Vue, and compressing the HTML gives me hydration errors on every Vue component.
Is there a way to fix this?

@NikolaRHristov
Copy link
Member

NikolaRHristov commented Aug 8, 2022

I'll take a look. Check the options for html-minifier-terser https://github.com/Playform/astro-compress/blob/main/src/options/html.ts and modify them a bit.

These are the defaults used by astro-compress:

const defaultOptions: Options = {
	html: {
		caseSensitive: true,
		collapseBooleanAttributes: true,
		collapseInlineTagWhitespace: false,
		collapseWhitespace: true,
		conservativeCollapse: false,
		continueOnParseError: false,
		customAttrAssign: [],
		customAttrCollapse: "",
		customAttrSurround: [],
		customEventAttributes: [/^on[a-z]{3,}$/],
		decodeEntities: false,
		html5: true,
		ignoreCustomComments: [],
		ignoreCustomFragments: [],
		includeAutoGeneratedTags: true,
		keepClosingSlash: true,
		maxLineLength: null,
		minifyCSS: true,
		minifyJS: true,
		minifyURLs: false,
		preserveLineBreaks: false,
		preventAttributesEscaping: false,
		processConditionalComments: true,
		processScripts: ["module"],
		quoteCharacter: "",
		removeAttributeQuotes: true,
		removeComments: true,
		removeEmptyAttributes: true,
		removeEmptyElements: false,
		removeOptionalTags: false,
		removeRedundantAttributes: true,
		removeScriptTypeAttributes: true,
		removeStyleLinkTypeAttributes: true,
		removeTagWhitespace: true,
		sortAttributes: true,
		sortClassName: true,
		trimCustomFragments: false,
		useShortDoctype: false,
	},
};

It could be the removeEmptyAttributes option. Try setting it to false and re-running the build script.

import { defineConfig } from 'astro/config'

export default defineConfig({
	integrations: [
		compress({
			html: {
				removeEmptyAttributes: false,
			},
		}),
	],
};

@preetamslot
Copy link
Author

Thanks!

html: {
	removeEmptyAttributes: false,
	}

Worked for me!

NikolaRHristov added a commit that referenced this issue Aug 9, 2022
@preetamslot
Copy link
Author

Just see that if i use

compress({
			html: {
				removeEmptyAttributes: false,
			},
		}),

all other HTML compressions are not processed, so I had no hydration errors.

The critter in my case was removeComments.
if true it gave hydration errors even if my code did not have any <!--comments -->

What worked for me was:

html: {
        caseSensitive: true,
        collapseBooleanAttributes: true,
        collapseInlineTagWhitespace: false,
        collapseWhitespace: true,
        conservativeCollapse: false,
        continueOnParseError: false,
        customAttrAssign: [],
        customAttrCollapse: "",
        customAttrSurround: [],
        customEventAttributes: [/^on[a-z]{3,}$/],
        decodeEntities: false,
        html5: true,
        ignoreCustomComments: [],
        ignoreCustomFragments: [],
        includeAutoGeneratedTags: true,
        keepClosingSlash: true,
        maxLineLength: null,
        minifyCSS: true,
        minifyJS: true,
        minifyURLs: false,
        preserveLineBreaks: false,
        preventAttributesEscaping: false,
        processConditionalComments: true,
        processScripts: ["module"],
        quoteCharacter: "",
        removeAttributeQuotes: true,
        removeComments: false,
        removeEmptyAttributes: false,
        removeEmptyElements: false,
        removeOptionalTags: false,
        removeRedundantAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true,
        removeTagWhitespace: true,
        sortAttributes: true,
        sortClassName: true,
        trimCustomFragments: false,
        useShortDoctype: false,
      },

@preetamslot preetamslot reopened this Aug 9, 2022
@NikolaRHristov
Copy link
Member

Thank you! I don't know about the comments issue, but this made me fix another one regarding the options merging. Right now, they're merged flat, but I made it merge deep, so now no other issues like that should emerge.

@NikolaRHristov
Copy link
Member

NikolaRHristov commented Aug 9, 2022

I'll take a closer look at the comments one another time. But it is set to false for now in v0.0.17

@NikolaRHristov NikolaRHristov added the bug Something isn't working label Aug 16, 2022
@NikolaRHristov
Copy link
Member

Fixed in 0aa6056

NikolaRHristov added a commit that referenced this issue Oct 29, 2022
NikolaRHristov added a commit that referenced this issue Oct 29, 2022
NikolaRHristov added a commit that referenced this issue Oct 30, 2022
NikolaRHristov added a commit that referenced this issue Oct 31, 2022
NikolaRHristov added a commit that referenced this issue Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants