0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Fix SolidJS view transition state persistence (#11998)

* Fix SolidJS view transition state persistence

* Add changeset

---------

Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com>
This commit is contained in:
Johannes Spohr 2024-09-17 15:21:41 +02:00 committed by GitHub
parent b75bfc8cc4
commit 082f450944
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 127 additions and 28 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/solid-js': patch
---
Fix view transition state persistence

View file

@ -1,6 +1,7 @@
import nodejs from '@astrojs/node';
import react from '@astrojs/react';
import svelte from '@astrojs/svelte';
import solidjs from '@astrojs/solid-js';
import vue from '@astrojs/vue';
import { defineConfig } from 'astro/config';
@ -8,7 +9,11 @@ import { defineConfig } from 'astro/config';
export default defineConfig({
output: 'hybrid',
adapter: nodejs({ mode: 'standalone' }),
integrations: [react(),vue(),svelte()],
integrations: [react( {
exclude: ['**/solid/**'],
}),vue(),svelte(),solidjs({
include: ['**/solid/**'],
})],
redirects: {
'/redirect-two': '/two',
'/redirect-external': 'http://example.com/',

View file

@ -7,10 +7,12 @@
"@astrojs/react": "workspace:*",
"@astrojs/svelte": "workspace:*",
"@astrojs/vue": "workspace:*",
"@astrojs/solid-js": "workspace:*",
"astro": "workspace:*",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"svelte": "^4.2.19",
"vue": "^3.5.3"
"vue": "^3.5.3",
"solid-js": "^1.8.0"
}
}

View file

@ -0,0 +1,19 @@
import {createSignal} from "solid-js";
export default function Counter(props) {
const [count, setCount] = createSignal(0);
const add = () => setCount(count() + 1);
const subtract = () => setCount(count() - 1);
return (
<>
<div class="counter">
<button onClick={subtract} class="decrement">-</button>
<pre>{props.prefix ?? ''}{count()}{props.postfix ?? ""}</pre>
<button onClick={add} class="increment">+</button>
</div>
<div class="counter-message">{props.children}</div>
</>
);
}

View file

@ -0,0 +1,11 @@
---
import Layout from '../components/Layout.astro';
import Counter from '../components/solid/Counter.jsx';
export const prerender = false;
---
<Layout>
<p id="island-one">Page 1</p>
<a id="click-two" href="/island-solid-two">go to 2</a>
<Counter prefix="A" client:load transition:persist transition:name="counter" />
</Layout>

View file

@ -0,0 +1,11 @@
---
import Layout from '../components/Layout.astro';
import Counter from '../components/solid/Counter.jsx';
export const prerender = false;
---
<Layout>
<p id="island-two">Page 2</p>
<a id="click-one" href="/island-solid-one">go to 1</a>
<Counter prefix="B" postfix="!" client:load transition:persist transition:name="counter" />
</Layout>

View file

@ -544,6 +544,31 @@ test.describe('View Transitions', () => {
await expect(pageTitle).toHaveText('Island 2');
});
test('Solid Islands can persist using transition:persist', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/island-solid-one'));
let cnt = page.locator('.counter pre');
await expect(cnt).toHaveText('A0');
await page.click('.increment');
await expect(cnt).toHaveText('A1');
// Navigate to page 2
await page.click('#click-two');
let p = page.locator('#island-two');
await expect(p).toBeVisible();
cnt = page.locator('.counter pre');
// Count should remain, but the prefix should be updated
await expect(cnt).toHaveText('B1!');
await page.click('#click-one');
p = page.locator('#island-one');
await expect(p).toBeVisible();
cnt = page.locator('.counter pre');
// Count should remain, but the postfix should be removed again (to test unsetting props)
await expect(cnt).toHaveText('A1');
});
test('Vue Islands can persist using transition:persist', async ({ page, astro }) => {
// Go to page 1
await page.goto(astro.resolveUrl('/island-vue-one'));

View file

@ -1,10 +1,12 @@
import { Suspense } from 'solid-js';
import { createStore, reconcile } from 'solid-js/store';
import { createComponent, hydrate, render } from 'solid-js/web';
const alreadyInitializedElements = new WeakMap<Element, any>();
export default (element: HTMLElement) =>
(Component: any, props: any, slotted: any, { client }: { client: string }) => {
if (!element.hasAttribute('ssr')) return;
const isHydrate = client !== 'only';
const bootstrap = isHydrate ? hydrate : render;
@ -32,31 +34,44 @@ export default (element: HTMLElement) =>
const { default: children, ...slots } = _slots;
const renderId = element.dataset.solidRenderId;
if (alreadyInitializedElements.has(element)) {
// update the mounted component
alreadyInitializedElements.get(element)!(
// reconcile will make sure to apply as little updates as possible, and also remove missing values w/o breaking reactivity
reconcile({
...props,
...slots,
children,
}),
);
} else {
const [store, setStore] = createStore({
...props,
...slots,
children,
});
// store the function to update the current mounted component
alreadyInitializedElements.set(element, setStore);
const dispose = bootstrap(
() => {
const inner = () =>
createComponent(Component, {
...props,
...slots,
children,
});
const dispose = bootstrap(
() => {
const inner = () => createComponent(Component, store);
if (isHydrate) {
return createComponent(Suspense, {
get children() {
return inner();
},
});
} else {
return inner();
}
},
element,
{
renderId,
},
);
element.addEventListener('astro:unmount', () => dispose(), { once: true });
if (isHydrate) {
return createComponent(Suspense, {
get children() {
return inner();
},
});
} else {
return inner();
}
},
element,
{
renderId,
},
);
element.addEventListener('astro:unmount', () => dispose(), { once: true });
}
};

View file

@ -1721,6 +1721,9 @@ importers:
'@astrojs/react':
specifier: workspace:*
version: link:../../../../integrations/react
'@astrojs/solid-js':
specifier: workspace:*
version: link:../../../../integrations/solid
'@astrojs/svelte':
specifier: workspace:*
version: link:../../../../integrations/svelte
@ -1736,6 +1739,9 @@ importers:
react-dom:
specifier: ^18.3.1
version: 18.3.1(react@18.3.1)
solid-js:
specifier: ^1.8.0
version: 1.8.22
svelte:
specifier: ^4.2.19
version: 4.2.19