#How do i speed up `getServerSideProps`? The first request is around 7-10 seconds.

28 messages · Page 1 of 1 (latest)

elfin plinth
#

I have all the code related to data fetching using firebase in getServerSideProps. Read the docs to write server side code in getServerSideProps directly.
After deploying it to vercel, I have noticed the first request is about 8-10 seconds. Which is very slow. Next requests are quick and less than 1 second. Someone said its an issue with cold starts. How do I speed this up?

Possible solutions I am thinking is use a auto-pinger or something like that keeps hitting the site's webpage every few seconds. But for a large app, its not ideal to ping all other pages.

Is there anyway I can speed this process up, so that no matter when I visit the site its fast and there is a quick response.

forest tapir
#

You’re experiencing a cold start

elfin plinth
#

Yes, is there any workaround for that? How do I workaround that?

forest tapir
#

Reducing your function size can help

#

The way I’ve worked around this is by keeping functions warm by pinging them every couple of minutes

#

Unfortunately this is a side effect of serverless architectures

#

In theory if your site has active usage then the amount of cold starts will be reduced since there people using the app

#

But for sites with infrequent traffic this is especially noticeable

#

There are some ui tricks you can do

#

Like serving a static site first, and then initiating a client side request to your backend

elfin plinth
#

I am not using /api/ any routes. So I have deleted the folder.
But I have about 9 -10 category, and each category has 100-150 pages.
Then I would have to ping all of them every few minutes?

forest tapir
#

To warm up the the function

#

I don’t remember if each page runs within its own lambda container or a shared lambda container

#

You’ll need to experiment and see

fiery bramble
#

cache stuff

#

use dynamic imports

elfin plinth
forest tapir
#

I think it’s 5 minutes

#

Checkly is a good service

elfin plinth
# fiery bramble cache stuff

Yes!
I have tried this

  context.res.setHeader('Cache-Control', 'public, s-maxage=10, stale-while-revalidate=59');
forest tapir
#

You can use their service to check up a health check which pings your functions

elfin plinth
#

Do I need to increase this time?

fiery bramble
#

10 is 10 seconds

forest tapir
#

Keep in mind that it’s useful to include a header in your pings to indicate it’s a warm up call

#

And short circuit your logic to just returns a 200 response without any additional logic running

#

I would look at caching and code splitting though before going down the pinging route

elfin plinth