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

Fix astro-static-slot hydration mismatch error #7196

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/eleven-walls-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astrojs/preact': patch
'@astrojs/react': patch
'@astrojs/vue': patch
---

Fix `astro-static-slot` hydration mismatch error
16 changes: 16 additions & 0 deletions packages/astro/e2e/nested-in-react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/'));

Expand Down
16 changes: 16 additions & 0 deletions packages/astro/e2e/nested-in-vue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('/'));

Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/preact/src/static-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } });
};

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/react/static-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
5 changes: 4 additions & 1 deletion packages/integrations/vue/static-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down