freddy38510 / quasar-app-extension-ssg

Static Site Generator App Extension for Quasar.
MIT License
155 stars 16 forks source link

Empty data on all pages except one, when routes uses same component #383

Open vsqla opened 10 months ago

vsqla commented 10 months ago

routes:

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    component: () => import('layouts/MainLayout.vue'),
    children: [
      {
        path: '',
        component: () => import('pages/DynamicPage.vue'),
        props: () => ({ path: 'main' }),
      },
      {
        path: ':path?',
        component: () => import('pages/DynamicPage.vue'),
        props: (route) => ({ path: route?.params?.path ?? 'main' }),
      },
    ],
  },

MainLayout:

defineOptions({
  async preFetch() {
    const store = usePagesStore();
    await store.getPages();
    return store.pages.length > 0;
  },
});

Store:

export const usePagesStore = defineStore('pages', () => {
  const { resolveClient } = useApolloClient();
  const client = resolveClient();
  const pages = ref([]);

  const getPages = async () => {
    const res = await client.query({ query: getAllPages });
    console.log('GET PAGES!', res.data.pages.data.length);
    pages.value = res.data?.pages?.data ?? pages.value;
  };
  return {
    getPages,
    pages,
  };
});

quasar.conf:

prefetch: true,
ssg: {
      routes: async () => {
        const res = await axios.get(`${process.env.API_URI}/pages`);
        return res.data.data.map(({ attributes }) => attributes.path ?? '');
      },
      shouldPrefetch: () => true,
      shouldPreload: () => true,
      // crawler: true,
    },

Excepted: all generated pages have initial data; Result: only one of the generated pages have non empty __INITIAL_STATE__

vsqla commented 10 months ago

so, how to force prefetch to work on every page?