Next.js Discord

Discord Forum

Saas official boilerplate and data fetching round trip

Unanswered
Southeastern blueberry bee posted this in #help-forum
Open in Discord
Southeastern blueberry beeOP
i've just had a look at the saas boilerplate on the next https://github.com/nextjs/saas-starter repo.

I noticed that on some pages the data fecthing was initiated on the browser https://github.com/nextjs/saas-starter/blob/be930693f2f830893f2902eecfed42b9a52f6aef/app/(dashboard)/dashboard/page.tsx#L41 when rendering.

Whereas before, data fetching was done during ssr, the commit description for this change announces a performance improvement, but I'd like to know how

1 Reply

its explained on the PR, they switched to a new version of SWR


This PR improves the performance of the dashboard routes. Previously, many of the logged-in pages were being dynamically rendered, even though this application can take advantage of Partial Prerendering (PPR).

All dashboard pages now have a static shell which gets prerendered, either with a manual loading.tsx file or up to a Suspense boundary defined inline in the page. At the same time, since I went from one custom hook useUser() to two with useTeam(), I moved from the raw use() API usage to the most recent version of useSWR().

The new SWR patterns are a wrapper on top of the use() API, so it was pretty much drop in. This required updates in a few places, but the same ideas apply. You can prerender the pages (yes, including client components) and kick of the Promises early in the root layout. Then, the results get streamed in. Works great with PPR. Btw, another nice thing with SWR, is that you can get the behavior of refetching on browser window refocus for free, if you want.

Finally, there were still a few places where I was manually hand-rolling using transitions and onClick versus React actions. I updated those to use the canonical React best practice of keeping the state between the server action and useActionState. That allowed me to delete some (confusing) code and go back to action={myAction}.