Skip to content

Commit

Permalink
fix(loaders): avoid double fetch on nested loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 30, 2024
1 parent 683a798 commit f15bedc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@
"build": "pnpm run build:core && pnpm run build:runtime",
"build:core": "tsup",
"build:runtime": "tsup --config tsup-runtime.config.ts",
"dev": "pnpm run test",
"dev": "pnpm run test --ui",
"docs": "vitepress dev docs",
"docs:build": "vitepress build docs",
"lint": "prettier -c '{src,examples,playground}/**/*.{ts,vue}'",
"play": "npm -C playground run dev",
"play:build": "npm -C playground run build",
"release": "node scripts/release.mjs",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
"test": "vitest --typecheck --ui"
"test": "vitest --typecheck"
},
"gitHooks": {
"pre-commit": "lint-staged",
Expand Down
8 changes: 4 additions & 4 deletions src/data-loaders/defineColadaLoader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getRouter } from 'vue-router-mock'
import { enableAutoUnmount, mount } from '@vue/test-utils'
import RouterViewMock from '../../tests/data-loaders/RouterViewMock.vue'
import { setActivePinia, createPinia, getActivePinia } from 'pinia'
import { QueryPlugin, useQuery } from '@pinia/colada'
import { useQuery, PiniaColada } from '@pinia/colada'
import { RouteLocationNormalizedLoaded } from 'vue-router'

describe(
Expand Down Expand Up @@ -55,7 +55,7 @@ describe(
setActivePinia(pinia)
return { pinia }
},
plugins: ({ pinia }) => [pinia, QueryPlugin],
plugins: ({ pinia }) => [pinia, PiniaColada],
}
)

Expand Down Expand Up @@ -95,7 +95,7 @@ describe(
plugins: [
[DataLoaderPlugin, { router, ...pluginOptions }],
createPinia(),
QueryPlugin,
PiniaColada,
],
},
})
Expand Down Expand Up @@ -168,7 +168,7 @@ describe(
}),
{
global: {
plugins: [getActivePinia()!, QueryPlugin],
plugins: [getActivePinia()!, PiniaColada],
},
}
)
Expand Down
5 changes: 5 additions & 0 deletions src/data-loaders/defineColadaLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ export function defineColadaLoader<Data, isLazy extends boolean>(
// entries.delete(loader)
// }
})
// avoid double reload since calling `useQuery()` will trigger a refresh
// and we might also do it below for nested loaders
if (entry.ext.status.value === 'loading') {
reload = false
}
}

const { isLoading, data, error, ext } = entry
Expand Down
2 changes: 2 additions & 0 deletions tests/data-loaders/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,11 @@ export function testDefineLoader<Context = void>(
it('can nest loaders', async () => {
const spyOne = vi
.fn<(...args: unknown[]) => Promise<string>>()
.mockResolvedValue('ko')
.mockResolvedValueOnce('one')
const spyTwo = vi
.fn<(...args: unknown[]) => Promise<string>>()
.mockResolvedValue('ko')
.mockResolvedValueOnce('two')
const useLoaderOne = loaderFactory({ fn: spyOne, key: 'one' })
const useLoaderTwo = loaderFactory({
Expand Down

0 comments on commit f15bedc

Please sign in to comment.