|
1 | 1 | import Promise from "promise";
|
2 | 2 | import React from "react";
|
3 |
| -import { StaticRouter, matchPath } from "react-router-dom"; |
| 3 | +import { StaticRouter as Router, matchPath } from "react-router-dom"; |
4 | 4 | import { renderToString } from "react-dom/server";
|
5 |
| -//import { initialize } from "freactal/lib/server"; |
6 |
| -// import our main App component |
7 | 5 | import { App } from "components/notabug";
|
8 | 6 | import { routes } from "components/notabug/routes";
|
9 |
| -//import { provideState } from "freactal"; |
10 |
| -import { calculateListing } from "./listings"; |
11 | 7 | import init from "notabug-peer";
|
12 | 8 | import serialize from "serialize-javascript";
|
13 | 9 |
|
14 | 10 | const path = require("path");
|
15 |
| -const fs = require("fs"); |
| 11 | +const { readFile } = require("fs"); |
16 | 12 |
|
17 |
| -export default (nab, req, res) => { |
18 |
| - // point to the html file created by CRA"s build tool |
19 |
| - const filePath = path.resolve(__dirname, "..", "htdocs", "index.html"); |
20 |
| - fs.readFile(filePath, "utf8", (err, htmlData) => { |
21 |
| - if (err) { |
22 |
| - console.error("err", err); |
23 |
| - return res.status(404).end(); |
24 |
| - } |
| 13 | +const calculateListing = (nab, req, routeMatch, scope) => { |
| 14 | + let params; |
| 15 | + const opId = req.params.opId || (routeMatch && routeMatch.params.submission_id); |
25 | 16 |
|
26 |
| - let routeMatch; |
27 |
| - const route = routes.find(route => { |
28 |
| - const match = matchPath(req.path, route); |
29 |
| - if (match) routeMatch = match; |
30 |
| - return match; |
31 |
| - }); |
| 17 | + if (opId) { |
| 18 | + params = { submissionId: opId, sort: "new" }; |
| 19 | + } else { |
| 20 | + const count = parseInt(req.query.count || 0, 10); |
| 21 | + const topic = req.params.topic || (routeMatch && routeMatch.params.topic) || "all"; |
| 22 | + const topics = [topic]; |
| 23 | + const sort = req.params.sort || (routeMatch && routeMatch.params.sort); |
| 24 | + const days = parseInt(req.query.days, 10); |
| 25 | + const limit = parseInt(req.query.limit, 10) || 25; |
| 26 | + params = { topics, sort, days, count, limit }; |
| 27 | + } |
32 | 28 |
|
33 |
| - if (!route) { |
34 |
| - return res.status(404).end(); |
35 |
| - } |
36 |
| - |
37 |
| - route.getStaticPeer = () => { |
38 |
| - const staticPeer = init({ noGun: true, localStorage: false, disableValidation: true }); |
39 |
| - return calculateListing(nab, req, routeMatch) |
40 |
| - .then(staticPeer.loadState).then(() => staticPeer); |
41 |
| - }; |
| 29 | + return nab.scopedListing({ scope: scope || nab.newScope() }).withData.query(params); |
| 30 | +}; |
42 | 31 |
|
43 |
| - return Promise.resolve(route && route.getStaticPeer && route.getStaticPeer(routeMatch)) |
44 |
| - .then(staticPeer => { |
45 |
| - try { |
46 |
| - const context={}; |
47 |
| - const html = renderToString(( |
48 |
| - <StaticRouter context={context} location={req.url}> |
49 |
| - <App notabugApi={staticPeer} /> |
50 |
| - </StaticRouter> |
51 |
| - )); |
52 |
| - // inject the rendered app into our html and send it |
53 |
| - console.log("server rendered", req.url); |
54 |
| - const stateScript = ` |
| 32 | +export default (nab, req, res) => readFile( |
| 33 | + path.resolve(__dirname, "..", "htdocs", "index.html"), "utf8", |
| 34 | + (err, htmlData) => { |
| 35 | + if (err) return console.error("err", err) || res.status(404).end(); |
| 36 | + let routeMatch; |
| 37 | + const isJson = (/\.json$/.test(req.path)); |
| 38 | + const urlpath = (isJson ? req.path.replace(/\.json$/, "") : req.path).replace(/^\/api/, ""); |
| 39 | + const url = isJson ? req.url.replace(req.path, urlpath) : req.url; |
| 40 | + const route = routes.find(route => routeMatch = matchPath(urlpath, route)); |
| 41 | + if (!route) return res.status(404).end(); |
| 42 | + const staticPeer = init({ noGun: true, localStorage: false, disableValidation: true }); |
| 43 | + const scope = staticPeer.scope = nab.newScope({ isCacheing: true }); |
| 44 | + route.query = () => calculateListing(nab, req, routeMatch, scope).then(() => staticPeer); |
| 45 | + return Promise.resolve(route.query && route.query(routeMatch)).then(() => { |
| 46 | + const html = renderToString(( |
| 47 | + <Router context={{}} location={url}><App notabugApi={staticPeer} /></Router> |
| 48 | + )); |
| 49 | + console.log("server rendered", isJson ? "json" : "html", url); |
| 50 | + if (isJson) return res.send(staticPeer.scope.getCache()); |
| 51 | + const stateScript = ` |
55 | 52 | <script type="text/javascript">
|
56 |
| -window.initNabState = ${serialize(staticPeer.getState(), { isJSON: true })}; |
| 53 | +window.initNabState = ${serialize(staticPeer.scope.getCache(), { isJSON: true })}; |
57 | 54 | </script>
|
58 |
| - `; |
59 |
| - const parts = htmlData.split("!!!CONTENT!!!"); |
60 |
| - const result = [parts[0], html, stateScript, parts[1]].join(""); |
61 |
| - return res.send(result); |
62 |
| - } catch (e) { |
63 |
| - console.error("error generating page", (e && e.stack) || e); |
64 |
| - res.send(htmlData.replace("!!!CONTENT!!!", "<noscript>Something Broke</noscript>")); |
65 |
| - } |
66 |
| - }); |
| 55 | + `; |
| 56 | + const parts = htmlData.split("!!!CONTENT!!!"); |
| 57 | + const result = [parts[0], html, stateScript, parts[1]].join(""); |
| 58 | + return res.send(result); |
| 59 | + }).catch(e => { |
| 60 | + console.error("error generating page", (e && e.stack) || e); |
| 61 | + res.status(500).end(); |
| 62 | + // res.send(htmlData.replace("!!!CONTENT!!!", "<noscript>Something Broke</noscript>")); |
| 63 | + }); |
67 | 64 | });
|
68 |
| -}; |
0 commit comments