Skip to content

Commit 77b6145

Browse files
committed
Revert "Support shared signals in Preact islands (#4763)"
This reverts commit 5e46be5.
1 parent 1e7adaf commit 77b6145

File tree

21 files changed

+39
-272
lines changed

21 files changed

+39
-272
lines changed

.changeset/itchy-tigers-help.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/framework-preact/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"dependencies": {
1414
"astro": "^1.2.8",
1515
"preact": "^10.7.3",
16-
"@astrojs/preact": "^1.1.0",
17-
"@preact/signals": "1.0.3"
16+
"@astrojs/preact": "^1.1.0"
1817
}
1918
}

examples/framework-preact/src/components/Counter.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { h, Fragment } from 'preact';
2+
import { useState } from 'preact/hooks';
23
import './Counter.css';
34

4-
export default function Counter({ children, count }) {
5-
const add = () => count.value++
6-
const subtract = () => count.value--;
5+
export default function Counter({ children }) {
6+
const [count, setCount] = useState(0);
7+
const add = () => setCount((i) => i + 1);
8+
const subtract = () => setCount((i) => i - 1);
79

810
return (
911
<>

examples/framework-preact/src/pages/index.astro

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
// Component Imports
33
import Counter from '../components/Counter';
44
5-
import { signal } from '@preact/signals';
6-
75
// Full Astro Component Syntax:
86
// https://docs.astro.build/core-concepts/astro-components/
9-
10-
const count = signal(0);
117
---
128

139
<html lang="en">
@@ -29,12 +25,8 @@ const count = signal(0);
2925
</head>
3026
<body>
3127
<main>
32-
<Counter count={count} client:visible>
33-
<h1>Hello, Preact 1!</h1>
34-
</Counter>
35-
36-
<Counter count={count} client:visible>
37-
<h1>Hello, Preact 2!</h1>
28+
<Counter client:visible>
29+
<h1>Hello, Preact!</h1>
3830
</Counter>
3931
</main>
4032
</body>

packages/astro/src/runtime/server/hydration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export async function generateHydrateScript(
135135
// Attach renderer-provided attributes
136136
if (attrs) {
137137
for (const [key, value] of Object.entries(attrs)) {
138-
island.props[key] = escapeHTML(value);
138+
island.props[key] = value;
139139
}
140140
}
141141

packages/astro/test/fixtures/preact-component/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"private": true,
55
"dependencies": {
66
"@astrojs/preact": "workspace:*",
7-
"astro": "workspace:*",
8-
"@preact/signals": "1.0.3",
9-
"preact": "^10.7.3"
7+
"astro": "workspace:*"
108
}
119
}

packages/astro/test/fixtures/preact-component/src/components/Signals.jsx

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/astro/test/fixtures/preact-component/src/pages/signals.astro

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/astro/test/fixtures/ssr-response/src/pages/some-header.astro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
Astro.response.headers.set('One-Two', 'three');
33
Astro.response.headers.set('Four-Five', 'six');
4-
Astro.response.headers.set("Cache-Control", `max-age=0, s-maxage=86400`);
54
---
65
<html>
76
<head>

packages/astro/test/preact-component.test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as cheerio from 'cheerio';
33
import { loadFixture } from './test-utils.js';
44

55
describe('Preact component', () => {
6-
/** @type {import('./test-utils').Fixture} */
76
let fixture;
87

98
before(async () => {
@@ -81,23 +80,4 @@ describe('Preact component', () => {
8180
// test 1: preact/jsx-runtime is used for the component
8281
expect(jsxRuntime).to.be.ok;
8382
});
84-
85-
it('Can use shared signals between islands', async () => {
86-
const html = await fixture.readFile('/signals/index.html');
87-
const $ = cheerio.load(html);
88-
expect($('.preact-signal')).to.have.a.lengthOf(2);
89-
90-
const sigs1Raw = $($('astro-island')[0]).attr('data-preact-signals');
91-
const sigs2Raw = $($('astro-island')[1]).attr('data-preact-signals');
92-
93-
expect(sigs1Raw).to.not.be.undefined;
94-
expect(sigs2Raw).to.not.be.undefined;
95-
96-
97-
const sigs1 = JSON.parse(sigs1Raw);
98-
const sigs2 = JSON.parse(sigs2Raw);
99-
100-
expect(sigs1.count).to.not.be.undefined;
101-
expect(sigs1.count).to.equal(sigs2.count);
102-
});
10383
});

packages/astro/test/ssr-response.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,5 @@ describe('Using Astro.response in SSR', () => {
3636
const headers = response.headers;
3737
expect(headers.get('one-two')).to.equal('three');
3838
expect(headers.get('four-five')).to.equal('six');
39-
expect(headers.get('Cache-Control')).to.equal(`max-age=0, s-maxage=86400`)
4039
});
4140
});

packages/integrations/preact/src/client-dev.ts renamed to packages/integrations/preact/client-dev.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-ignore
21
import 'preact/debug';
32
import clientFn from './client.js';
43

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { h, render } from 'preact';
2+
import StaticHtml from './static-html.js';
3+
4+
export default (element) =>
5+
(Component, props, { default: children, ...slotted }) => {
6+
if (!element.hasAttribute('ssr')) return;
7+
for (const [key, value] of Object.entries(slotted)) {
8+
props[key] = h(StaticHtml, { value, name: key });
9+
}
10+
render(
11+
h(Component, props, children != null ? h(StaticHtml, { value: children }) : children),
12+
element
13+
);
14+
};

packages/integrations/preact/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"homepage": "https://docs.astro.build/en/guides/integrations-guide/preact/",
2222
"exports": {
2323
".": "./dist/index.js",
24-
"./client.js": "./dist/client.js",
25-
"./client-dev.js": "./dist/client-dev.js",
26-
"./server.js": "./dist/server.js",
24+
"./client.js": "./client.js",
25+
"./client-dev.js": "./client-dev.js",
26+
"./server.js": "./server.js",
2727
"./package.json": "./package.json"
2828
},
2929
"scripts": {
@@ -35,8 +35,7 @@
3535
"@babel/core": ">=7.0.0-0 <8.0.0",
3636
"@babel/plugin-transform-react-jsx": "^7.17.12",
3737
"babel-plugin-module-resolver": "^4.1.0",
38-
"preact-render-to-string": "^5.2.4",
39-
"@preact/signals": "^1.1.0"
38+
"preact-render-to-string": "^5.2.0"
4039
},
4140
"devDependencies": {
4241
"astro": "workspace:*",

packages/integrations/preact/src/server.ts renamed to packages/integrations/preact/server.js

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import type { AstroPreactAttrs, RendererContext } from './types';
21
import { h, Component as BaseComponent } from 'preact';
32
import render from 'preact-render-to-string';
43
import StaticHtml from './static-html.js';
5-
import { getContext } from './context.js';
6-
import { restoreSignalsOnProps, serializeSignals } from './signals.js';
74

8-
const slotName = (str: string) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
5+
const slotName = (str) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
96

10-
let originalConsoleError: typeof console.error;
7+
let originalConsoleError;
118
let consoleFilterRefs = 0;
129

13-
function check(this: RendererContext, Component: any, props: Record<string, any>, children: any) {
10+
function check(Component, props, children) {
1411
if (typeof Component !== 'function') return false;
1512

1613
if (Component.prototype != null && typeof Component.prototype.render === 'function') {
@@ -21,7 +18,7 @@ function check(this: RendererContext, Component: any, props: Record<string, any>
2118

2219
try {
2320
try {
24-
const { html } = renderToStaticMarkup.call(this, Component, props, children);
21+
const { html } = renderToStaticMarkup(Component, props, children);
2522
if (typeof html !== 'string') {
2623
return false;
2724
}
@@ -38,33 +35,20 @@ function check(this: RendererContext, Component: any, props: Record<string, any>
3835
}
3936
}
4037

41-
function renderToStaticMarkup(this: RendererContext, Component: any, props: Record<string, any>, { default: children, ...slotted }: Record<string, any>) {
42-
const ctx = getContext(this.result);
43-
44-
const slots: Record<string, ReturnType<typeof h>> = {};
38+
function renderToStaticMarkup(Component, props, { default: children, ...slotted }) {
39+
const slots = {};
4540
for (const [key, value] of Object.entries(slotted)) {
4641
const name = slotName(key);
4742
slots[name] = h(StaticHtml, { value, name });
4843
}
49-
50-
// Restore signals back onto props so that they will be passed as-is to components
51-
let propsMap = restoreSignalsOnProps(ctx, props);
52-
44+
// Note: create newProps to avoid mutating `props` before they are serialized
5345
const newProps = { ...props, ...slots };
54-
55-
const attrs: AstroPreactAttrs = {};
56-
serializeSignals(ctx, props, attrs, propsMap);
57-
5846
const html = render(
5947
h(Component, newProps, children != null ? h(StaticHtml, { value: children }) : children)
6048
);
61-
return {
62-
attrs,
63-
html
64-
};
49+
return { html };
6550
}
6651

67-
6852
/**
6953
* Reduces console noise by filtering known non-problematic errors.
7054
*
@@ -107,7 +91,7 @@ function finishUsingConsoleFilter() {
10791
* Ignores known non-problematic errors while any code is using the console filter.
10892
* Otherwise, simply forwards all arguments to the original function.
10993
*/
110-
function filteredConsoleError(msg: string, ...rest: any[]) {
94+
function filteredConsoleError(msg, ...rest) {
11195
if (consoleFilterRefs > 0 && typeof msg === 'string') {
11296
// In `check`, we attempt to render JSX components through Preact.
11397
// When attempting this on a React component, React may output

packages/integrations/preact/src/client.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/integrations/preact/src/context.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/integrations/preact/src/signals.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)