| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | import debug from 'debug'; | 
					
						
							| 
									
										
										
										
											2018-08-31 16:04:04 +01:00
										 |  |  | // import { renderToString } from 'react-dom/server';
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  | // import createMemoryHistory from 'history/createMemoryHistory';
 | 
					
						
							|  |  |  | // import { NOT_FOUND } from 'redux-first-router';
 | 
					
						
							|  |  |  | // import devtoolsEnhancer from 'remote-redux-devtools';
 | 
					
						
							| 
									
										
										
										
											2016-10-29 00:46:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  | // import {
 | 
					
						
							|  |  |  | //   errorThrowerMiddleware
 | 
					
						
							|  |  |  | // } from '../utils/react.js';
 | 
					
						
							|  |  |  | // import { createApp, provideStore, App } from '../../common/app';
 | 
					
						
							|  |  |  | // import waitForEpics from '../../common/utils/wait-for-epics.js';
 | 
					
						
							|  |  |  | // import { titleSelector } from '../../common/app/redux';
 | 
					
						
							| 
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | const log = debug('fcc:react-server'); | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  | // const isDev = process.env.NODE_ENV !== 'production';
 | 
					
						
							| 
									
										
										
										
											2015-07-01 15:14:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  | // // add routes here as they slowly get reactified
 | 
					
						
							|  |  |  | // // remove their individual controllers
 | 
					
						
							|  |  |  | // const routes = [
 | 
					
						
							|  |  |  | //   '/settings',
 | 
					
						
							|  |  |  | //   '/settings/*',
 | 
					
						
							|  |  |  | //   '/:username'
 | 
					
						
							|  |  |  | // ];
 | 
					
						
							| 
									
										
										
										
											2015-10-19 23:16:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  | // const devRoutes = [];
 | 
					
						
							| 
									
										
										
										
											2015-12-22 19:28:07 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  | // const middlewares = isDev ? [errorThrowerMiddleware] : [];
 | 
					
						
							| 
									
										
										
										
											2018-05-04 19:35:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  | // const markupMap = {};
 | 
					
						
							| 
									
										
										
										
											2018-05-04 19:35:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-29 12:01:56 -07:00
										 |  |  | export default function reactSubRouter(app) { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   // var router = app.loopback.Router();
 | 
					
						
							| 
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   // router.get('/videos', (req, res) => res.redirect('/map'));
 | 
					
						
							|  |  |  |   // router.get(
 | 
					
						
							|  |  |  |   //   '/videos/:dashedName',
 | 
					
						
							|  |  |  |   //   (req, res) => res.redirect(`/challenges/${req.params.dashedName}`)
 | 
					
						
							|  |  |  |   // );
 | 
					
						
							| 
									
										
										
										
											2016-06-17 12:35:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   // router.get(
 | 
					
						
							|  |  |  |   //   '/portfolio/:redirectUsername',
 | 
					
						
							|  |  |  |   //   (req, res) => res.redirect(`/${req.params.redirectUsername}`)
 | 
					
						
							|  |  |  |   // );
 | 
					
						
							| 
									
										
										
										
											2018-06-07 17:45:30 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   // // These routes are in production
 | 
					
						
							|  |  |  |   // routes.forEach((route) => {
 | 
					
						
							|  |  |  |   //   router.get(route, serveReactApp);
 | 
					
						
							|  |  |  |   // });
 | 
					
						
							| 
									
										
										
										
											2015-10-19 23:16:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   // if (process.env.NODE_ENV === 'development') {
 | 
					
						
							|  |  |  |   //   devRoutes.forEach(function(route) {
 | 
					
						
							|  |  |  |   //     router.get(route, serveReactApp);
 | 
					
						
							|  |  |  |   //   });
 | 
					
						
							|  |  |  |   // }
 | 
					
						
							| 
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   // app.use(router);
 | 
					
						
							| 
									
										
										
										
											2015-06-29 09:50:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   // function serveReactApp(req, res, next) {
 | 
					
						
							|  |  |  |   //   const serviceOptions = { req };
 | 
					
						
							|  |  |  |   //   if (req.originalUrl in markupMap) {
 | 
					
						
							|  |  |  |   //     log('sending markup from cache');
 | 
					
						
							|  |  |  |   //     const { state, title, markup } = markupMap[req.originalUrl];
 | 
					
						
							|  |  |  |   //     res.expose(state, 'data', { isJSON: true });
 | 
					
						
							|  |  |  |   //     // note(berks): we render without express-flash dumping our messages
 | 
					
						
							|  |  |  |   //     // the app will query for these on load
 | 
					
						
							|  |  |  |   //     return res.renderWithoutFlash('layout-react', { markup, title });
 | 
					
						
							|  |  |  |   //   }
 | 
					
						
							|  |  |  |   //   return createApp({
 | 
					
						
							|  |  |  |   //     serviceOptions,
 | 
					
						
							|  |  |  |   //     middlewares,
 | 
					
						
							|  |  |  |   //     enhancers: [
 | 
					
						
							|  |  |  |   //       devtoolsEnhancer({ name: 'server' })
 | 
					
						
							|  |  |  |   //     ],
 | 
					
						
							|  |  |  |   //     history: createMemoryHistory({ initialEntries: [ req.originalUrl ] }),
 | 
					
						
							|  |  |  |   //     defaultState: {}
 | 
					
						
							|  |  |  |   //   })
 | 
					
						
							|  |  |  |   //     .filter(({
 | 
					
						
							|  |  |  |   //       location: {
 | 
					
						
							|  |  |  |   //         type,
 | 
					
						
							|  |  |  |   //         kind,
 | 
					
						
							|  |  |  |   //         pathname
 | 
					
						
							|  |  |  |   //       } = {}
 | 
					
						
							|  |  |  |   //     }) => {
 | 
					
						
							|  |  |  |   //       if (kind === 'redirect') {
 | 
					
						
							|  |  |  |   //         log('react found a redirect');
 | 
					
						
							|  |  |  |   //         res.redirect(pathname);
 | 
					
						
							|  |  |  |   //         return false;
 | 
					
						
							|  |  |  |   //       }
 | 
					
						
							| 
									
										
										
										
											2016-01-03 19:40:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   //       if (type === NOT_FOUND) {
 | 
					
						
							|  |  |  |   //         log(`react tried to find ${req.path} but got 404`);
 | 
					
						
							|  |  |  |   //         next();
 | 
					
						
							|  |  |  |   //         return false;
 | 
					
						
							|  |  |  |   //       }
 | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:29:26 +01:00
										 |  |  |   //       return true;
 | 
					
						
							|  |  |  |   //     })
 | 
					
						
							|  |  |  |   //     .flatMap(({ store, epic }) => {
 | 
					
						
							|  |  |  |   //       return waitForEpics(epic)
 | 
					
						
							|  |  |  |   //         .map(() => renderToString(
 | 
					
						
							|  |  |  |   //           provideStore(App, store)
 | 
					
						
							|  |  |  |   //         ))
 | 
					
						
							|  |  |  |   //         .map((markup) => ({ markup, store, epic }));
 | 
					
						
							|  |  |  |   //     })
 | 
					
						
							|  |  |  |   //     .do(({ markup, store, epic }) => {
 | 
					
						
							|  |  |  |   //       log('react markup rendered, data fetched');
 | 
					
						
							|  |  |  |   //       const state = store.getState();
 | 
					
						
							|  |  |  |   //       const title = titleSelector(state);
 | 
					
						
							|  |  |  |   //       epic.dispose();
 | 
					
						
							|  |  |  |   //       res.expose(state, 'data', { isJSON: true });
 | 
					
						
							|  |  |  |   //       // note(berks): we render without express-flash dumping our messages
 | 
					
						
							|  |  |  |   //       // the app will query for these on load
 | 
					
						
							|  |  |  |   //       res.renderWithoutFlash('layout-react', { markup, title });
 | 
					
						
							|  |  |  |   //       markupMap[req.originalUrl] = { markup, state, title };
 | 
					
						
							|  |  |  |   //     })
 | 
					
						
							|  |  |  |   //     .subscribe(() => log('html rendered and sent'), next);
 | 
					
						
							|  |  |  |   // }
 | 
					
						
							| 
									
										
										
										
											2015-06-29 12:01:56 -07:00
										 |  |  | } |