Skip to content

Commit

Permalink
refactor(vue): use suspense (#40)
Browse files Browse the repository at this point in the history
* refactor(vue): use suspense

* chore: comment

* chore: pending state

* chore: comment

* chore: comment

* chore: comment
  • Loading branch information
hi-ogawa authored Apr 19, 2024
1 parent 6398cbf commit f2d4ab7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function submitEventToFormData(e: SubmitEvent) {
return formData;
}

// TODO: action return value on progressive enhancement?
export function useEnhance<T>(
action: FormAction<T>,
options?: {
Expand Down
17 changes: 13 additions & 4 deletions examples/vue-ssr-extra/src/root.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ onMounted(() => {
onUpdated(() => {
updated.value++;
});
const suspended = ref(false);
</script>

<template>
Expand All @@ -21,6 +23,7 @@ onUpdated(() => {
>
GitHub
</a>
<span v-if="suspended">...</span>
</div>
<div style="display: flex; align-items: center; gap: 0.5rem">
mounted: {{ mounted }}, updated: {{ mounted }}
Expand All @@ -34,13 +37,19 @@ onUpdated(() => {
<li>
<RouterLink to="/client">Counter (client)</RouterLink>
</li>
<li style="display: flex; gap: 0.5rem">
<RouterLink to="/server">Counter (server)</RouterLink>
<a href="/server?__nojs">(disable js)</a>
<li>
<span style="display: flex; gap: 0.5rem">
<RouterLink to="/server">Counter (server)</RouterLink>
<a href="/server?__nojs">(disable js)</a>
</span>
</li>
</ul>
</nav>
<main>
<RouterView />
<RouterView v-slot="{ Component }">
<Suspense @pending="suspended = true" @resolve="suspended = false">
<component :is="Component"></component>
</Suspense>
</RouterView>
</main>
</template>
13 changes: 2 additions & 11 deletions examples/vue-ssr-extra/src/routes/server/page.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
<script setup lang="ts">
import { onMounted, onServerPrefetch } from "vue";
import { useServerCounter } from "./_store";
import { Form, useEnhance } from "../../features/server-action/shared";
import { changeCounter, getCounter } from "./_action";
// TODO: revalidation
const store = useServerCounter();
// TODO: suspend?
onServerPrefetch(async () => {
store.data = await getCounter();
});
onMounted(async () => {
// TODO: refetch on stale?
store.data ??= await getCounter();
});
store.data ??= await getCounter();
const [formAction, { status }] = useEnhance(changeCounter, {
onSuccess(data) {
Expand Down

0 comments on commit f2d4ab7

Please sign in to comment.