Why router is not passed to preFetch on SSR mode?
#12905
-
|
I'm using the following plugin import { useRouteQuery } from '@vueuse/router'
const search = useRouteQuery('search')
const search = useRouteQuery('search', 'foo') // or with a default valueWhich keeps the URL query parameters in sync with preFetch ({ store, currentRoute, previousRoute, redirect, ssrContext, urlPath, publicPath }) {
// missing here `router`
},Which brings the problem that we are unable to do something like: preFetch ({ store, currentRoute, previousRoute, redirect, ssrContext, urlPath, publicPath }) {
const page = useRouteQuery('page', '1');
},Because For now my solution is to simply pass the preFetch ({ store, currentRoute, previousRoute, redirect, ssrContext, urlPath, publicPath }) {
const mockRouter = { push: () => {} };
const page = useRouteQuery('page', '1', { router: mockRouter, route: currentRoute });
},It seems a bit off having to pass a mock for a library to be able to work. QuestionWhy isn't |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Worth to say that this is a silly example because why would you want to maintain reactivity of URL parameters in export function useMeetupFilters() {
const page = useRouteQuery('page', '1');
const city = ref(loggedUser.city.id);
const parameters = computed(() => ({
page: page.value,
city: city.value,
}));
return { parameters, page, city };
}
// in my component
preFetch ({ store, currentRoute, previousRoute, redirect, ssrContext, urlPath, publicPath }) {
const mockRouter = { push: () => {} };
const { parameters } = useMeetupFilters();
store.fetchMeetups(parameters.value);
},
setup() {
const mockRouter = { push: () => {} };
const { parameters } = useMeetupFilters();
const onFilterChanges = () => {
store.fetchMeetups(parameters.value);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Composables are meant to be used inside of setup(). That's the textbook definition of them. What you are doing is trying to use them outside of setup() so that's why it's no wonder it doesn't works as you expect. It's like trying to use a composable in a plain js file. The "router" is not passed to Quasar CLI's preFetch hook because: 1) it doesn't makes any sense to be using it there and (more aggravating) 2) it allows devs to make mistakes like assuming they can redirect to another page using it -- which would break the boot procedure from every angle. So there's "currentRoute", "previousRoute" and "redirect" method there. |
Beta Was this translation helpful? Give feedback.
Composables are meant to be used inside of setup(). That's the textbook definition of them. What you are doing is trying to use them outside of setup() so that's why it's no wonder it doesn't works as you expect. It's like trying to use a composable in a plain js file.
The "router" is not passed to Quasar CLI's preFetch hook because: 1) it doesn't makes any sense to be using it there and (more aggravating) 2) it allows devs to make mistakes like assuming they can redirect to another page using it -- which would break the boot procedure from every angle. So there's "currentRoute", "previousRoute" and "redirect" method there.