mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
Fix vue resetting state when using view transition persistence (#11946)
* Fix vue resetting state when using view transition persistence * Avoid calling internal apis when forcing vue component update
This commit is contained in:
parent
a2f8c5d85f
commit
b75bfc8cc4
6 changed files with 87 additions and 18 deletions
5
.changeset/five-walls-build.md
Normal file
5
.changeset/five-walls-build.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/vue': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix vue islands keeping their state when using view transition persistence
|
|
@ -1,8 +1,8 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="counter">
|
<div class="counter">
|
||||||
<button @click="subtract()">-</button>
|
<button @click="subtract()" class="decrement">-</button>
|
||||||
<pre>{{ count }}</pre>
|
<pre>{{prefix}}{{ count }}</pre>
|
||||||
<button @click="add()">+</button>
|
<button @click="add()" class="increment">+</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="counter-message">
|
<div class="counter-message">
|
||||||
<slot />
|
<slot />
|
||||||
|
@ -12,6 +12,12 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue';
|
||||||
export default {
|
export default {
|
||||||
|
props: {
|
||||||
|
prefix: {
|
||||||
|
type: String,
|
||||||
|
default: '',
|
||||||
|
},
|
||||||
|
},
|
||||||
setup() {
|
setup() {
|
||||||
const count = ref(0);
|
const count = ref(0);
|
||||||
const add = () => (count.value = count.value + 1);
|
const add = () => (count.value = count.value + 1);
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
import Layout from '../components/Layout.astro';
|
||||||
|
import Counter from '../components/VueCounter.vue';
|
||||||
|
export const prerender = false;
|
||||||
|
|
||||||
|
---
|
||||||
|
<Layout>
|
||||||
|
<p id="island-one">Page 1</p>
|
||||||
|
<a id="click-two" href="/island-vue-two">go to 2</a>
|
||||||
|
<Counter prefix="AA" client:load transition:persist transition:name="counter" />
|
||||||
|
</Layout>
|
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
import Layout from '../components/Layout.astro';
|
||||||
|
import Counter from '../components/VueCounter.vue';
|
||||||
|
export const prerender = false;
|
||||||
|
|
||||||
|
---
|
||||||
|
<Layout>
|
||||||
|
<p id="island-two">Page 2</p>
|
||||||
|
<a id="click-two" href="/island-vue-one">go to 1</a>
|
||||||
|
<Counter prefix="BB" client:load transition:persist transition:name="counter" />
|
||||||
|
</Layout>
|
|
@ -522,7 +522,7 @@ test.describe('View Transitions', () => {
|
||||||
expect(secondTime).toBeGreaterThanOrEqual(firstTime);
|
expect(secondTime).toBeGreaterThanOrEqual(firstTime);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Islands can persist using transition:persist', async ({ page, astro }) => {
|
test('React Islands can persist using transition:persist', async ({ page, astro }) => {
|
||||||
// Go to page 1
|
// Go to page 1
|
||||||
await page.goto(astro.resolveUrl('/island-one'));
|
await page.goto(astro.resolveUrl('/island-one'));
|
||||||
let cnt = page.locator('.counter pre');
|
let cnt = page.locator('.counter pre');
|
||||||
|
@ -544,6 +544,24 @@ test.describe('View Transitions', () => {
|
||||||
await expect(pageTitle).toHaveText('Island 2');
|
await expect(pageTitle).toHaveText('Island 2');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Vue Islands can persist using transition:persist', async ({ page, astro }) => {
|
||||||
|
// Go to page 1
|
||||||
|
await page.goto(astro.resolveUrl('/island-vue-one'));
|
||||||
|
let cnt = page.locator('.counter pre');
|
||||||
|
await expect(cnt).toHaveText('AA0');
|
||||||
|
|
||||||
|
await page.click('.increment');
|
||||||
|
await expect(cnt).toHaveText('AA1');
|
||||||
|
|
||||||
|
// Navigate to page 2
|
||||||
|
await page.click('#click-two');
|
||||||
|
const 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('BB1');
|
||||||
|
});
|
||||||
|
|
||||||
test('transition:persist-props prevents props from changing', async ({ page, astro }) => {
|
test('transition:persist-props prevents props from changing', async ({ page, astro }) => {
|
||||||
// Go to page 1
|
// Go to page 1
|
||||||
await page.goto(astro.resolveUrl('/island-one?persist'));
|
await page.goto(astro.resolveUrl('/island-one?persist'));
|
||||||
|
|
|
@ -2,6 +2,9 @@ import { setup } from 'virtual:@astrojs/vue/app';
|
||||||
import { Suspense, createApp, createSSRApp, h } from 'vue';
|
import { Suspense, createApp, createSSRApp, h } from 'vue';
|
||||||
import StaticHtml from './static-html.js';
|
import StaticHtml from './static-html.js';
|
||||||
|
|
||||||
|
// keep track of already initialized apps, so we don't hydrate again for view transitions
|
||||||
|
let appMap = new WeakMap();
|
||||||
|
|
||||||
export default (element) =>
|
export default (element) =>
|
||||||
async (Component, props, slotted, { client }) => {
|
async (Component, props, slotted, { client }) => {
|
||||||
if (!element.hasAttribute('ssr')) return;
|
if (!element.hasAttribute('ssr')) return;
|
||||||
|
@ -15,10 +18,20 @@ export default (element) =>
|
||||||
|
|
||||||
const isHydrate = client !== 'only';
|
const isHydrate = client !== 'only';
|
||||||
const bootstrap = isHydrate ? createSSRApp : createApp;
|
const bootstrap = isHydrate ? createSSRApp : createApp;
|
||||||
|
|
||||||
|
// keep a reference to the app, props and slots so we can update a running instance later
|
||||||
|
let appInstance = appMap.get(element);
|
||||||
|
|
||||||
|
if (!appInstance) {
|
||||||
|
appInstance = {
|
||||||
|
props,
|
||||||
|
slots,
|
||||||
|
};
|
||||||
const app = bootstrap({
|
const app = bootstrap({
|
||||||
name,
|
name,
|
||||||
render() {
|
render() {
|
||||||
let content = h(Component, props, slots);
|
let content = h(Component, appInstance.props, appInstance.slots);
|
||||||
|
appInstance.component = this;
|
||||||
// related to https://github.com/withastro/astro/issues/6549
|
// related to https://github.com/withastro/astro/issues/6549
|
||||||
// if the component is async, wrap it in a Suspense component
|
// if the component is async, wrap it in a Suspense component
|
||||||
if (isAsync(Component.setup)) {
|
if (isAsync(Component.setup)) {
|
||||||
|
@ -29,7 +42,12 @@ export default (element) =>
|
||||||
});
|
});
|
||||||
await setup(app);
|
await setup(app);
|
||||||
app.mount(element, isHydrate);
|
app.mount(element, isHydrate);
|
||||||
|
appMap.set(element, appInstance);
|
||||||
|
} else {
|
||||||
|
appInstance.props = props;
|
||||||
|
appInstance.slots = slots;
|
||||||
|
appInstance.component.$forceUpdate();
|
||||||
|
}
|
||||||
element.addEventListener('astro:unmount', () => app.unmount(), { once: true });
|
element.addEventListener('astro:unmount', () => app.unmount(), { once: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue