DEV Community

How to Disable Server-Side Rendering (SSR) in Next.js

Ari Koponen on December 23, 2020

Learn how to disable SSR in Next.js and use it to replace Create React App or any custom tooling. Next.js is my absolute favorite tool for deve...
Collapse
 
ivan_jrmc profile image
Ivan Jeremic

If you import the component with next/dynamic you have an option which you can pass in {ssr: false }

Collapse
 
apkoponen profile image
Ari Koponen

Yes, that is useful. next/dynamic also allows you to send a smaller initial JavaScript bundle to the browser, which is great for performance!

Collapse
 
franklivania profile image
Chibuzo Franklin Odigbo

I tried using next/dynamic, and I am still getting the same errors I am getting in production. The window error.

Collapse
 
pranaypratyush profile image
Pranay Pratyush

What's the difference between this method and simply importing my main App component with next/dynamic and passing {ssr: false}?

Thread Thread
 
apkoponen profile image
Ari Koponen

Haven’t tried to do that, so not sure. Using next/dynamic might lead to one additional JS bundle.

Also, if you want multiple entrypoints (like pages/index.js and pages/account.js) you’d need to remember to do the dynamic import in each file separately.

Collapse
 
meglio profile image
Anton A.

In this scenario, what's the point in keeping using Next.js at all? No SSR, no routing, so why would one use this workaround and keep staying with Next.js instead of using something like create-react-app?

Collapse
 
apkoponen profile image
Ari Koponen • Edited

Next.js is just better than create-react-app in many small ways. It has everything that create-react-app has and more in a very developer-friendly package.

One major thing I can mention that you never need to "eject" from Next.js the way you might need to do with create-react-app. You can customize your Webpack Config and be happy (nextjs.org/docs/api-reference/next...).

Also, even if you don't need SSR now, it does not mean you won't need it in the future. Using Next.js as your tooling will allow you to use SSR/SSG for some pages and normal client-side rendered behavior SPA for others.

Collapse
 
meglio profile image
Anton A.

So, the idea is to use Next.js but not its killer features because "just in case"?

Thread Thread
 
apkoponen profile image
Ari Koponen

What do you mean by not using Next.js's "killer features"?

Collapse
 
hamzalak profile image
LAKHAL Hamza

Because you don't need SSR every time blog.usejournal.com/when-should-i-...

Collapse
 
shadowtime2000 profile image
shadowtime2000

You need a complicated hosting environment. You cannot just upload your app to a server or a CDN. You need Node.js servers doing the server-side rendering. This adds complexity and also costs more.

NextJS only does this with the next start command, it can be used with smaller serverless platforms that can reduce costs.

Collapse
 
apkoponen profile image
Ari Koponen

Correct, serverless is cheaper. Still, even a serverless platform will cost more than a CDN serving plain HTML, and even if the platform hides the complexity of doing SSR at scale, it still exists.

If you're able to use Vercel, things will work nicely out of the box. They've invested millions in having a developer-friendly platform that hides all the complexity of the underlying serverless infrastructure and tooling. 🤗

With platforms like Netlify or AWS, you'll have to do a little bit more work, but there are developer-friendly solutions like github.com/netlify/next-on-netlify and serverless.com/blog/serverless-nextjs.

However, many companies cannot use serverless solutions. I can talk from experience that running your own Node.js servers to do SSR is not very simple. Would not do it again. 😫

Collapse
 
ashutoshpw profile image
Ashutosh Kumar

Despite all the criticism to this article, I would like to thank @apkoponen for this wonderful article.

People may say that what is the use of next-js then but people coming from nuxt.js side will understand the importance of this article. Also, it helps us to get a bit better understanding of how exactly next.js works.

Thanks again @apkoponen

Collapse
 
masulum profile image
Ulum

Hi @apkoponen ,

thanks for the article. it really helps us. currently we are using nextjs and we would like to run it on http server such as : nginx or apache httpd. and API also already built by other team, so we don't really need ssr in our case.

i also agree with you about nextjs in some cases better than create-react-app

Collapse
 
mickmister profile image
Michael Kochell

I'm interested in trying nextjs for the DX benefits, though I've heard horror stories about SSR intricacies, and personally I don't want to learn that if I don't have to.

The redirect stuff seems like a bit of a hack. Does this make it so the user can't refresh on their current page, and they are automatically sent back to the root (or whatever you redirected to)?

Would you be able to explain how you go from a client-side-only project (like one made from CRA), and modify it to work with nextjs (with no SSR enabled on purpose)? Assume things like react-router is already set up. What are the main benefits of making the switch tool nextjs in this case?

Collapse
 
parkerproject profile image
Parker

Also check out this thread
stackoverflow.com/questions/531398...

Collapse
 
ajsharp profile image
Alex Sharp 🛠sharesecret.co

Worth mentioning the target option is now deprecated/removed

Collapse
 
franklivania profile image
Chibuzo Franklin Odigbo

I have been having a hosting issue because of the window error. I'd try this to see if it works. Mind that I did dynamic imports and used the undefined methods, to no awail