Skip to content

Commit

Permalink
Upgrade SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
Fi1osof committed Jul 25, 2019
1 parent ffeaa54 commit 4577955
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
4 changes: 0 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ import {App as PrismaApp} from "@prisma-cms/front";

import Renderer from "./components/Renderer";

import * as queryFragments from "./schema/generated/api.fragments";

export {
Renderer,
queryFragments,
}

export default class App extends PrismaApp {

static defaultProps = {
...PrismaApp.defaultProps,
Renderer,
queryFragments,
// lang: "ru",
themeOptions: {
direction: 'ltr',
Expand Down
16 changes: 9 additions & 7 deletions src/components/Renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ import TemplatePage from "@prisma-cms/front-editor/lib/components/pages/Template

import RootPage from "./pages/Root";

import * as queryFragments from "../../schema/generated/api.fragments";

export default class BoilerplateRenderer extends PrismaCmsRenderer {

// static defaultProps = {
// ...PrismaCmsRenderer.defaultProps,
// queryFragments,
// }
static defaultProps = {
...PrismaCmsRenderer.defaultProps,
queryFragments,
}


renderMenu() {
Expand Down Expand Up @@ -242,9 +244,9 @@ export default class BoilerplateRenderer extends PrismaCmsRenderer {
queryFragments,
} = this.props;

// console.log("queryFragments", queryFragments);

// return "Sdfsdf";
if(!queryFragments) {
return null;
}

return <Context.Consumer>
{context => {
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PrismaCms from "@prisma-cms/front";
import * as serviceWorker from './serviceWorker';

import App, {
queryFragments,
// queryFragments,
} from "./App";

import {
Expand All @@ -35,9 +35,12 @@ if (node) {
${UserNoNestingFragment}
`,
}}
queryFragments={queryFragments}
// queryFragments={queryFragments}
/>, node);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

}
Expand Down
6 changes: 3 additions & 3 deletions src/serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// resources are updated in the background.

// To learn more about the benefits of this model and instructions on how to
// opt-in, read http://bit.ly/CRA-PWA
// opt-in, read https://bit.ly/CRA-PWA

const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
Expand Down Expand Up @@ -43,7 +43,7 @@ export function register(config) {
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit http://bit.ly/CRA-PWA'
'worker. To learn more, visit https://bit.ly/CRA-PWA'
);
});
} else {
Expand Down Expand Up @@ -71,7 +71,7 @@ function registerValidSW(swUrl, config) {
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See http://bit.ly/CRA-PWA.'
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
);

// Execute callback
Expand Down
7 changes: 7 additions & 0 deletions src/ssr/SSR/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ class Server {
// result = result.replace(`<body><div id="root"></div>`, `<body><div id="root">${content}</div>`);
// result = result.replace(`<body><div id="root"></div>`, `<body><div id="root">${content}</div>${apiSchema}`);
result = result.replace(`<div id="root"></div>`, `<div id="root">${content || ""}</div>${apolloState || ""}${apiSchema || ""}`);
// result = result.replace(`<div id="root"></div>`, `<div id="root">${"content" || ""}</div>${apolloState || ""}${apiSchema || ""}`);
// result = result.replace(`<div id="root"></div>`, `<div id="root">${content || ""}</div>${"apolloState" || ""}${apiSchema || ""}`);
// result = result.replace(`<div id="root"></div>`, `<div id="root">${content || ""}</div>${"apolloState" || ""}`);

this.timeLog("renderHTML", "replace root content end");

Expand Down Expand Up @@ -654,7 +657,11 @@ class Server {

});
// res.end(`<!doctype html>\n${output}`);

this.timeLog("renderHTML then", "res.end start");
res.end(output);
// res.end("output");
this.timeLog("renderHTML then", "res.end end");



Expand Down
48 changes: 44 additions & 4 deletions src/ssr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ require('@babel/register')({

require('@babel/polyfill');

const fs = require("fs");

let SSRmiddlewareClass = require('./SSR');

let SSRmiddleware = new SSRmiddlewareClass({
Expand All @@ -50,17 +52,55 @@ const setupProxy = require("../setupProxy");

setupProxy(app);

app.use('/static', express.static(cwd + '/build/static')); //Serves resources from build folder
app.use('/build', express.static(cwd + '/build')); //Serves resources from build folder
app.use('/public', express.static(cwd + '/public')); //Serves resources from public folder

app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(bodyParser.json()); // to support JSON-encoded bodies


app.get('**', SSRmiddleware);
app.get("/static/js/voyager.worker.js", (req, res, next) => {
res.sendFile(`${cwd}/node_modules/@prisma-cms/graphql-voyager/dist/voyager.worker.js`);
});

/**
* Process static files
*/
app.get("**", (req, res, next) => {

const {
pathname,
} = req._parsedUrl;

if (pathname && pathname !== "/") {

const path = `${cwd}/build${pathname}`;

if (fs.existsSync(path)) {
// Do something
return res.sendFile(path);
}

}

next();
});

app.get('**', (req, res, next) => {

return SSRmiddleware(req, res, next);
});

// app.get('/', express.static(cwd + '/build')); //Serves resources from build folder
// app.use('/static', express.static(cwd + '/build/static')); //Serves resources from build folder
// app.use('/build', express.static(cwd + '/build')); //Serves resources from build folder
// app.use('/public', express.static(cwd + '/public')); //Serves resources from public folder
// app.get('/favicon.ico', express.static(cwd + '/build')); //Serves resources from build folder
// app.get('/manifest.json', express.static(cwd + '/build')); //Serves resources from build folder
// app.get('/service-worker.js', express.static(cwd + '/build')); //Serves resources from build folder




// get the intended port number, use port 3000 if not provided
const port = argv.port || process.env.PORT || 3000;
Expand Down

0 comments on commit 4577955

Please sign in to comment.