-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathindex.tsx
427 lines (369 loc) · 16.2 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import {AppContext} from 'next/app';
import {useCallback, useLayoutEffect, useMemo, useState} from 'react';
import {Store, Middleware, Action, Dispatch} from 'redux';
import {
GetServerSideProps,
GetServerSidePropsContext,
GetStaticProps,
GetStaticPropsContext,
NextComponentType,
NextPageContext,
} from 'next';
import {useDispatch} from 'react-redux';
/**
* Quick note on Next.js return types:
*
* Page.getInitialProps https://nextjs.org/docs/api-reference/data-fetching/getInitialProps
* as-is
*
* App.getInitialProps: AppInitialProps https://nextjs.org/docs/advanced-features/custom-app
* {pageProps: any}
*
* getStaticProps https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation
* {props: any}
*
* getServerSideProps https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering
* {props: any}
*/
const RENDER = '__NEXT_REDUX_WRAPPER_FIRST_RENDER__';
const REQPROP = '__NEXT_REDUX_WRAPPER_STORE__';
const getIsServer = () => typeof window === 'undefined';
const getDeserialized = (actions: Action[], {deserialize}: Config<any> = {}): Action[] => (deserialize ? deserialize(actions) : actions);
const getSerialized = (actions: Action[], {serialize}: Config<any> = {}): Action[] => (serialize ? serialize(actions) : actions);
export declare type MakeStore<S extends Store> = (init: {context: Context; reduxWrapperMiddleware: Middleware}) => S;
export interface InitStoreOptions<S extends Store> {
makeStore: MakeStore<S>;
context?: Context;
config: Config<S>;
}
export enum Source {
GIAP = 'GIAP',
GIPP = 'GIPP',
GSP = 'GSP',
GSSP = 'GSSP',
}
let sharedClientStore: any;
const replaceUndefinedWithNull = (obj: any): any => {
if (obj === undefined) {
return null;
}
if (Array.isArray(obj)) {
return obj.map(replaceUndefinedWithNull);
}
if (typeof obj !== 'object' || obj === null) {
return obj;
}
const res = {} as any;
for (const [key, value] of Object.entries(obj)) {
res[key] = replaceUndefinedWithNull(value);
}
return res;
};
const createMiddleware = (config: Config<any>): {log: Action[]; reduxWrapperMiddleware: Middleware} => {
const log: Action[] = [];
const reduxWrapperMiddleware: Middleware = api => next => action => {
if (!getIsServer()) {
return next(action);
}
log.push(action);
return next(action);
};
return {log, reduxWrapperMiddleware};
};
const initStore = <S extends Store>({makeStore, context = {}, config}: InitStoreOptions<S>): {store: S; log: Action[]} => {
const createStore = () => {
const {log, reduxWrapperMiddleware} = createMiddleware(config);
const store = makeStore({context, reduxWrapperMiddleware});
return {store, log};
};
if (getIsServer()) {
const req: any = (context as NextPageContext)?.req || (context as AppContext)?.ctx?.req;
if (req) {
// ATTENTION! THIS IS INTERNAL, DO NOT ACCESS DIRECTLY ANYWHERE ELSE
// @see https://github.com/kirill-konshin/next-redux-wrapper/pull/196#issuecomment-611673546
if (!req[REQPROP]) {
if (config.debug) {
console.log('1. Store created within request');
}
req[REQPROP] = createStore(); // GIP/GSSP
} else if (config.debug) {
console.log('1. Store reused from request');
}
return req[REQPROP];
}
if (config.debug) {
console.log('1. Store created without request');
}
return createStore(); // GSP or server rendering
}
// Memoize the store if we're on the client
if (!sharedClientStore) {
if (config.debug) {
console.log('1. Store created on client');
}
sharedClientStore = createStore();
}
return sharedClientStore;
};
/**
* Returns an array of tuples with state and source. An array is needed to guarantee the order.
* Returns an empty array if it does not recognize state
*
* If GSP has run, then state will _not_ contain the data from GIAP (if it exists), because GSP is run at build
* time, and GIAP runs at request time.
*
* So we have to hydrate the GSP data first, and then do another hydrate on the GIAP state.
*
* If GSSP has run, then state _will_ contain the data from GIAP (if there is a GIAP) and the GSSP data combined
* (see https://github.com/kirill-konshin/next-redux-wrapper/pull/499#discussion_r1014500941), thus one hydrate.
* !!! Not applied anymore since we flush log after dispatches.
*
* If there is no GSP or GSSP for this page, but there is a GIPP (not _app), there will be one hydrate.
*
* If there is no GSP or GSSP and no GIP on page level for this page, but there is a GIAP on _app level there
* will be also one hydrate.
*
* GIPP (partial) -> GIAP (full)
* GIAP (partial) -> GSSP (full)
* GIAP (partial) -> GSP (partial)
*/
const getStates = ({
reduxWrapperActionsGSSP,
reduxWrapperActionsGSP,
reduxWrapperActionsGIAP,
reduxWrapperActionsGIPP,
}: PageProps): Map<string, Action[]> => {
const map = new Map();
if (reduxWrapperActionsGIAP) {
if (reduxWrapperActionsGIPP) {
// send both as they both are partial, order is important as GIPP happens before GIAP
map.set(Source.GIPP, reduxWrapperActionsGIPP);
map.set(Source.GIAP, reduxWrapperActionsGIAP);
} else if (reduxWrapperActionsGSSP) {
// send both as they both are partial, order is important as GIAP happens before GSSP
map.set(Source.GIAP, reduxWrapperActionsGIAP);
map.set(Source.GSSP, reduxWrapperActionsGSSP);
} else if (reduxWrapperActionsGSP) {
// send both as they both are partial, order is important as GSP happens way before GIAP
map.set(Source.GSP, reduxWrapperActionsGSP);
map.set(Source.GIAP, reduxWrapperActionsGIAP);
} else {
map.set(Source.GIAP, reduxWrapperActionsGIAP); // simply return GIAP
}
} else if (reduxWrapperActionsGSP) {
map.set(Source.GSP, reduxWrapperActionsGSP);
} else if (reduxWrapperActionsGSSP) {
map.set(Source.GSSP, reduxWrapperActionsGSSP);
} else if (reduxWrapperActionsGIPP) {
map.set(Source.GIPP, reduxWrapperActionsGIPP);
}
return map;
};
const dispatchStates = (dispatch: Dispatch, states: PageProps, config: Config<any>) =>
getStates(states).forEach((actions, source) =>
getDeserialized(actions, config).forEach(action =>
dispatch({
...action,
meta: {...(action as any).meta, source},
}),
),
);
export const createWrapper = <S extends Store>(makeStore: MakeStore<S>, config: Config<S> = {}) => {
const makeProps = async function <P extends Object>({
callback,
context,
}: {
callback: Callback<S, P>;
context: any;
}): Promise<WrapperProps<any>> {
const {store, log} = initStore({context, makeStore, config});
const nextCallback = callback && callback(store);
const initialProps = ((nextCallback && (await nextCallback(context))) || {}) as P;
if (config.debug) {
console.log(`2. initial state after dispatches`, store.getState(), 'actions', log);
}
const reduxWrapperActions = getSerialized([...log], config).map(replaceUndefinedWithNull); // serialize then replace undefined just in case
log.splice(0, log.length); // flush all logs
return {
initialProps,
reduxWrapperActions,
};
};
const getInitialPageProps =
<P extends {} = any>(callback: PageCallback<S, P>): ReturnType<PageCallback<S, P>> =>
async (
context: NextPageContext | any, // legacy
) => {
if (!context.query || !context.pathname || !context.AppTree) {
throw new Error(`Looks like you've used getInitialPageProps for different kind of lifecycle method`);
}
const {reduxWrapperActions, initialProps} = await makeProps({callback, context});
return {
...initialProps,
reduxWrapperActionsGIPP: reduxWrapperActions,
};
};
const getInitialAppProps =
<P extends {} = any>(callback: AppCallback<S, P>): ReturnType<AppCallback<S, P>> =>
async (context: AppContext) => {
if (!context.router || !context.Component || !context.AppTree || !context.ctx) {
throw new Error(`Looks like you've used getInitialAppProps for different kind of lifecycle method`);
}
const {reduxWrapperActions, initialProps} = await makeProps({callback, context});
return {
...initialProps,
pageProps: {
...initialProps.pageProps,
reduxWrapperActionsGIAP: reduxWrapperActions,
},
};
};
const getStaticProps =
<P extends {} = any>(callback: GetStaticPropsCallback<S, P>): ReturnType<GetStaticPropsCallback<S, P>> =>
async (context: GetStaticPropsContext) => {
//TODO Check context props to ensure GSP, problem is context has all params optional...
// if (???) throw new Error('Looks like you've used getStaticProps for different kind of lifecycle method');
const {reduxWrapperActions, initialProps} = await makeProps({callback, context});
return {
...initialProps,
props: {
...initialProps.props,
reduxWrapperActionsGSP: reduxWrapperActions,
},
};
};
const getServerSideProps =
<P extends {} = any>(callback: GetServerSidePropsCallback<S, P>): ReturnType<GetServerSidePropsCallback<S, P>> =>
async (context: GetServerSidePropsContext) => {
if (!context.req || !context.res || !context.resolvedUrl || !context.query) {
throw new Error(`Looks like you've used getServerSideProps for different kind of lifecycle method`);
}
const {reduxWrapperActions, initialProps} = await makeProps({callback, context});
return {
...initialProps,
props: {
...initialProps.props,
reduxWrapperActionsGSSP: reduxWrapperActions,
},
};
};
const useStore = () => useMemo<S>(() => initStore<S>({makeStore, config}).store, []);
const useHydration = ({
reduxWrapperActionsGSSP,
reduxWrapperActionsGSP,
reduxWrapperActionsGIAP,
reduxWrapperActionsGIPP,
}: PageProps | any) => {
const dispatch = useDispatch();
const [hydrating, setHydrating] = useState(true);
const hydrate = useCallback(
() =>
dispatchStates(
dispatch,
{
reduxWrapperActionsGSSP,
reduxWrapperActionsGSP,
reduxWrapperActionsGIAP,
reduxWrapperActionsGIPP,
},
config,
),
[dispatch, reduxWrapperActionsGSSP, reduxWrapperActionsGSP, reduxWrapperActionsGIAP, reduxWrapperActionsGIPP],
);
// This guard is solely to suppress Next.js warning about useless layout effect
// Coverage is not gathered, all checks are via demo apps
/* c8 ignore start */
if (!getIsServer()) {
// eslint-disable-next-line react-hooks/rules-of-hooks
useLayoutEffect(() => {
if (!(window as any)[RENDER]) {
(window as any)[RENDER] = true;
return;
}
// if there are only GIAP or GIPP these actions were already dispatched on client side
if (!reduxWrapperActionsGSP && !reduxWrapperActionsGSSP) {
if (config.debug) {
console.log('4. useHydration effect skipped');
}
return;
}
setHydrating(true);
if (config.debug) {
console.log('3. useHydration effect');
}
hydrate();
setHydrating(false);
if (config.debug) {
console.log('4. useHydration done');
}
}, [dispatch, hydrate, reduxWrapperActionsGSP, reduxWrapperActionsGSSP]);
if ((window as any)[RENDER]) {
return {hydrating};
}
}
/*c8 ignore end */
/**
* When we navigate client side, we may always synchronously hydrate the state before the new page components
* are mounted. This means we hydrate while the previous page components are still mounted.
*
* This will cause ugly react message, that you're trying to update state of a component while rendering another.
* Warnings only appear in development mode.
*
* You might think that might cause issues because the selectors on the previous page (still mounted) will suddenly
* contain other data, and maybe even nested properties, causing null reference exceptions.
*
* But that's not the case.
*
* Hydrating synchronously will not trigger a rerender of the still mounted page component. So if your selectors do
* have some initial state values causing them to rerun after hydration, and you're accessing deeply nested values
* inside your components, you still wouldn't get errors, because there's no rerender.
*
* Instead, React will render the new page components straight away, which will have selectors with the correct data.
*
* So technically, other than an ugly warning, this does not have consequences. Nevertheless, subsequent navigation
* events (without reload) will be dispatched from useEffect, properly. This means that for split second selectors
* may render empty data. React may batch state updates though (or not).
*/
if (config.debug) {
console.log('3. useHydration sync');
}
hydrate();
if (config.debug) {
console.log('4. useHydration done');
}
return {hydrating: false};
};
return {
getServerSideProps,
getStaticProps,
getInitialAppProps,
getInitialPageProps,
useStore,
useHydration,
};
};
export type Context = NextPageContext | AppContext | GetStaticPropsContext | GetServerSidePropsContext;
export interface Config<S extends Store> {
serialize?: (actions: Action[]) => Action[];
deserialize?: (actions: Action[]) => Action[];
debug?: boolean;
}
export interface PageProps {
reduxWrapperActionsGIAP?: Action[]; // stuff in the Store state after App.getInitialProps
reduxWrapperActionsGIPP?: Action[]; // stuff in the Store state after Page.getInitialProps
reduxWrapperActionsGSSP?: Action[]; // stuff in the Store state after getServerSideProps
reduxWrapperActionsGSP?: Action[]; // stuff in the Store state after getStaticProps
}
export interface WrapperProps<P extends Object> {
reduxWrapperActions: Action[];
initialProps: P; // stuff returned from getInitialProps or getServerSideProps
}
export type GetStaticPropsCallback<S extends Store, P extends {[key: string]: any}> = (store: S) => GetStaticProps<P>;
export type GetServerSidePropsCallback<S extends Store, P extends {[key: string]: any}> = (store: S) => GetServerSideProps<P>;
export type PageCallback<S extends Store, P> = (store: S) => NextComponentType<NextPageContext, P, P>['getInitialProps'];
export type AppCallback<S extends Store, P> = (store: S) => ({Component, ctx}: AppContext) => Promise<{pageProps: P}> | {pageProps: P}; // AppInitialProps & //FIXME Could be typeof App.getInitialProps & appGetInitialProps (not exported), see https://github.com/kirill-konshin/next-redux-wrapper/issues/412
export type Callback<S extends Store, P extends {[key: string]: any}> =
| GetStaticPropsCallback<S, P>
| GetServerSidePropsCallback<S, P>
| PageCallback<S, P>
| AppCallback<S, P>;