CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/share/init.js
Views: 687
1
/*
2
IMPORTANT: to use this from packages/hub (say), it's critical that
3
packages/hub *also* have its own copy of next installed.
4
Otherwise, you'll see an error about
5
6
"Parsing error: Cannot find module 'next/babel'"
7
8
This is mentioned here, and it's maybe a bug in next?
9
https://www.gitmemory.com/issue/vercel/next.js/26127/862661818
10
*/
11
12
const { join } = require("path");
13
const next = require("next");
14
const conf = require("../next.config");
15
const getLogger = require("@cocalc/backend/logger").default;
16
17
async function init({ basePath }) {
18
const winston = getLogger("share-server:init");
19
20
// dev = Whether or not to run in dev mode. This features hot module reloading,
21
// but navigation between pages and serving pages is much slower.
22
const dev = process.env.NODE_ENV != "production";
23
24
// We do this to ensure that our config is like when
25
// running things directly (via npm run dev), without having
26
// to set the BASE_PATH env variable, which might have
27
// a strange impact somewhere else in CoCalc.
28
conf.basePath = basePath == "/" ? "" : basePath; // won't happen since is "../share".
29
conf.env.BASE_PATH = basePath;
30
31
winston.info(
32
`creating next.js app with basePath="${basePath}", and dev=${dev}`
33
);
34
const app = next({ dev, conf, dir: join(__dirname, "..") });
35
const handle = app.getRequestHandler();
36
winston.info("preparing next.js app...");
37
await app.prepare();
38
winston.info("ready to handle next.js requests");
39
return (req, res) => {
40
winston.http("req.url %s", req.url);
41
handle(req, res);
42
};
43
}
44
45
module.exports = init;
46
47