Next.js: Fabric CSS + Next.js document is not defined

Created on 6 Apr 2017  路  12Comments  路  Source: vercel/next.js

I using Fabric React by Microsoft in my Next.js application. For first time load I got document is not defined, but for 2 or maybe 3 times I try to reload that pages it works. How it can be? How can I solve this problem, so I never see document is not defined everytime I restart my server.

Most helpful comment

How can I handle document not defined in next.js?

All 12 comments

Could you provide an repo with example?

@aranajhonny here's my repo https://github.com/rohmanhm/nextjs-fabric
when you run npm run dev for first time it will throw an error document not defined but if you load the page it will passed without any errors.

@luisrudge I'm not sure, so why it's just throw an error when I open my page for first time load after restart my server?

Not sure. what I'm sure is that you have to follow the lib's instructions to make it work with SSR

First, take a look my repo https://github.com/rohmanhm/nextjs-fabric
I followed all instructions in here https://github.com/OfficeDev/office-ui-fabric-react#rendering-fabric-components-on-the-server-ssr

I just confused about this line

import * as React from 'react';
import * as ReactDOMServer from 'react-dom/server';
import { Button } from 'office-ui-fabric-react/lib/Button'; 

let body = ReactDOMServer.renderToString(<Button>hello</Button>);

console.log(
  `
  <html>
  <head>
    <style>${ _allStyles}</style>
  </head>
  <body>
    ${ body}
  </body>
  </html>
  `);

How I can implement renderToString in nextjs

It happens that the SSR is not working, I am reviewing your example.

Turn off javascript in the browser, and you'll see it does not work.

I think for now this will not work with next.js there are many things incompatible with ssr.

How can I handle document not defined in next.js?

Probably works when you use dynamic imports in next@beta https://github.com/zeit/next.js#dynamic-import

Yes it's work fine, but with dynamic import is very slow. any other solution?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

formula349 picture formula349  路  3Comments

swrdfish picture swrdfish  路  3Comments

olifante picture olifante  路  3Comments

pie6k picture pie6k  路  3Comments

timneutkens picture timneutkens  路  3Comments