shell
- This example does:
- demonstrate how server side versioning works
- use app/1.0, app/2.0 and app/3.0 as the previous, current and upcoming build respectively.
1a. Prepare app/1.0, app/2.0 and app/3.0 build
- npm
- yarn
npm run shell:all
yarn shell:all
1a. Prepare shell build
- npm
- yarn
npm run shell
yarn shell
2. Check index.js
examples/shell/index.js
const http = require("http");
const path = require("path");
const polka = require("polka");
const serveStatic = require("serve-static");
const url = require("url");
const {ReactSSREntry} = require("../../src/ReactSSREntry");
const workdir = path.resolve(process.cwd(), process.argv.slice(2)[0] || ".");
console.log(`The shell on the url "http://localhost:8080" is running for the directory "${workdir}" ...`);
[[http.createServer(), [process.env.PORT || 8080]]].forEach(([server, args]) => {
polka({
server,
})
.use(serveStatic(workdir))
.get(
"*",
async (req, res) => {
const reqUrl = url.parse(req.url, true);
// If you have your own web framework,
// you will need to use ReactSSREntry
// to get server side versioning working
const {statusCode, body} = await ReactSSREntry({
"require": __non_webpack_require__,
workdir,
"props": {"url": reqUrl},
"url": reqUrl,
"version": reqUrl.query.version || "default",
});
res.writeHead(
statusCode,
{
"Content-Type": body.startsWith("<!DOCTYPE html>") ? "text/html" : "text/plain",
}
);
res.end(body);
}
)
.listen(...args);
});
3. Start shell
- npm
- yarn
npm run shell:start
yarn shell:start
4a. Inspect the current build
note
By default, both http://127.0.0.1:8080/a.node and http://127.0.0.1:8080/b.node are using app/2.0
2.0.production.js
is copied and named asdefault.js
.package.json"shell:all": "yarn app:1.0 && yarn app:2.0 && yarn app:3.0 && cp ./examples/app/dist/2.0.production.js ./examples/app/dist/default.js",
4b. Revisit the previous build
note
- http://127.0.0.1:8080/a.node?version=1.0.production uses app/1.0 and http://127.0.0.1:8080/b.node?version=1.0.production returns 404
4c. Preview the upcoming build
note
tip
Now you can travel different builds without a second deployment or restart.
caution
This shell does not have a mechanism to release the outdated server entry file in the require cache. You need to think about when and how or just simpily restart the node regularly.
warning
This shell does not have an access control for the server builds and the manifest JSON. Hackers can download your server builds.
- Do not hard-coded any secret in the app.
- You can add some rules to the proxy to block all external request to e.g.
development.*.json
,production.*.json
and*.node.js