[英]NextJs Middleware and redux store
I would like to check what's inside my redux store from NextJs middleware.我想从 NextJs 中间件检查我的 redux 商店里面有什么。
I couldn't find a solution, and the better i achieved is the following:我找不到解决方案,我取得的更好的是以下内容:
I've started with the [NextJs-with-redux-saga]example( https://github.com/vercel/next.js/tree/canary/examples/with-redux-saga )我已经开始使用 [NextJs-with-redux-saga] 示例( https://github.com/vercel/next.js/tree/canary/examples/with-redux-saga )
and to access the store SSR I had to use a library .要访问商店 SSR,我必须使用库。 Current store.js content is like this
当前 store.js 内容是这样的
import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { HYDRATE, createWrapper } from 'next-redux-wrapper';
import rootReducer from './reducers/rootReducer';
import createSagaMiddleware from 'redux-saga';
import rootSaga from './sagas/rootSaga';
const sagaMiddleware = createSagaMiddleware();
const reducer = (state, action) => {
if (action.type === HYDRATE) {
// Merge any state which can be set in SSR here
const nextState = {
...state,
tick: {
...action.payload.tick,
},
};
return nextState;
} else {
return rootReducer(state, action);
}
};
const initStore = () => {
console.log('here');
const store = createStore(reducer, composeWithDevTools(applyMiddleware(sagaMiddleware)));
sagaMiddleware.run(rootSaga);
return store;
};
export const nextStore = createWrapper(initStore);
I've then created a middleware under the pages directory as per doc然后我根据文档在 pages 目录下创建了一个中间件
import { NextResponse } from 'next/server';
const middleware = (req, ev) => {
return NextResponse.next();
};
export default middleware;
This clearly wasn't enough since i need access to the store.这显然还不够,因为我需要访问商店。 The library I picked has no solution for accessing the store on the middleware, so i tried to hack around by using the
getServerSideProps
one.我选择的库没有访问中间件上的存储的解决方案,所以我尝试使用
getServerSideProps
来破解。
import { NextResponse } from 'next/server';
import { nextStore } from '@store/store';
export const middleware = nextStore.getServerSideProps((store) => async ({ req, query, res }) => {
const { tick } = store.getState();
console.log(tick;
return NextResponse.next();
});
somehow it does work and i can see the store, but some errors is prompted不知何故它确实有效,我可以看到商店,但提示了一些错误
error - TypeError: result.response.headers is not iterable
错误 - TypeError:result.response.headers 不可迭代
./node_modules/react/cjs/react.development.js
./node_modules/react/cjs/react.development.js
eval
not allowed in Middleware pages/_middleware中间件页面/_middleware 中不允许使用
eval
Was anyone able to achieve this?有没有人能够做到这一点?
As far I know NextJS middleware new feature works on server side.据我所知,NextJS 中间件的新功能适用于服务器端。 While Redux Front-end library.
而 Redux 前端库。 may It works on some cases but it will show error where redux needs some browsers actions related.
可能它适用于某些情况,但它会显示错误,其中 redux 需要一些与浏览器相关的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.