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

[Lit] Fix hydration not having the same reactive values as server #6080

Merged
merged 8 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-tomatoes-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/lit': major
---

Serialize Lit JSX bindings in HTML for hydration
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import MyCounter from '../components/Counter.js';

const someProps = {
count: 0,
count: 10,
};
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import MyCounter from '../components/Counter.js';

const someProps = {
count: 0,
count: 10,
};
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import MyCounter from '../components/Counter.js';

const someProps = {
count: 0,
count: 10,
};
---

Expand Down
18 changes: 9 additions & 9 deletions packages/astro/e2e/lit-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ test.describe('Lit components', () => {
await expect(counter).toHaveCount(1);

const count = counter.locator('p');
await expect(count, 'initial count is 0').toHaveText('Count: 0');
await expect(count, 'initial count is 10').toHaveText('Count: 10');

const inc = counter.locator('button');
await inc.click();

await expect(count, 'count incremented by 1').toHaveText('Count: 1');
await expect(count, 'count incremented by 1').toHaveText('Count: 11');
});

t('client:load', async ({ page, astro }) => {
Expand All @@ -47,12 +47,12 @@ test.describe('Lit components', () => {
await expect(counter, 'component is visible').toBeVisible();

const count = counter.locator('p');
await expect(count, 'initial count is 0').toHaveText('Count: 0');
await expect(count, 'initial count is 10').toHaveText('Count: 10');

const inc = counter.locator('button');
await inc.click();

await expect(count, 'count incremented by 1').toHaveText('Count: 1');
await expect(count, 'count incremented by 1').toHaveText('Count: 11');
});

t('client:visible', async ({ page, astro }) => {
Expand All @@ -64,12 +64,12 @@ test.describe('Lit components', () => {
await expect(counter, 'component is visible').toBeVisible();

const count = counter.locator('p');
await expect(count, 'initial count is 0').toHaveText('Count: 0');
await expect(count, 'initial count is 10').toHaveText('Count: 10');

const inc = counter.locator('button');
await inc.click();

await expect(count, 'count incremented by 1').toHaveText('Count: 1');
await expect(count, 'count incremented by 1').toHaveText('Count: 11');
});

t('client:media', async ({ page, astro }) => {
Expand All @@ -79,18 +79,18 @@ test.describe('Lit components', () => {
await expect(counter, 'component is visible').toBeVisible();

const count = counter.locator('p');
await expect(count, 'initial count is 0').toHaveText('Count: 0');
await expect(count, 'initial count is 10').toHaveText('Count: 10');

const inc = counter.locator('button');
await inc.click();

await expect(count, 'component not hydrated yet').toHaveText('Count: 0');
await expect(count, 'component not hydrated yet').toHaveText('Count: 10');

// Reset the viewport to hydrate the component (max-width: 50rem)
await page.setViewportSize({ width: 414, height: 1124 });

await inc.click();
await expect(count, 'count incremented by 1').toHaveText('Count: 1');
await expect(count, 'count incremented by 1').toHaveText('Count: 11');
});

t.skip('HMR', async ({ page, astro }) => {
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
".": "./dist/index.js",
"./server.js": "./server.js",
"./client-shim.js": "./client-shim.js",
"./dist/client.js": "./dist/client.js",
"./hydration-support.js": "./hydration-support.js",
"./package.json": "./package.json"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/lit/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function* render(Component, attrs, slots) {

instance.connectedCallback();

yield `<${tagName}`;
yield `<${tagName} defer-hydration`;
e111077 marked this conversation as resolved.
Show resolved Hide resolved
yield* instance.renderAttributes();
yield `>`;
const shadowContents = instance.renderShadow({});
Expand Down
22 changes: 22 additions & 0 deletions packages/integrations/lit/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default (element: HTMLElement) =>
async (
Component: any,
props: Record<string, any>,
) => {
// Get the LitElement element instance (may or may not be upgraded).
const component = element.children[0] as HTMLElement;
if (!component) {
return;
}

// Set properties on the LitElement instance for resuming hydration.
for (let [name, value] of Object.entries(props)) {
// Check if reactive property or class property.
if (name in Component.prototype) {
(component as any)[name] = value;
}
}

// Tell LitElement to resume hydration.
component.removeAttribute('defer-hydration');
};
2 changes: 2 additions & 0 deletions packages/integrations/lit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function getViteConfiguration() {
return {
optimizeDeps: {
include: [
'@astrojs/lit/dist/client.js',
'@astrojs/lit/client-shim.js',
'@astrojs/lit/hydration-support.js',
'@webcomponents/template-shadowroot/template-shadowroot.js',
Expand Down Expand Up @@ -34,6 +35,7 @@ export default function (): AstroIntegration {
addRenderer({
name: '@astrojs/lit',
serverEntrypoint: '@astrojs/lit/server.js',
clientEntrypoint: '@astrojs/lit/dist/client.js',
});
// Update the vite configuration.
updateConfig({
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/lit/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('renderToStaticMarkup', () => {
customElements.define(tagName, class extends LitElement {});
const render = await renderToStaticMarkup(tagName);
expect(render).to.deep.equal({
html: `<${tagName}><template shadowroot="open"><!--lit-part--><!--/lit-part--></template></${tagName}>`,
html: `<${tagName} defer-hydration><template shadowroot="open"><!--lit-part--><!--/lit-part--></template></${tagName}>`,
});
});

Expand Down