pinia-colada: Nuxt unplugin-vue-router. With frequent requests, there is no data in the state. Abort signal not working when lazy true

When quickly navigating to the next pagination page, when 2 or more data requests are executed in parallel, the data disappears from state.value.data and placeholderData . However, it is present in data.value. Because of this I have to use data.value.

I expect that data should not be lost.

- Operating System: Linux
- Node Version:     v22.12.0
- Nuxt Version:     3.15.4
- CLI Version:      3.22.2
- Nitro Version:    2.10.4
- Package Manager:  pnpm@9.15.4
- Builder:          -
- User Config:      ssr, future, experimental, modules, compatibilityDate, devtools, css, app, components, hooks
- Runtime Modules:  @nuxt/eslint@1.1.0, @nuxt/icon@1.10.3, @pinia/nuxt@0.10.1, @pinia/colada-nuxt@0.0.5
- Build Modules:    -

Lazt = true (Abort signal not working)

import { defineColadaLoader } from "unplugin-vue-router/data-loaders/pinia-colada";

export const useCarListData = defineColadaLoader("moulages-cars-accepted", {
  key(route) {
    const inputs = route.query.input ?? "";
    const input = (Array.isArray(inputs) ? inputs[0] : inputs) ?? "";
    return ["cars", input];
  },
  query(route, ctx) {
    return $fetch(`/api/cars`, {
      signal: ctx.signal,
    });
  },
  placeholderData(data) {
    if (!data) console.error("Empty: placeholderData");
    return data;
  },
  lazy: (to, from) => !!(from && to.name === from.name),
});

<script setup lang="ts">
import { useCarListData } from "./model";

definePageMeta({
  layout: "moulages",
  loaders: [useCarListData],
});

const { data, state } = useCarListData();

watchSyncEffect(() => {
  if (!data.value) console.error("Empty: data", data.value);
  if (!state.value.data) console.error("Empty: state.data", state.value.data, state.value.status);
});

const inputQuery = useInputQuery();
const page = computed(() => inputQuery.input?.page ?? 1);
</script>

<template>
  <PageMain>
    <PageHeader>
      <template #title>
        <PageHeaderTitle> Page {{ page }} </PageHeaderTitle>
      </template>
    </PageHeader>
    <PagePagination>
      <CarCardList>
        <CarCard v-for="car in data?.data ?? []" :key="car.id" :car="car">
          <template #actions>
            <ReturnCar variant="neutral" :id="car.id" />
            <RejectCar :id="car.id" />
          </template>
        </CarCard>
      </CarCardList>

      <template #footer>
        <Pagination :pagination="data?.meta" />
      </template>
    </PagePagination>
  </PageMain>
</template>

https://github.com/user-attachments/assets/7fedf9c1-f528-45fb-9275-40f8409a624b

Lazy = false + abort signal

import { defineColadaLoader } from "unplugin-vue-router/data-loaders/pinia-colada";

export const useCarListData = defineColadaLoader("moulages-cars-accepted", {
  key(route) {
    const inputs = route.query.input ?? "";
    const input = (Array.isArray(inputs) ? inputs[0] : inputs) ?? "";
    return ["cars", input];
  },
  query(route, ctx) {
    return $fetch(`/api/cars`, {
      signal: ctx.signal,
    });
  },
  placeholderData(data) {
    if (!data) console.error("Empty: placeholderData");
    return data;
  },
  lazy: false
  // lazy: (to, from) => !!(from && to.name === from.name),
});

https://github.com/user-attachments/assets/fff7f7bf-7c65-4e4e-bd70-6771557d38d5

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Instead of calling cancelQueries, you can create a plugin that matches your specific needs:

export function PiniaColadaCancelInactive(): PiniaColadaPlugin {
  return ({ queryCache }) => {
    queryCache.$onAction(({ name, after, args }) => {
      if (name === 'untrack') {
        const [entry] = args
        after(() => {
          if (!entry.active) {
            queryCache.cancel(entry)
          }
        })
      }
    })
  }
}

Check the source code of the plugins/delay (or others) to extend this even further.

You can implement all the things you want already but I wouldn’t recommend to use this for the reasons mentioned above. Note that the goal is not to be like tanstack query. The goal is to ease migration from it. Changes in default behavior that are beneficial like this one are simply bonus.

To summarize the reasons so far:

  • In pinia colada, signals are aborted when we need to discard the result. e.g. a mutation invalidated the query, a new query was triggered with the same key. Aborting the signal in other cases not only is useless for your app but also wasteful because the result is discarded even though it’s valid.
  • In loaders, the signal is different which makes me think I should expose both signals posva/unplugin-vue-router#605
    • The navigation signal in aborts if the navigation is canceled or aborted. If a loader is lazy, it doesn’t block the navigation so the navigation is already finished by the time you change pages, and the navigation isn’t canceled