Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Vue HMR for script tags #8860

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-brooms-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vue': patch
---

Fix Vue component HMR when updating the script tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script setup>
import { ref } from 'vue'
const props = defineProps(['id'])
const count = ref(1)
</script>

<template>
<span :id="props.id">Count is {{ count }}</span>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Counter from '../components/Counter.vue';
import VueComponent from '../components/VueComponent.vue';
import AsyncTest from '../components/Test.vue'
import State from '../components/State.vue'

const someProps = {
count: 0,
Expand Down Expand Up @@ -35,5 +36,6 @@ const someProps = {

<VueComponent id="client-only" client:only="vue" />
<AsyncTest id="client-test" client:only="vue" />
<State id="state" client:load />
</body>
</html>
13 changes: 13 additions & 0 deletions packages/astro/e2e/vue-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ test('test the async vue component in mdx', async ({ page, astro }) => {

await expect(label, 'component not hydrated').toHaveText('2');
});

test('hmr works', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));

const span = page.locator('#state');
await expect(span).toHaveText('Count is 1');

await astro.editFile('./src/components/State.vue', (content) =>
content.replace('ref(1)', 'ref(2)')
);

await expect(span).toHaveText('Count is 2');
});
22 changes: 13 additions & 9 deletions packages/integrations/vue/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ export default (element) =>
slots[key] = () => h(StaticHtml, { value, name: key === 'default' ? undefined : key });
}

let content = h(Component, props, slots);
// related to https://github.com/withastro/astro/issues/6549
// if the component is async, wrap it in a Suspense component
if (isAsync(Component.setup)) {
content = h(Suspense, null, content);
}

const isHydrate = client !== 'only';
const boostrap = isHydrate ? createSSRApp : createApp;
const app = boostrap({ name, render: () => content });
const bootstrap = isHydrate ? createSSRApp : createApp;
const app = bootstrap({
name,
render() {
let content = h(Component, props, slots);
// related to https://github.com/withastro/astro/issues/6549
// if the component is async, wrap it in a Suspense component
if (isAsync(Component.setup)) {
content = h(Suspense, null, content);
}
return content;
},
});
await setup(app);
app.mount(element, isHydrate);

Expand Down
Loading