Nuxt 3 Architecture Question: Separating Logic from Components
Hi everyone ,
I’m in the process of refactoring my Nuxt 3 app and wanted to get some feedback on best practices. Right now, a lot of my components directly handle data fetching and business logic, but I’d like to move those responsibilities into composables for better testability and reusability.
The idea is to keep components as “dumb” UI pieces and move logic elsewhere. For example:
import { computed } from 'vue'export function useUsers() { const { data: users, pending, error, refresh } = useFetch('/api/users') // Example computed property const activeUsers = computed(() => (users.value ?? []).filter(u => u.isActive) ) // Example function function findUserById(id: number) { return (users.value ?? []).find(u => u.id === id) } return { users, pending, error, refresh, activeUsers, findUserById }}
import { computed } from 'vue'export function useUsers() { const { data: users, pending, error, refresh } = useFetch('/api/users') // Example computed property const activeUsers = computed(() => (users.value ?? []).filter(u => u.isActive) ) // Example function function findUserById(id: number) { return (users.value ?? []).find(u => u.id === id) } return { users, pending, error, refresh, activeUsers, findUserById }}
This way I can test my data-fetching + derived state + helper functions in isolation without rendering the full component tree.
My questions for the community:
- Is moving fetching + business logic into composables a sound approach in Nuxt 3? - Are there pitfalls I should be aware of (SSR concerns, test complexity, etc.)? - Do you follow a different pattern for separating logic from UI?
Appreciate any insights or examples of how you structure this in larger Nuxt apps
Similar Threads
Continue the conversation
Join the Discord to ask follow-up questions and connect with the community