Next 13.5.6 "PAGES": getServersideProps long request
Unanswered
Bartholomeas posted this in #help-forum
[Nextjs 13 PAGES dir]
Hello, didnt worked in /pages for some time, but now im having a problem. In getServersideProps i have two requests, getProducts and getFilters. getProducts is very slow, but getFilters is 2-3x faster but my entire loading state and return from getServersideProps is returnde after time of getProducts time.
Can i make it faster or somehow separate return of these two requests?
Its pretty legacy and dirty code..
Hello, didnt worked in /pages for some time, but now im having a problem. In getServersideProps i have two requests, getProducts and getFilters. getProducts is very slow, but getFilters is 2-3x faster but my entire loading state and return from getServersideProps is returnde after time of getProducts time.
Can i make it faster or somehow separate return of these two requests?
Its pretty legacy and dirty code..
export const getServerSideProps = async (context) => {
const sessionId = await getSessionId(context.req, context.res)
const params = context.resolvedUrl.split("?")?.[1] ?? ""
let filters = formatURLParams(params, context.query)
filters = formatSearchKeys({ filters })
try {
const productsAndMeta = await getAllProducts(filters, sessionId)
const filterCategories = await getFilterCategories()
const envelopWidths =
productsAndMeta?.attributes &&
productsAndMeta?.attributes
?.filter((attribute) => {
return attribute?.name === "pa_szerokosc-koperty"
})?.[0]
?.options?.map((envelopAttribute) => {
return envelopAttribute.slug
})
const parsedProductsFilters = parseProductsFilters(filters, envelopWidths)
if (context.res.status >= 400) context.res.writeHead(500, { Location: "/500" })
return {
props: {
filters: parsedProductsFilters,
filterCategories,
productsAndMeta,
},
}
} catch (error) {
notifySentry(error, context.req, context.res.statusCode)
return {
redirect: {
destination: "/404",
statusCode: 302,
},
}3 Replies
There is no way of "streaming" or partial rendering like in app approach?
Its pretty legacy project and entire page is in loading state to the finish of longest (and its really long) 'getAllProducts' request
Its pretty legacy project and entire page is in loading state to the finish of longest (and its really long) 'getAllProducts' request
for the
/pages router you need to wait for all the requests to complete to continue rendering the page. what you can do is to run both requests in parallel so it finishes a bit faster:const [productsAndMeta, filterCategories] = await Promise.all([
getAllProducts(...),
getFilterCategories()
])@Rafael Almeida for the `/pages` router you need to wait for all the requests to complete to continue rendering the page. what you can do is to run both requests in parallel so it finishes a bit faster:
js
const [productsAndMeta, filterCategories] = await Promise.all([
getAllProducts(...),
getFilterCategories()
])
damn... On the start of app dir i underestimated "APP" approach but now i see how awesome it is... xd
Thanks!
Thanks!