Skip to content

Commit 1c77779

Browse files
authored
Fix astro-static-slot hydration mismatch error (#7196)
1 parent 6c7df28 commit 1c77779

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

.changeset/eleven-walls-explain.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astrojs/preact': patch
3+
'@astrojs/react': patch
4+
'@astrojs/vue': patch
5+
---
6+
7+
Fix `astro-static-slot` hydration mismatch error

packages/astro/e2e/nested-in-react.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ test.afterAll(async () => {
1414
});
1515

1616
test.describe('Nested Frameworks in React', () => {
17+
test('No hydration mismatch', async ({ page, astro }) => {
18+
// Get browser logs
19+
const logs = [];
20+
page.on('console', (msg) => logs.push(msg.text()));
21+
22+
await page.goto(astro.resolveUrl('/'));
23+
24+
// wait for root island to hydrate
25+
const counter = page.locator('#react-counter');
26+
await waitForHydrate(page, counter);
27+
28+
for (const log of logs) {
29+
expect(log, 'React hydration mismatch').not.toMatch('An error occurred during hydration');
30+
}
31+
});
32+
1733
test('React counter', async ({ astro, page }) => {
1834
await page.goto(astro.resolveUrl('/'));
1935

packages/astro/e2e/nested-in-vue.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ test.afterAll(async () => {
1414
});
1515

1616
test.describe('Nested Frameworks in Vue', () => {
17+
test('no hydration mismatch', async ({ page, astro }) => {
18+
// Get browser logs
19+
const logs = [];
20+
page.on('console', (msg) => logs.push(msg.text()));
21+
22+
await page.goto(astro.resolveUrl('/'));
23+
24+
// wait for root island to hydrate
25+
const counter = page.locator('#vue-counter');
26+
await waitForHydrate(page, counter);
27+
28+
for (const log of logs) {
29+
expect(log, 'Vue hydration mismatch').not.toMatch('Hydration node mismatch');
30+
}
31+
});
32+
1733
test('React counter', async ({ astro, page }) => {
1834
await page.goto(astro.resolveUrl('/'));
1935

packages/integrations/preact/src/static-html.ts

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

packages/integrations/react/static-html.js

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

packages/integrations/vue/static-html.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const StaticHtml = defineComponent({
1010
props: {
1111
value: String,
1212
name: String,
13-
hydrate: Boolean,
13+
hydrate: {
14+
type: Boolean,
15+
default: true,
16+
},
1417
},
1518
setup({ name, value, hydrate }) {
1619
if (!value) return () => null;

0 commit comments

Comments
 (0)