Skip to content

Commit

Permalink
renderContext fix
Browse files Browse the repository at this point in the history
  • Loading branch information
comus committed Feb 7, 2017
1 parent efe56c2 commit 665547e
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 71 deletions.
6 changes: 5 additions & 1 deletion packages/nova-core/lib/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import './callbacks.js';
// import and re-export
export {
// apollo
createApolloClient, renderContext,
createApolloClient,
// callbacks
Callbacks, addCallback, removeCallback, runCallbacks, runCallbacksAsync,
// components
Expand All @@ -18,6 +18,8 @@ export {
Headtags,
// redux
getActions, addAction, getReducers, addReducer, getMiddlewares, addMiddleware,
// render
renderContext, getRenderContext,
// routes
Routes, addRoute, getRoute, populateRoutesApp,
// settings
Expand All @@ -30,6 +32,8 @@ export {
configureStore,
// mutations (for server only)
newMutation, editMutation, removeMutation,
// ssr (for server only)
ssr, ssrNext
} from 'meteor/nova:lib';

export { default as App } from "./components/App.jsx";
Expand Down
2 changes: 1 addition & 1 deletion packages/nova-core/lib/server/apollo_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const createApolloServer = (givenOptions = {}, givenConfig = {}) => {
}

// This binds the specified paths to the Express server running Apollo + GraphiQL
WebApp.connectHandlers.use(graphQLServer);
WebApp.connectHandlers.use(Meteor.bindEnvironment(graphQLServer));
};

Meteor.startup(function () {
Expand Down
4 changes: 2 additions & 2 deletions packages/nova-lib/lib/client/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import cookie from 'react-cookie';

import { Meteor } from 'meteor/meteor';

import { renderContext } from './render_context.js';
import { getRenderContext } from './render.js';

const context = renderContext.get();
const context = getRenderContext();

function setToken(loginToken, expires) {
if (loginToken && expires !== -1) {
Expand Down
2 changes: 1 addition & 1 deletion packages/nova-lib/lib/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import './auth.js';

export * from '../modules/index.js';
export * from './mongo_redux.js';
export * from './render_context.js';
export * from './render.js';
4 changes: 2 additions & 2 deletions packages/nova-lib/lib/client/mongo_redux.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderContext } from './render_context.js';
import { getRenderContext } from './render.js';

const { store } = renderContext.get();
const { store } = getRenderContext;

// use global store
Mongo.Collection.prototype.findRedux = function (selector = {}, options = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ export const renderContext = {
return context;
},
};

export const getRenderContext = () => renderContext.get();
2 changes: 1 addition & 1 deletion packages/nova-lib/lib/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import './oauth_config.js';
export * from '../modules/index.js';
export * from './store.js';
export * from './mutations.js';
export * from './render_context.js';
export * from './render.js';
76 changes: 76 additions & 0 deletions packages/nova-lib/lib/server/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Meteor } from 'meteor/meteor';
import { DDP } from 'meteor/ddp';
import { Accounts } from 'meteor/accounts-base';

import { createApolloClient, getReducers, getMiddlewares } from '../modules/index.js';
import { configureStore } from './store.js';

const Fiber = Npm.require('fibers');

export const renderContext = new Meteor.EnvironmentVariable();

export const getRenderContext = () => renderContext.get();

const LoginContext = function LoginContext(loginToken) {
this._loginToken = loginToken;

// get the user
if (Meteor.users) {
// check to make sure, we've the loginToken,
// otherwise a random user will fetched from the db
let user;
if (loginToken) {
const hashedToken = loginToken && Accounts._hashLoginToken(loginToken);
const query = { 'services.resume.loginTokens.hashedToken': hashedToken };
const options = { fields: { _id: 1 } };
user = Meteor.users.findOne(query, options);
}

if (user) {
this.userId = user._id;
}
}
};

export const ssr = (func, options = {}) => {
const newFunc = Meteor.bindEnvironment((req, res, next) => {
req.loginToken = req.loginToken || (req.cookies && req.cookies.meteor_login_token);
req.apolloClient = req.apolloClient || createApolloClient({ currentUserToken: req.loginToken });
req.reducers = req.reducers || { ...getReducers(), apollo: req.apolloClient.reducer() };
req.middlewares = req.middlewares || [...getMiddlewares(), req.apolloClient.middleware()];
req.store = req.store || configureStore(req.reducers, {}, req.middlewares);
req.loginContext = req.loginContext || new LoginContext(req.loginToken);
req.renderContext = req.renderContext || {
loginToken: req.loginToken,
apolloClient: req.apolloClient,
reducers: req.reducers,
middlewares: req.middlewares,
store: req.store,
};

Fiber.current._meteor_dynamics = Fiber.current._meteor_dynamics || [];
Fiber.current._meteor_dynamics[DDP._CurrentInvocation.slot] = req.loginContext;
Fiber.current._meteor_dynamics[renderContext.slot] = req.renderContext;

func(req, res, next);

if (options.autoNext) {
next();
}
})

if (options.name) {
Object.defineProperty(newFunc, 'name', { value: options.name });
}
WebApp.connectHandlers.use(newFunc);
}

export const ssrNext = (func) => {
ssr(func, { autoNext: true });
}

ssr((req, res, next) => {
// initialize
// console.log(Fiber.current)
next();
});
53 changes: 0 additions & 53 deletions packages/nova-lib/lib/server/render_context.js

This file was deleted.

8 changes: 4 additions & 4 deletions packages/nova-routing/lib/client/routing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
addRoute,
addReducer, addMiddleware,
Routes, populateComponentsApp, populateRoutesApp, runCallbacks,
renderContext,
getRenderContext,
} from 'meteor/nova:core';

import { RouterClient } from './router.jsx';
Expand All @@ -37,7 +37,7 @@ Meteor.startup(() => {
const options = {
rehydrateHook(data) {
const initialState = data;
const context = renderContext.get();
const context = getRenderContext();
context.initialState = initialState;

// configure apollo
Expand All @@ -50,13 +50,13 @@ Meteor.startup(() => {
store.reload();
},
historyHook(newHistory) {
const context = renderContext.get();
const context = getRenderContext();
const history = newHistory;
context.history = history;
return history;
},
wrapperHook(appGenerator) {
const { apolloClient, store } = renderContext.get();
const { apolloClient, store } = getRenderContext();
const app = appGenerator({
onUpdate: () => {
// the first argument is an item to iterate on, needed by nova:lib/callbacks
Expand Down
19 changes: 17 additions & 2 deletions packages/nova-routing/lib/server/router.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import cookieParser from 'cookie-parser';
import { RoutePolicy } from 'meteor/routepolicy';
import { WebApp } from 'meteor/webapp';

import { ssr, ssrNext } from 'meteor/nova:core';

import { InjectData } from './inject_data.js';

function isAppUrl(req) {
Expand Down Expand Up @@ -114,7 +116,20 @@ export const RouterServer = {

WebApp.rawConnectHandlers.use(cookieParser());

WebApp.connectHandlers.use((req, res, next) => {
// Ensure router middleware is at the end
ssrNext(() => {
const stack = WebApp.connectHandlers.stack;
if (stack[stack.length - 1].handle.name === 'routerMiddleware') {
return;
}
for (let i in stack) {
if (stack[i].handle.name === 'routerMiddleware') {
stack.push(stack.splice(i, 1)[0]);
}
}
});

ssr((req, res, next) => {
if (!isAppUrl(req)) {
next();
return;
Expand Down Expand Up @@ -142,6 +157,6 @@ export const RouterServer = {
res.end();
}
});
});
}, { name: 'routerMiddleware' });
},
};
8 changes: 4 additions & 4 deletions packages/nova-routing/lib/server/routing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Components,
addRoute,
Routes, populateComponentsApp, populateRoutesApp,
renderContext,
getRenderContext,
} from 'meteor/nova:core';

import { RouterServer } from './router.jsx';
Expand All @@ -36,20 +36,20 @@ Meteor.startup(() => {
const options = {
historyHook(req, res, newHistory) {
req.history = newHistory;
const context = renderContext.get();
const context = getRenderContext();
context.history = req.history;
return req.history;
},
wrapperHook(req, res, appGenerator) {
const { apolloClient, store } = renderContext.get();
const { apolloClient, store } = getRenderContext();
const app = appGenerator();
return <ApolloProvider store={store} client={apolloClient}>{app}</ApolloProvider>;
},
preRender(req, res, app) {
return Promise.await(getDataFromTree(app));
},
dehydrateHook(req, res) {
const context = renderContext.get();
const context = getRenderContext();
return context.apolloClient.store.getState();
},
postRender(req, res) {
Expand Down

0 comments on commit 665547e

Please sign in to comment.