Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context and Server Side Rendering #38

Closed
codyaverett opened this issue Aug 27, 2018 · 5 comments · Fixed by #40
Closed

Context and Server Side Rendering #38

codyaverett opened this issue Aug 27, 2018 · 5 comments · Fixed by #40
Labels
bug Something isn't working

Comments

@codyaverett
Copy link
Collaborator

One of our project's GatsbyJS server side rendering seems to have some issues with finding the shape of state during build.

Example Snippet

export const SharedContainer: ComposableContainer<State, Actions, {}, Effects> = props => {
    return <Container initialState={initialState} effects={effects} actions={actions} context="myContextString" {...props} />;
};

Gatsby Build error

error Building static HTML for pages failed

See our docs page on debugging HTML builds for help https://goo.gl/yL9lND

  302 |             mountContainer = _ref.mountContainer;
  303 |         return createElement(InnerContainer, _extends({}, _this4.props, {
> 304 |           state: state[context],
      | ^
  305 |           setContextState: setContextState,
  306 |           mountContainer: mountContainer
  307 |         }));


  WebpackError: TypeError: Cannot read property 'myContextString' of undefined

The link in the error describes common pitfalls of SSR in gatsby.
e.g. window object is missing, either mockup window or wrap the code in a conditional so that it only runs if the object is truthy.

Running the app in dev mode does not have this issue. (Not SSR)

I'm trying to figure out how to get around this. I'd like to not have to mock out the state of the app and just use context as is, but i'm not sure if that is possible at this point.

@diegohaz diegohaz added the bug Something isn't working label Aug 28, 2018
@diegohaz
Copy link
Owner

The error seems to be on this line:

state={state[context]}

That state comes directly from Provider and it's set there in the constructor:

state: initialState as State,

Have you wrapped your app with Provider?

Maybe the solution is to add state: {} here:

const Context = React.createContext({} as ContextState);

@codyaverett
Copy link
Collaborator Author

Modifying this line as you recommended solved our issues.

const Context = React.createContext({} as ContextState);

My solution was just this

const Context = React.createContext<Partial<ContextState>>({ state: {} });

What do you think @diegohaz ?

@diegohaz
Copy link
Owner

I think that's OK. Do we really need Partial there?

@codyaverett
Copy link
Collaborator Author

codyaverett commented Aug 29, 2018

As I see it, it's either we have it like that as a partial, or we'd also need to define setContextState, and mountContainer in that createContext Call.

const Context = React.createContext<ContextState>({ state: {}, mountContainer: (...mountContainerParams) => void, mountContainer: { ...mountContainerProps } });

@diegohaz
Copy link
Owner

I guess we can define setContextState and mountContainer as optionals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants