Prevent client: attributes from being passed to components (#891)

* Prevent client: attributes from being passed to components

* Adds a changeset
This commit is contained in:
Matthew Phillips 2021-07-27 16:01:15 -04:00 committed by GitHub
parent ed0ed49df6
commit d8cebb0132
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Removes a warning in Svelte hydrated components

View file

@ -30,6 +30,8 @@ const traverse: typeof babelTraverse.default = (babelTraverse.default as any).de
const babelGenerator: typeof _babelGenerator = _babelGenerator.default;
const { transformSync } = esbuild;
const hydrationDirectives = new Set(['client:load', 'client:idle', 'client:visible', 'client:media']);
interface Attribute {
start: number;
end: number;
@ -55,8 +57,6 @@ function findHydrationAttributes(attrs: Record<string, string>): HydrationAttrib
let method: HydrationAttributes['method'];
let value: undefined | string;
const hydrationDirectives = new Set(['client:load', 'client:idle', 'client:visible', 'client:media']);
for (const [key, val] of Object.entries(attrs)) {
if (hydrationDirectives.has(key)) {
method = key.slice(7) as HydrationAttributes['method'];
@ -155,7 +155,9 @@ function getTextFromAttribute(attr: any): string {
function generateAttributes(attrs: Record<string, string>): string {
let result = '{';
for (const [key, val] of Object.entries(attrs)) {
if (key.startsWith('...')) {
if (hydrationDirectives.has(key)) {
continue;
} else if (key.startsWith('...')) {
result += key + ',';
} else {
result += JSON.stringify(key) + ':' + val + ',';

View file

@ -39,4 +39,10 @@ Components('Still throws an error for undefined components', async ({ runtime })
assert.equal(result.statusCode, 500);
});
Components('Svelte component', async ({ runtime }) => {
const result = await runtime.load('/client');
const html = result.contents;
assert.ok(!/"client:load": true/.test(html), 'Client attrs not added');
});
Components.run();

View file

@ -0,0 +1,9 @@
---
import SvelteComponent from '../components/Component.svelte';
---
<html>
<head><title>Components</title></head>
<body>
<SvelteComponent client:load />
</body>
</html>