I am looking for some Next.js Architecture advise please 🙏
I am in the middle of migrating a very large express NodeJS app which has a custom SSR setup over to a Next.js setup.
For the current NodeJS site I have:
eg:
The frontend URL would be: www.mywebsite.com.au
And any api call done from the frontend is done with route: www.mywebsite.com.au/api/*
Based on all the docs i have read so far, I figure the best setup is:
/api/*
ones/api/*
(which I kind of already have but need to clean up)nextApp.render
to specific next pagesgetInitialProps
in the Next.js pages to fetch data from those apis I mentioned in 2.getInitialProps
This is so that the server and client share the same logic of whats in getInitialProps
- and this will make the architecture clean and give me the ability to have smooth transition to use Link
.
The thing im struggling with it is step 4.
It means now on SSR of a page request it needs to call out to the public api (www.mywebsite.com.au/api/*
) on getInitialProps
to fetch its data.
To me seems like a performance overhead to do this as it requires a network hop to public domain mywebsite.com.au
to fetch data it could have got locally on that same request (by calling localhost:3000/api/*
for instance)
Any advise on how to avoid the network hop outside for server render?
Can I detect if its a server render and then use an internal URL (eg: use localhost:3000/api/*
on the request to the same web server) or is that not best practices when using Next.js?
The server needs to call out to itself to fetch its data, alot of the overhead involved with fetching data is around the delay (I.E Between the client and nearest datacenter) this will be less of a concern when its (datacenter -> datacenter).
If performance is still an issue, your next best bet will be to do SSR caching, of which there are many different methods alot you can find in a quick google search however it will depend on your hosting/setup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With