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.

Using redux-api with Next.js next-redux-wrapper for server-side rendering?

See original GitHub issue

Anyone tried using redux-api with Next.js for server-side rendering?

I’d like to get tips on how to do the initial redux-api sync() call inside getInitialProps (Next.js specific method) instead of componentDidMount (as in the example), to get the data rendered server-side along with the first page request.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tomsoderlundcommented, Dec 21, 2017

Here’s a complete working example of Next.js, redux-api, and next-redux-wrapper working in harmony:

https://github.com/tomsoderlund/nextjs-express-mongoose-crudify-boilerplate

0reactions
te-onlinecommented, Sep 12, 2019

Just to follow up my own question: I ended up creating a (kind of?) singleton class for ReduxApi that has a setter for handing in the database connection instance. This database class variable can then be used by the custom adapter, deciding whether to fetch from the API or database and using the connection instance to query the database.

Like so

// ReduxApiClient.ts

class ReduxApiClientLib {
  private rest;
  private req;

  constructor() {
    this.rest = reduxApi({
     ...
    }).use('fetch', this.adapter);
  }

  public setServerRequest = (req) => {
    this.req = req;
  };

  public getRest = () => {
    return this.rest;
  };

  private adapter = async (url, options) => {
    // Server-side behaviour
    if (this.req) {
      let data: any = [];
      switch (url) {
        case '/api/test':
          ...
          // Query database
          data = await req.database().get('test');
          break;
          ...
      }
      // Return
      return Promise.resolve(data);
    }

    // Default client-side behaviour
    try {
      const response = await fetch(
        fetchUrl,
        { ...options.fetchOptions, credentials: 'include' } || { credentials: 'include' },
      );
      if (response.ok) {
        const result = await response.json();
        return result.data;
      }
    } catch (e) {
      return [];
    }
  };

  ...
}

export const ReduxApiClient = new ReduxApiClientLib();
// _app.tsx
...
static async getInitialProps({ ctx }) {
    ...
    if (ctx.req) {
      // Get data from database
      ReduxApiClient.setServerRequest(ctx.req);
      await ctx.store.dispatch(ReduxApiClient.getRest().actions.test.sync());
      // Get current state
      const state: ReduxState = ctx.store.getState();
      // Compose props
      pageProps.test = state.apiData.test.data;
    }
    ...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Server Side Rendering - Redux Toolkit
RTK Query supports Server Side Rendering (SSR) with Next.js via rehydration in combination with next-redux-wrapper.
Read more >
How to integrate Redux — with Next.js and SSR
First of all, we need to create our redux store using the “createStore” function provided by the Redux Toolkit. nextRepo: nextSlice,
Read more >
I want to use Redux with Next.js and do Server-Side ... - Reddit
Create a Redux store on the server side for every incoming request. Dispatch some actions and set up initial data while rendering the...
Read more >
Server-Side Rendered App with Next.js, React and Redux
Next -redux-wrapper will enable us to create a store at every new request and it will pass it to MyApp (Our App Implementation)...
Read more >
How to use Redux in Next.js - LogRocket Blog
The payload of this action will contain the state at the moment of static generation or server-side rendering, so your reducer must merge...
Read more >

github_iconTop Related Medium Post

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