Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
redux-toolkit nextjs server-side rehydration

I am trying to:

  1. get server-sided headers (req.headers.x-my-custom-header)
  2. getInitialProps (_document) dispatch x-my-custom-header to store (server-sided) <- PROBLEM
  3. rehydrate x-my-custom-header to client

_document.tsx:

MyDocument.getInitialProps = async (context: DocumentContext) => {
  const initialProps = await Document.getInitialProps(context);
  const isServer = context.req;
  if (isServer) {
    // const site = context.req?.headers['x-site']
    store.dispatch(setSite('foo')); // <- setting state to 'foo'
  }
  const initialState = store.getState();
  console.log('initialState', initialState); // <- has 'bar' in it
  return {
    ...initialProps,
    initialState,
  };
};

my slice

import { createSlice, Slice } from '@reduxjs/toolkit';

export const name = 'settings';

export enum Sites {
  foo = 'foo',
  bar = 'bar',
}

export type SettingsState = {
  site: Sites;
};

const initialState: SettingsState = {
  site: Sites.bar,
};

const slice: Slice<SettingsState> = createSlice({
  name,
  initialState,
  reducers: {
    setSite(state, action: { type: string; payload: Sites }) {
      state.settings.site = action.payload;
    },
  },
});

export const { setSite } = slice.actions;

export default slice.reducer;

Any ideas what could be the problem here?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!