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:
parent
ed0ed49df6
commit
d8cebb0132
4 changed files with 25 additions and 3 deletions
5
.changeset/eight-monkeys-hammer.md
Normal file
5
.changeset/eight-monkeys-hammer.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Removes a warning in Svelte hydrated components
|
|
@ -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 + ',';
|
||||
|
|
|
@ -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();
|
||||
|
|
9
packages/astro/test/fixtures/astro-components/src/pages/client.astro
vendored
Normal file
9
packages/astro/test/fixtures/astro-components/src/pages/client.astro
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
import SvelteComponent from '../components/Component.svelte';
|
||||
---
|
||||
<html>
|
||||
<head><title>Components</title></head>
|
||||
<body>
|
||||
<SvelteComponent client:load />
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue