question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SSR: Server-Side Rendering

See original GitHub issue

Hello,

I’m currently trying to use boardgame.io together with next.js. Because the Server part exports a Koa server, I went with this example to create a basic project: https://github.com/zeit/next.js/tree/canary/examples/custom-server-koa

However, when I run the code, I get a window is not defined error. So I did some research and it’s of course because Next.js is SSR and the Client component requires window to be present. So I made it like this:

const App = Client({
    game: Game,
    multiplayer: true,
});

export default class Index extends React.Component {
    state = {
        isClient: false,
    };

    componentDidMount() {
        this.setState({
            isClient: true,
        });
    }

    render() {
        if (this.state.isClient) {
            return <App />;
        }

        return 'Loading...';
    }
}

According to next.js, the client side code only runs, after componentDidMount has been triggered, so obviously window should be available, but somehow it isn’t. Something inside Client seems to run before that happens.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
vdfdevcommented, Mar 12, 2018

I got it working! 👏 Just submitted a PR, also managed to do a test so we dont break SSR in the future 😆

1reaction
tomasreichmanncommented, Jan 10, 2018

Maybe something like this could help… https://www.npmjs.com/package/window-or-global

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Server-Side Rendering? Definition and FAQs
Server -side rendering (SSR) is an application's ability to convert HTML files on the server into a fully rendered HTML page for the...
Read more >
The Benefits of Server Side Rendering Over Client ... - Medium
SSR throughput of your server is significantly less than CSR throughput. For react in particular, the throughput impact is extremely large.
Read more >
What is server-side rendering and how does it improve site ...
Server -side rendering (SSR) addresses the performance and search engine optimization issues of single-page JavaScript applications.
Read more >
Server-Side Rendering (SSR) - Vue.js
During SSR, each request URL maps to a desired state of our application. There is no user interaction and no DOM updates, so...
Read more >
Client-side vs. Server-side vs. Pre-rendering for Web Apps
An SSR/universal application can be really powerful if you have a large application with a lot of different pages. It allows your content...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found