-
-
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.
Add support for
client:only
hydrator (#935)
* Adding support for client:only hydration * Adding documentation for client:only * Adding changeset * Updating the test to use a browser-only API * Adding a browser-specific import script, this reproduces the issue where client:only imports must be removed * typo fix * removing mispelled test component * WIP: delaying inclusion of component imports until the hydration method is known * WIP: tweaking the test to use window instead of document * When only one renderer is included, use that for client:only hydration * temporary test script snuck into the last commit * WIP: adding check for a client:only renderer hint * refactor: Remove client:only components instead of delaying all component import statements * Updating the changeset and docs for the renderer hint * refactor: pull client:only render matching out to it's own function * Updating renderer hinting to match full name, with shorthand for internal renderers Co-authored-by: Tony Sullivan <tony.f.sullivan@gmail.com>
- Loading branch information
Tony Sullivan
and
Tony Sullivan
authored
Aug 17, 2021
1 parent
64bbd89
commit 1971ab3
Showing
17 changed files
with
274 additions
and
7 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,34 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Adds support for client:only hydrator | ||
|
||
The new `client:only` hydrator allows you to define a component that should be skipped during the build and only hydrated in the browser. | ||
|
||
In most cases it is best to render placeholder content during the build, but that may not always be feasible if an NPM dependency attempts to use browser APIs as soon as is imported. | ||
|
||
**Note** If more than one renderer is included in your Astro config, you need to include a hint to determine which renderer to use. Renderers will be matched to the name provided in your Astro config, similar to `<MyComponent client:only="@astrojs/renderer-react" />`. Shorthand can be used for `@astrojs` renderers, i.e. `<MyComponent client:only="react" />` will use `@astrojs/renderer-react`. | ||
|
||
An example usage: | ||
|
||
```jsx | ||
--- | ||
import BarChart from '../components/BarChart.jsx'; | ||
--- | ||
|
||
<BarChart client:only /> | ||
/** | ||
* If multiple renderers are included in the Astro config, | ||
* this will ensure that the component is hydrated with | ||
* the Preact renderer. | ||
*/ | ||
<BarChart client:only="preact" /> | ||
/** | ||
* If a custom renderer is required, use the same name | ||
* provided in the Astro config. | ||
*/ | ||
<BarChart client:only="my-custom-renderer" /> | ||
``` | ||
|
||
This allows you to import a chart component dependent on d3.js while making sure that the component isn't rendered at all at build time. |
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
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
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,14 @@ | ||
import type { GetHydrateCallback, HydrateOptions } from '../../@types/hydrate'; | ||
|
||
/** | ||
* Hydrate this component immediately | ||
*/ | ||
export default async function onLoad(astroId: string, _options: HydrateOptions, getHydrateCallback: GetHydrateCallback) { | ||
const roots = document.querySelectorAll(`astro-root[uid="${astroId}"]`); | ||
const innerHTML = roots[0].querySelector(`astro-fragment`)?.innerHTML ?? null; | ||
const hydrate = await getHydrateCallback(); | ||
|
||
for (const root of roots) { | ||
hydrate(root, innerHTML); | ||
} | ||
} |
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,44 @@ | ||
import { suite } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
import { setup, setupBuild } from './helpers.js'; | ||
|
||
const ClientOnlyComponents = suite('Client only components tests'); | ||
|
||
setup(ClientOnlyComponents, './fixtures/astro-client-only'); | ||
setupBuild(ClientOnlyComponents, './fixtures/astro-client-only'); | ||
|
||
ClientOnlyComponents('Loads pages using client:only hydrator', async ({ runtime }) => { | ||
let result = await runtime.load('/'); | ||
assert.ok(!result.error, `build error: ${result.error}`); | ||
|
||
let html = result.contents; | ||
|
||
const rootExp = /<astro-root\s[^>]*><\/astro-root>/; | ||
assert.ok(rootExp.exec(html), 'astro-root is empty'); | ||
|
||
// Grab the svelte import | ||
const exp = /import\("(.+?)"\)/g; | ||
let match, svelteRenderer; | ||
while ((match = exp.exec(result.contents))) { | ||
if (match[1].includes('renderers/renderer-svelte/client.js')) { | ||
svelteRenderer = match[1]; | ||
} | ||
} | ||
|
||
assert.ok(svelteRenderer, 'Svelte renderer is on the page'); | ||
|
||
result = await runtime.load(svelteRenderer); | ||
assert.equal(result.statusCode, 200, 'Can load svelte renderer'); | ||
}); | ||
|
||
ClientOnlyComponents('Can be built', async ({ build }) => { | ||
try { | ||
await build(); | ||
assert.ok(true, 'Can build a project with svelte dynamic components'); | ||
} catch (err) { | ||
console.log(err); | ||
assert.ok(false, 'build threw'); | ||
} | ||
}); | ||
|
||
ClientOnlyComponents.run(); |
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
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/astro-client-only/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 @@ | ||
export default { | ||
buildOptions: { | ||
sitemap: false, | ||
}, | ||
renderers: [ | ||
'@astrojs/renderer-svelte', | ||
], | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/astro-client-only/snowpack.config.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,3 @@ | ||
{ | ||
"workspaceRoot": "../../../../../" | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/astro/test/fixtures/astro-client-only/src/components/PersistentCounter.svelte
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,22 @@ | ||
|
||
<script> | ||
import './logResize'; | ||
let count = parseInt(localStorage.getItem('test:count')) || 0; | ||
$: localStorage.setItem('test:count', count); | ||
function add() { | ||
count += 1; | ||
} | ||
function subtract() { | ||
count -= 1; | ||
} | ||
</script> | ||
|
||
<div class="counter"> | ||
<button on:click={subtract}>-</button> | ||
<pre>{ count }</pre> | ||
<button on:click={add}>+</button> | ||
</div> | ||
|
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/astro-client-only/src/components/logResize.js
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 @@ | ||
window.addEventListener("resize", function() { | ||
console.log("window resized"); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/astro/test/fixtures/astro-client-only/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,9 @@ | ||
--- | ||
import PersistentCounter from '../components/PersistentCounter.svelte'; | ||
--- | ||
<html> | ||
<head><title>Client only pages</title></head> | ||
<body> | ||
<PersistentCounter client:only /> | ||
</body> | ||
</html> |
Oops, something went wrong.