> For the complete documentation index, see [llms.txt](https://docs.tss-react.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tss-react.dev/v3-1/ssr/next.js.md).

# Next.js

{% hint style="danger" %}
Next.js + React 18 -> SSR will only work with Next.js 12.1.7-canary.4 or newer.
{% endhint %}

Setup to make SSR work with [Next.js](https://nextjs.org).

```
yarn add @emotion/server
```

{% tabs %}
{% tab title="With MUI" %}
{% hint style="info" %}
The following instructions are assuming you are using `@mui`v5.

&#x20;You can find [here](https://github.com/garronej/tss-react/tree/main/src/test/apps/muiV4ssr) a Next.js setup with `@material-ui` v4.
{% endhint %}

`pages/_document.tsx`

```tsx
import BaseDocument from "next/document";
import { withEmotionCache } from "tss-react/nextJs";
import { createMuiCache } from "./index";

export default withEmotionCache({
    //If you have a custom document pass it instead
    "Document": BaseDocument,
    //Every emotion cache used in the app should be provided.
    //Caches for MUI should use "prepend": true.
    "getCaches": ()=> [ createMuiCache() ]
});
```

`page/index.tsx`

```tsx
import type { EmotionCache } from "@emotion/cache";
import { CacheProvider } from "@emotion/react";
import createCache from "@emotion/cache";

let muiCache: EmotionCache | undefined = undefined;

export const createMuiCache = () =>
    muiCache = createCache({
        "key": "mui",
        "prepend": true
    });

export default function Index() {
    return (
        <CacheProvider value={muiCache ?? createMuiCache()}>
            {/* Your app  */}
        </CacheProvider>
    );
}
```

You can find a working example [here](https://github.com/garronej/tss-react/tree/main/src/test/apps/ssr).

{% hint style="info" %}
This setup is merely a suggestion. Feel free, for example, [to move the `<CacheProvider/>` into `pages/_app.tsx`](https://github.com/garronej/tss-react/blob/main/src/test/apps/ssr/pages/_app.tsx).&#x20;

What's important to remember however is that new instances of the caches should be created **for each render**`!`
{% endhint %}
{% endtab %}

{% tab title="Without MUI" %}
`pages/_document.tsx`

```tsx
import BaseDocument from "next/document";
import { withEmotionCache } from "tss-react/nextJs";
import { createMuiCache } from "./index";

export default withEmotionCache({
    /** If you have a custom document pass it instead */,
    "Document": BaseDocument
});
```

{% hint style="warning" %}
`If you use` \<TssCacheProvider/> `or` \<CacheProvider/> `anywhere in your app you must provide a getCache function to withEmotionCache.` &#x20;

What's important to remember however is that new instances of the caches should be created **for each render**`!`

`You can get inspiration on how to do it under the`` `*`With MUI`*` ``tab.`
{% endhint %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
If you are using nested selectors, you may need to provide [uniq identifiers to your stylesheet](/v3-1/nested-selectors.md#ssr).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.tss-react.dev/v3-1/ssr/next.js.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
