Skip to content

Commit

Permalink
better SSR content loading by defering and logging error (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip authored Jun 20, 2018
1 parent 48dc459 commit 04fb81a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
6 changes: 4 additions & 2 deletions packages/electrode-react-webapp/lib/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const registerRoutes = (app, options, next = () => {}) => {
const routeOptions = _.defaults({ htmlFile: v.htmlFile }, registerOptions);
const routeHandler = ReactWebapp.makeRouteHandler(routeOptions);
const handleRoute = options.handleRoute || DefaultHandleRoute;
const content = resolveContent();
let content;

/*eslint max-nested-callbacks: [0, 4]*/
let methods = v.method || ["GET"];
Expand All @@ -58,7 +58,9 @@ const registerRoutes = (app, options, next = () => {}) => {
if (method === "*") {
method = "ALL";
}
app[method.toLowerCase()](path, (req, res) => handleRoute(req, res, routeHandler, content));
app[method.toLowerCase()](path, (req, res) =>
handleRoute(req, res, routeHandler, content || (content = resolveContent()))
);
});
});

Expand Down
5 changes: 3 additions & 2 deletions packages/electrode-react-webapp/lib/hapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ const registerRoutes = (server, options) => {
const routeOptions = ReactWebapp.setupPathOptions(registerOptions, path);
const routeHandler = ReactWebapp.makeRouteHandler(routeOptions);
const handleRoute = options.handleRoute || DefaultHandleRoute;
const content = resolveContent();
let content;

server.route({
method: pathData.method || "GET",
path,
config: pathData.config || {},
handler: (req, reply) => handleRoute(req, reply, routeHandler, content)
handler: (req, reply) =>
handleRoute(req, reply, routeHandler, content || (content = resolveContent()))
});
});
};
Expand Down
4 changes: 2 additions & 2 deletions packages/electrode-react-webapp/lib/koa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const registerRoutes = (router, options) => {
const routeOptions = _.defaults({ htmlFile: v.htmlFile }, registerOptions);
const routeHandler = ReactWebapp.makeRouteHandler(routeOptions);
const handleRoute = options.handleRoute || DefaultHandleRoute;
const content = resolveContent();
let content;

let methods = v.method || ["GET"];
if (!Array.isArray(methods)) {
Expand All @@ -69,7 +69,7 @@ const registerRoutes = (router, options) => {

/*eslint max-nested-callbacks: [0, 4]*/
router(method.toLowerCase(), path, function() {
return handleRoute.call(this, routeHandler, content);
return handleRoute.call(this, routeHandler, content || (content = resolveContent()));
}); //end get
});
});
Expand Down
8 changes: 7 additions & 1 deletion packages/electrode-react-webapp/lib/react-webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ const resolveContent = (content, xrequire) => {
? Path.join(process.cwd(), content.module)
: content.module;
xrequire = xrequire || require;
return xrequire(module);
try {
return xrequire(module);
} catch (err) {
const msg = `electrode-react-webapp: load SSR content ${module} failed`;
console.error(msg, err); // eslint-disable-line
return msg;
}
}

return content;
Expand Down
3 changes: 3 additions & 0 deletions packages/electrode-react-webapp/test/data/bad-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */

require("foo-blah");
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Path = require("path");
const reactWebapp = require("../../lib/react-webapp");
const xstdout = require("xstdout");

describe("react-webapp", function() {
describe("resolveContent", function() {
Expand All @@ -10,6 +11,13 @@ describe("react-webapp", function() {
expect(reactWebapp.resolveContent({ module: f })).to.equal("hello");
});

it("should log error if resolving content fail", () => {
const intercept = xstdout.intercept(true);
const f = "./test/data/bad-content.js";
expect(reactWebapp.resolveContent({ module: f })).includes("test/data/bad-content.js failed");
intercept.restore();
expect(intercept.stderr.join("")).includes("Error: Cannot find module 'foo-blah'");
});
it("should require module", () => {
let mod;
const fooRequire = x => (mod = x);
Expand Down

0 comments on commit 04fb81a

Please sign in to comment.