allow requests in getStaticProps to execute in parallel

This commit is contained in:
Tobias Koppers 2021-06-14 19:41:18 +02:00
parent d23da23dc0
commit f8fca2dd4d
9 changed files with 43 additions and 25 deletions

View File

@ -35,8 +35,8 @@ export default function getSiteInfoOperation({
} = {}): Promise<T['data']> { } = {}): Promise<T['data']> {
const cfg = commerce.getConfig(config) const cfg = commerce.getConfig(config)
const categories = await getCategories(cfg) const categoriesPromise = getCategories(cfg)
const brands = await getBrands(cfg) const brandsPromise = getBrands(cfg)
/* /*
const { fetch, locale } = cfg const { fetch, locale } = cfg
const { data } = await fetch<GetSiteInfoQuery, GetSiteInfoQueryVariables>( const { data } = await fetch<GetSiteInfoQuery, GetSiteInfoQueryVariables>(
@ -53,8 +53,8 @@ export default function getSiteInfoOperation({
*/ */
return { return {
categories, categories: await categoriesPromise,
brands, brands: await brandsPromise,
} }
} }

View File

@ -19,8 +19,10 @@ const getSiteInfo = async (options?: {
config = getConfig(config) config = getConfig(config)
const categories = await getCategories(config) const categoriesPromise = getCategories(config)
const brands = await getVendors(config) const brandsPromise = getVendors(config)
const categories = await categoriesPromise
const brands = await brandsPromise
return { return {
categories, categories,

View File

@ -16,8 +16,10 @@ export async function getStaticProps({
locales, locales,
}: GetStaticPropsContext<{ pages: string[] }>) { }: GetStaticPropsContext<{ pages: string[] }>) {
const config = { locale, locales } const config = { locale, locales }
const { pages } = await commerce.getAllPages({ config, preview }) const pagesPromise = commerce.getAllPages({ config, preview })
const { categories } = await commerce.getSiteInfo({ config, preview }) const siteInfoPromise = commerce.getSiteInfo({ config, preview })
const { pages } = await pagesPromise
const { categories } = await siteInfoPromise
const path = params?.pages.join('/') const path = params?.pages.join('/')
const slug = locale ? `${locale}/${path}` : path const slug = locale ? `${locale}/${path}` : path
const pageItem = pages.find((p) => (p.url ? getSlug(p.url) === slug : false)) const pageItem = pages.find((p) => (p.url ? getSlug(p.url) === slug : false))

View File

@ -13,8 +13,10 @@ export async function getStaticProps({
locales, locales,
}: GetStaticPropsContext) { }: GetStaticPropsContext) {
const config = { locale, locales } const config = { locale, locales }
const { pages } = await commerce.getAllPages({ config, preview }) const pagesPromise = commerce.getAllPages({ config, preview })
const { categories } = await commerce.getSiteInfo({ config, preview }) const siteInfoPromise = commerce.getSiteInfo({ config, preview })
const { pages } = await pagesPromise
const { categories } = await siteInfoPromise
return { return {
props: { pages, categories }, props: { pages, categories },
} }

View File

@ -11,13 +11,16 @@ export async function getStaticProps({
locales, locales,
}: GetStaticPropsContext) { }: GetStaticPropsContext) {
const config = { locale, locales } const config = { locale, locales }
const { products } = await commerce.getAllProducts({ const productsPromise = commerce.getAllProducts({
variables: { first: 6 }, variables: { first: 6 },
config, config,
preview, preview,
}) })
const { categories, brands } = await commerce.getSiteInfo({ config, preview }) const pagesPromise = commerce.getAllPages({ config, preview })
const { pages } = await commerce.getAllPages({ config, preview }) const siteInfoPromise = commerce.getSiteInfo({ config, preview })
const { products } = await productsPromise
const { pages } = await pagesPromise
const { categories, brands } = await siteInfoPromise
return { return {
props: { props: {

View File

@ -10,8 +10,10 @@ export async function getStaticProps({
locales, locales,
}: GetStaticPropsContext) { }: GetStaticPropsContext) {
const config = { locale, locales } const config = { locale, locales }
const { pages } = await commerce.getAllPages({ config, preview }) const pagesPromise = commerce.getAllPages({ config, preview })
const { categories } = await commerce.getSiteInfo({ config, preview }) const siteInfoPromise = commerce.getSiteInfo({ config, preview })
const { pages } = await pagesPromise
const { categories } = await siteInfoPromise
return { return {
props: { pages, categories }, props: { pages, categories },

View File

@ -15,20 +15,23 @@ export async function getStaticProps({
preview, preview,
}: GetStaticPropsContext<{ slug: string }>) { }: GetStaticPropsContext<{ slug: string }>) {
const config = { locale, locales } const config = { locale, locales }
const { pages } = await commerce.getAllPages({ config, preview }) const pagesPromise = commerce.getAllPages({ config, preview })
const { categories } = await commerce.getSiteInfo({ config, preview }) const siteInfoPromise = commerce.getSiteInfo({ config, preview })
const productPromise = commerce.getProduct({
const { product } = await commerce.getProduct({
variables: { slug: params!.slug }, variables: { slug: params!.slug },
config, config,
preview, preview,
}) })
const { products: relatedProducts } = await commerce.getAllProducts({ const allProductsPromise = commerce.getAllProducts({
variables: { first: 4 }, variables: { first: 4 },
config, config,
preview, preview,
}) })
const { pages } = await pagesPromise
const { categories } = await siteInfoPromise
const { product } = await productPromise
const { products: relatedProducts } = await allProductsPromise
if (!product) { if (!product) {
throw new Error(`Product with slug '${params!.slug}' not found`) throw new Error(`Product with slug '${params!.slug}' not found`)

View File

@ -10,8 +10,10 @@ export async function getStaticProps({
locales, locales,
}: GetStaticPropsContext) { }: GetStaticPropsContext) {
const config = { locale, locales } const config = { locale, locales }
const { pages } = await commerce.getAllPages({ config, preview }) const pagesPromise = commerce.getAllPages({ config, preview })
const { categories } = await commerce.getSiteInfo({ config, preview }) const siteInfoPromise = commerce.getSiteInfo({ config, preview })
const { pages } = await pagesPromise
const { categories } = await siteInfoPromise
return { return {
props: { pages, categories }, props: { pages, categories },

View File

@ -20,8 +20,10 @@ export async function getStaticProps({
} }
const config = { locale, locales } const config = { locale, locales }
const { pages } = await commerce.getAllPages({ config, preview }) const pagesPromise = commerce.getAllPages({ config, preview })
const { categories } = await commerce.getSiteInfo({ config, preview }) const siteInfoPromise = commerce.getSiteInfo({ config, preview })
const { pages } = await pagesPromise
const { categories } = await siteInfoPromise
return { return {
props: { props: {