Skip to content

Commit

Permalink
perf: improve index and [slug] page prefetching (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanweDE authored Jun 10, 2024
2 parents d6fad75 + 7cb8669 commit 130efbf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
28 changes: 15 additions & 13 deletions src/pages/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ export const getServerSideProps = async ({ locale, params, query }: CustomNextPa
const queryClient = new QueryClient();

// Default queries
await queryClient.prefetchQuery(
useCtfPageQuery.getKey({ slug, locale, preview }),
useCtfPageQuery.fetcher({ slug, locale, preview }),
);
await queryClient.prefetchQuery(
useCtfNavigationQuery.getKey({ locale, preview }),
useCtfNavigationQuery.fetcher({ locale, preview }),
);
await queryClient.prefetchQuery(
useCtfFooterQuery.getKey({ locale, preview }),
useCtfFooterQuery.fetcher({ locale, preview }),
);

const prefetchPromises = [
queryClient.prefetchQuery(
useCtfPageQuery.getKey({ slug, locale, preview }),
useCtfPageQuery.fetcher({ slug, locale, preview }),
),
queryClient.prefetchQuery(
useCtfNavigationQuery.getKey({ locale, preview }),
useCtfNavigationQuery.fetcher({ locale, preview }),
),
queryClient.prefetchQuery(
useCtfFooterQuery.getKey({ locale, preview }),
useCtfFooterQuery.fetcher({ locale, preview }),
),
];
// Dynamic queries
const pageData = await useCtfPageQuery.fetcher({ slug, locale, preview })();
const page = pageData.pageCollection?.items[0];
Expand All @@ -55,6 +56,7 @@ export const getServerSideProps = async ({ locale, params, query }: CustomNextPa
const content: ComponentReferenceFieldsFragment | undefined | null = page?.pageContent;

await Promise.all([
...prefetchPromises,
...prefetchPromiseArr({ inputArr: topSection, locale, queryClient }),
...prefetchPromiseArr({ inputArr: extraSection, locale, queryClient }),
...prefetchPromiseArr({ inputArr: [content], locale, queryClient }),
Expand Down
30 changes: 17 additions & 13 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ const LangPage: NextPage = () => {
};

export const getServerSideProps = async ({ locale, query }: NextPageContext) => {
const preview = Boolean(query.preview);

try {
const preview = Boolean(query.preview);
const queryClient = new QueryClient();

// Default queries
await queryClient.prefetchQuery(
useCtfPageQuery.getKey({ slug: 'home', locale, preview }),
useCtfPageQuery.fetcher({ slug: 'home', locale, preview }),
);
await queryClient.prefetchQuery(
useCtfNavigationQuery.getKey({ locale, preview }),
useCtfNavigationQuery.fetcher({ locale, preview }),
);
await queryClient.prefetchQuery(
useCtfFooterQuery.getKey({ locale, preview }),
useCtfFooterQuery.fetcher({ locale, preview }),
);
const prefetchPromises = [
queryClient.prefetchQuery(
useCtfPageQuery.getKey({ slug: 'home', locale, preview }),
useCtfPageQuery.fetcher({ slug: 'home', locale, preview }),
),
queryClient.prefetchQuery(
useCtfNavigationQuery.getKey({ locale, preview }),
useCtfNavigationQuery.fetcher({ locale, preview }),
),
queryClient.prefetchQuery(
useCtfFooterQuery.getKey({ locale, preview }),
useCtfFooterQuery.fetcher({ locale, preview }),
),
];

// Dynamic queries
const pageData = await useCtfPageQuery.fetcher({ slug: 'home', locale, preview })();
Expand All @@ -40,6 +43,7 @@ export const getServerSideProps = async ({ locale, query }: NextPageContext) =>
const extraSection = page?.extraSectionCollection?.items;

await Promise.all([
...prefetchPromises,
...prefetchPromiseArr({ inputArr: topSection, locale, queryClient }),
...prefetchPromiseArr({ inputArr: extraSection, locale, queryClient }),
...prefetchPromiseArr({ inputArr: [content], locale, queryClient }),
Expand Down

0 comments on commit 130efbf

Please sign in to comment.