6.46.8 • Published 4 days ago

@gasket/plugin-redux v6.46.8

Weekly downloads
93
License
MIT
Repository
github
Last release
4 days ago

@gasket/plugin-redux

Gasket plugin to setup redux store available to express middleware.

Installation

New apps

Recommended

gasket create <app-name> --plugins @gasket/plugin-redux

Existing apps

npm i @gasket/plugin-redux @gasket/redux redux

Modify plugins section of your gasket.config.js:

module.exports = {
  plugins: [
    add: [
+      '@gasket/plugin-redux'
    ]
  ]
}

Configuration

By default, Gasket apps will use the makeStore function from @gasket/redux/make-store. App developers can choose to use different makeStore file by utilizing configureMakeStore from @gasket/redux and then pointing to this file in the gasket.config.js.

Options

  • makeStore - (string) relative path to a custom makeStore configuration. If not specified, the plugin will check if a store.js file exists in the root of your project. Otherwise, the default will be used. This must be a CommonJS module.
  • initState - (object) initial state to include in the store

Example config

// gasket.config.js

module.exports = {
  redux: {
    makeStore: './relative/path/to/customMakeStore.js',
    initState: {
      urls: {
        fooService: 'https://foo.url/',
        barService: 'https://bar.url/'
      }
    }
  },
  
  // You can override initState by environment
  environments: {
    test: {
      redux: {
        initState: {
          urls: {
            fooService: 'https://test.foo.url/'
          }
        }
      }
    }
  }
}
// customMakeStore.js

const { configureMakeStore } = require('@gasket/redux');
const reducers = require('./reducers'); // app's reducers
const Log = require('@gasket/log');     // custom log implementation

module.exports = configureMakeStore({ reducers, logger: new Log() });

Usage

This plugin attaches a store object to the node request object. This allows redux to be invoked in express middleware and the same store instance used during server-side rendering.

Example middleware

async function doSomethingMiddleware(req, res, next) {
  try {
    await req.store.dispatch(myActionCreator());
    next();
  } catch(err) {
    next(err); 
  }
}

Lifecycles

initReduxState

This plugin fires an initReduxState event when constructing the initial server-side state for the redux store. Plugins that need to modify this initial state can hook this event and return a modified version of the initial state or a Promise that resolves to a new initial state. Example plugin:

const getExperiments = require('./get-experiments');

module.exports = {
  id: 'gasket-plugin-example',
  hooks: {
    initReduxState(gasket, state, req, res) {
      return {
        ...state,
        experiments: getExperiments(req)
      }
    }
  }
};

The hook is passed the following parameters:

ParameterDescription
gasketThe gasket API
stateThe initial state of the redux so far
reqThe express request object
resThe express response object

initReduxStore

The plugin fires an initReduxStore event after the server-side redux store has been constructed. This gives other plugins a chance to do such things as read the initial state or fire off actions to populate the store. Asynchronous actions should return a Promise. Example plugin:

const getExperiments = require('./get-experiments-action');

module.exports = {
  id: 'gasket-plugin-example',
  hooks: {
    initReduxStore(gasket, store, req, res) {
      store.dispatch(getExperiments(req));
    }
  }
};

The hook is passed the following parameters:

ParameterDescription
gasketThe gasket API
storeThe redux store
reqThe express request object
resThe express response object

License

MIT

7.0.0-next.21

4 days ago

7.0.0-next.9

8 days ago

7.0.0-next.10

8 days ago

7.0.0-next.7

9 days ago

7.0.0-next.11

8 days ago

7.0.0-next.8

8 days ago

7.0.0-next.12

8 days ago

7.0.0-next.13

8 days ago

7.0.0-next.6

9 days ago

7.0.0-next.20

8 days ago

7.0.0-next.5

9 days ago

6.46.8

10 days ago

7.0.0-next.4

11 days ago

7.0.0-next.3

15 days ago

7.0.0-next.2

22 days ago

7.0.0-next.1

23 days ago

7.0.0-next.0

23 days ago

7.0.0-cli.7

2 months ago

7.0.0-cli.6

2 months ago

6.46.1-cli.0

2 months ago

7.0.0-cli.5

2 months ago

7.0.0-cli.4

2 months ago

7.0.0-cli.1

2 months ago

7.0.0-cli.0

2 months ago

7.0.0-cli.3

2 months ago

7.0.0-cli.2

2 months ago

6.46.3-cli.0

2 months ago

6.46.2

2 months ago

7.0.0-canary.1

2 months ago

6.46.2-esm.0

2 months ago

6.46.0

3 months ago

6.45.2

3 months ago

6.45.0

4 months ago

6.43.0

7 months ago

6.39.3

11 months ago

6.38.8

12 months ago

6.39.0

12 months ago

6.38.5

1 year ago

6.38.1

1 year ago

6.36.1

1 year ago

6.38.0

1 year ago

6.34.6

2 years ago

6.35.0

1 year ago

6.36.0

1 year ago

6.34.3

2 years ago

6.34.4

2 years ago

6.34.2

2 years ago

6.33.1

2 years ago

6.26.1

2 years ago

6.30.0

2 years ago

6.28.0

2 years ago

6.21.0

2 years ago

6.24.2

2 years ago

6.24.0

2 years ago

6.20.4

2 years ago

6.20.3

2 years ago

6.20.2

2 years ago

6.19.0

2 years ago

6.17.0

2 years ago

6.17.1

2 years ago

6.15.2

2 years ago

6.10.1

2 years ago

6.10.0

2 years ago

6.14.0

2 years ago

6.1.0

3 years ago

6.0.12

3 years ago

6.0.8

3 years ago

6.0.7

3 years ago

6.0.3

3 years ago

6.0.0

3 years ago

6.0.0-canary.13

3 years ago

6.0.0-canary.10

3 years ago

6.0.0-canary.6

3 years ago

6.0.0-canary.4

3 years ago

6.0.0-canary.2

4 years ago

6.0.0-canary.1

4 years ago

6.0.0-canary.0

4 years ago

5.6.0

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

5.0.0-canary.4

4 years ago

5.0.0-canary.3

4 years ago

5.0.0-canary.2

4 years ago

5.0.0-canary.1

4 years ago

5.0.0-canary.0

4 years ago