-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add preact-ssr-prepass (#9524)
* feat: add preact-ssr-prepass * added more info to changelog * fix example in changelog * fix changelog description * fix tab in code of changelog * Update .changeset/blue-bobcats-remain.md --------- Co-authored-by: Florian Lefebvre <[email protected]>
- Loading branch information
1 parent
a1b324b
commit 0903ef9
Showing
18 changed files
with
287 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
"@astrojs/preact": minor | ||
--- | ||
|
||
Allows rendering lazy components. | ||
|
||
You can now use [lazy components](https://preactjs.com/guide/v10/switching-to-preact/#suspense-experimental) with Suspense: | ||
|
||
``` jsx | ||
import { lazy, Suspense } from 'preact/compat'; | ||
|
||
const HeavyComponent= lazy(() => import('./HeavyComponent')); | ||
|
||
const Component = () => { | ||
return ( | ||
<Suspense fallback={<p>Loading...</p>}> | ||
<HeavyComponent foo="bar" /> | ||
</Suspense> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,3 @@ | |
margin-top: 2em; | ||
place-items: center; | ||
} | ||
|
||
.counter-message { | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.message { | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import './Message.css'; | ||
|
||
export default function Message({ children }) { | ||
return <div class="message">{children}</div>; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/astro/e2e/fixtures/preact-lazy-component/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import preact from '@astrojs/preact'; | ||
import mdx from '@astrojs/mdx'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [preact(), mdx()], | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/astro/e2e/fixtures/preact-lazy-component/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@e2e/preact-lazy-component", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@astrojs/mdx": "workspace:*", | ||
"@astrojs/preact": "workspace:*", | ||
"astro": "workspace:*", | ||
"preact": "^10.15.1" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/astro/e2e/fixtures/preact-lazy-component/src/components/Counter.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.counter { | ||
display: grid; | ||
font-size: 2em; | ||
grid-template-columns: repeat(3, minmax(0, 1fr)); | ||
margin-top: 2em; | ||
place-items: center; | ||
} | ||
|
||
.counter-message { | ||
text-align: center; | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/astro/e2e/fixtures/preact-lazy-component/src/components/Counter.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { h, Fragment } from 'preact'; | ||
import { useState } from 'preact/hooks'; | ||
import { Suspense, lazy } from 'preact/compat'; | ||
import './Counter.css'; | ||
|
||
const LazyCounterMessage = lazy(() => import('./CounterMessage')) | ||
|
||
export default function Counter({ children, count: initialCount, id }) { | ||
const [count, setCount] = useState(initialCount); | ||
const add = () => setCount((i) => i + 1); | ||
const subtract = () => setCount((i) => i - 1); | ||
|
||
return ( | ||
<> | ||
<div id={id} className="counter"> | ||
<button className="decrement" onClick={subtract}>-</button> | ||
<pre>{count}</pre> | ||
<button className="increment" onClick={add}>+</button> | ||
</div> | ||
<Suspense fallback={<p>Load message...</p>}> | ||
<LazyCounterMessage className="counter-message">{children}</LazyCounterMessage> | ||
</Suspense> | ||
</> | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/astro/e2e/fixtures/preact-lazy-component/src/components/CounterMessage.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const CounterMessage = (props) => { | ||
return <div className={props.className}>{props.children}</div> | ||
} | ||
|
||
export default CounterMessage |
5 changes: 5 additions & 0 deletions
5
packages/astro/e2e/fixtures/preact-lazy-component/src/components/JSXComponent.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { h } from 'preact'; | ||
|
||
export default function({ id }) { | ||
return <div id={id}>Framework client:only component</div> | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/astro/e2e/fixtures/preact-lazy-component/src/components/Layout.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<html> | ||
<head><title>Preact component</title></head> | ||
<body><slot></slot></body> | ||
</html> |
37 changes: 37 additions & 0 deletions
37
packages/astro/e2e/fixtures/preact-lazy-component/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
import Counter from '../components/Counter.jsx'; | ||
import PreactComponent from '../components/JSXComponent.jsx'; | ||
const someProps = { | ||
count: 0, | ||
}; | ||
--- | ||
|
||
<html> | ||
<head> | ||
<!-- Head Stuff --> | ||
</head> | ||
<body> | ||
<Counter id="server-only" {...someProps}> | ||
<h1>Hello, server!</h1> | ||
</Counter> | ||
|
||
<Counter id="client-idle" {...someProps} client:idle> | ||
<h1>Hello, client:idle!</h1> | ||
</Counter> | ||
|
||
<Counter id="client-load" {...someProps} client:load> | ||
<h1>Hello, client:load!</h1> | ||
</Counter> | ||
|
||
<Counter id="client-visible" {...someProps} client:visible> | ||
<h1>Hello, client:visible!</h1> | ||
</Counter> | ||
|
||
<Counter id="client-media" {...someProps} client:media="(max-width: 50em)"> | ||
<h1>Hello, client:media!</h1> | ||
</Counter> | ||
|
||
<PreactComponent id="client-only" client:only="preact" /> | ||
</body> | ||
</html> |
29 changes: 29 additions & 0 deletions
29
packages/astro/e2e/fixtures/preact-lazy-component/src/pages/mdx.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export { default } from '../components/Layout.astro'; | ||
import Counter from '../components/Counter.jsx'; | ||
import PreactComponent from '../components/JSXComponent.jsx'; | ||
|
||
export const someProps = { | ||
count: 0, | ||
}; | ||
|
||
<Counter id="server-only" {...someProps}> | ||
# Hello, server! | ||
</Counter> | ||
|
||
<Counter id="client-idle" {...someProps} client:idle> | ||
# Hello, client:idle! | ||
</Counter> | ||
|
||
<Counter id="client-load" {...someProps} client:load> | ||
# Hello, client:load! | ||
</Counter> | ||
|
||
<Counter id="client-visible" {...someProps} client:visible> | ||
# Hello, client:visible! | ||
</Counter> | ||
|
||
<Counter id="client-media" {...someProps} client:media="(max-width: 50em)"> | ||
# Hello, client:media! | ||
</Counter> | ||
|
||
<PreactComponent id="client-only" client:only="preact" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { prepareTestFactory } from './shared-component-tests.js'; | ||
|
||
const { test, createTests } = prepareTestFactory({ root: './fixtures/preact-lazy-component/' }); | ||
|
||
const config = { | ||
counterComponentFilePath: './src/components/Counter.jsx', | ||
componentFilePath: './src/components/JSXComponent.jsx', | ||
}; | ||
|
||
test.describe('Preact lazy components in Astro files', () => { | ||
createTests({ | ||
...config, | ||
pageUrl: '/', | ||
pageSourceFilePath: './src/pages/index.astro', | ||
}); | ||
}); | ||
|
||
test.describe('Preact lazy components in MDX files', () => { | ||
createTests({ | ||
...config, | ||
pageUrl: '/mdx/', | ||
pageSourceFilePath: './src/pages/mdx.mdx', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.