Is it possible to disable ssr on some pages using Next js? For example, I have a page with a product description on which I use ssr for SEO but I also have a page with a list of items or products which I can filter and for that page, I don’t want to use ssr because this page generates dynamically every time, how can I disable ssr on this page?
9
Answers
Lazy load the component and disable SSR: https://nextjs.org/docs/advanced-features/dynamic-import#with-no-ssr
This is a late answer, but in case anyone runs into this:
This is on the “page” level.
isServer()
prevents rendering of anything while on the server side. You could, if you wanted to, prevent ssr on the component level also:The
dynamic()
function can also be used without a dynamic import:Anything wrapped in this component will not be visible in the SSR source. For example:
Here’s a solution that i just came up with based on the something mentioned in the React docs: https://reactjs.org/docs/react-dom.html#hydrate
You can then wrap any component / block with this and it won’t get rendered on the server. e.g.
This also prevents the following React.hydrate warning "Warning: Expected server HTML to contain a matching in ."
We can also use react-no-ssr React component.
Let’s say Comments is our client only component. Now we need to render it only on the client. Here’s how we do it.
Another and I believe the simplest solution is to just use
process.browser
, which will only be true when ran on client side.Put this on your _app.tsx
In @refactor answer, the
react-no-ssr
is not working for some typescript issue.I just wrote a very simple NoSSR component as below:
and can be used anywhere if you want to avoid SSR like below:
Based entirly on the answer by Erik Hofer, you can create an HOC to wrap your pages or other components in.
Then use as: