1

Note that I am using Turbo360, you can find more about here at https://www.turbo360.co/, but I assure you it is exactly like express in how it works, so the error is not coming from here.

I have talked to some people and from what I have gathered this error is coming from my entry point where I call cont initialState = window.INITIAL_STATE;.

This error is triggered when I hit my back-end routes on the server side. However the route itself does succeed on the back-end, and what I mean by that is that if I were to log the data I get a user back success.

However on the front-end I get this instead: enter image description here

I will try not to clutter this up to much so you will have to have some good knowledge about SSR. The SSR part works just fine until I hit my back-end express routes. So I will keep the code to those unless it is necessary to add it in.

React entry point:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import { renderRoutes } from 'react-router-config';

import store from './stores';
import routes from './routes';

const initialState = window.INITIAL_STATE; //WHERE I BELIEVE ERROR IS COMMING FROM

ReactDOM.hydrate(
  <Provider store={store.configure(initialState)}>
    <BrowserRouter>
      <div>{renderRoutes(routes)}</div>
    </BrowserRouter>
  </Provider>,
  document.getElementById('root')
);

Server entry point:

require('babel-core/register')({
  presets: ['env', 'react', 'stage-0', 'stage-1']
});

const pkg_json = require('./package.json');
const vertex = require('vertex360')({ site_id: pkg_json.app });
var renderer = require('./renderer.js');

// initialize app
const app = vertex.app();

// import routes
const index = require('./routes/index');
const api = require('./routes/api');
const users = require('./routes/users');

// set routes
app.use('/api/users', users);

// hopefully will be used on every Route, this should handle SSR RR4
app.use(renderer);

module.exports = app;

Server Side users Route for register:

router.post('/register', (req, res) => {
  const body = req.body;

  if (body.username.length == 0) {
    res.json({
      confirmation: 'fail',
      message: 'Please enter an username address.'
    });

    return;
  }

  if (body.password.length == 0) {
    res.json({
      confirmation: 'fail',
      message: 'Please enter a password.'
    });

    return;
  }

  turbo
    .createUser(body)
    .then(data => {
      res.json({
        confirmation: 'success',
        user: data
      });
    })
    .catch(err => {
      res.json({
        confirmation: 'fail',
        message: err.message
      });
    });
});

Action:

axios.post('/api/users/register', params)
  .then(response => { console.log(response.data });
  .catch(err => { console.log(err); });
Taylor Austin
  • 5,407
  • 15
  • 58
  • 103

2 Answers2

2

Looking at the code the first fix that should be done is

cont initialState = typeof window !== "undefined" && window && window.INITIAL_STATE;.

This should ideally solve the window not defined issues for you. Since while doing SSR, window object will not exist since that is a browser level object.

VivekN
  • 1,562
  • 11
  • 14
0

You can use process.browser to exclude error in your app.

See bottom example:

function Example() {
  const initialState =
    process.browser && window.INITIAL_STATE;

  return <div> { initialState } </div>
}