Fix astro-static-slot hydration mismatch error (#7196)

This commit is contained in:
Bjorn Lu 2023-05-26 22:37:33 +08:00 committed by GitHub
parent 6c7df28ab3
commit 1c77779dd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 4 deletions

View file

@ -0,0 +1,7 @@
---
'@astrojs/preact': patch
'@astrojs/react': patch
'@astrojs/vue': patch
---
Fix `astro-static-slot` hydration mismatch error

View file

@ -14,6 +14,22 @@ test.afterAll(async () => {
});
test.describe('Nested Frameworks in React', () => {
test('No hydration mismatch', async ({ page, astro }) => {
// Get browser logs
const logs = [];
page.on('console', (msg) => logs.push(msg.text()));
await page.goto(astro.resolveUrl('/'));
// wait for root island to hydrate
const counter = page.locator('#react-counter');
await waitForHydrate(page, counter);
for (const log of logs) {
expect(log, 'React hydration mismatch').not.toMatch('An error occurred during hydration');
}
});
test('React counter', async ({ astro, page }) => {
await page.goto(astro.resolveUrl('/'));

View file

@ -14,6 +14,22 @@ test.afterAll(async () => {
});
test.describe('Nested Frameworks in Vue', () => {
test('no hydration mismatch', async ({ page, astro }) => {
// Get browser logs
const logs = [];
page.on('console', (msg) => logs.push(msg.text()));
await page.goto(astro.resolveUrl('/'));
// wait for root island to hydrate
const counter = page.locator('#vue-counter');
await waitForHydrate(page, counter);
for (const log of logs) {
expect(log, 'Vue hydration mismatch').not.toMatch('Hydration node mismatch');
}
});
test('React counter', async ({ astro, page }) => {
await page.goto(astro.resolveUrl('/'));

View file

@ -13,9 +13,9 @@ type Props = {
* As a bonus, we can signal to Preact that this subtree is
* entirely static and will never change via `shouldComponentUpdate`.
*/
const StaticHtml = ({ value, name, hydrate }: Props) => {
const StaticHtml = ({ value, name, hydrate = true }: Props) => {
if (!value) return null;
const tagName = hydrate === false ? 'astro-static-slot' : 'astro-slot';
const tagName = hydrate ? 'astro-slot' : 'astro-static-slot';
return h(tagName, { name, dangerouslySetInnerHTML: { __html: value } });
};

View file

@ -7,7 +7,7 @@ import { createElement as h } from 'react';
* As a bonus, we can signal to React that this subtree is
* entirely static and will never change via `shouldComponentUpdate`.
*/
const StaticHtml = ({ value, name, hydrate }) => {
const StaticHtml = ({ value, name, hydrate = true }) => {
if (!value) return null;
const tagName = hydrate ? 'astro-slot' : 'astro-static-slot';
return h(tagName, {

View file

@ -10,7 +10,10 @@ const StaticHtml = defineComponent({
props: {
value: String,
name: String,
hydrate: Boolean,
hydrate: {
type: Boolean,
default: true,
},
},
setup({ name, value, hydrate }) {
if (!value) return () => null;