@cocalc/hub
Version:
CoCalc: Backend webserver component
104 lines (98 loc) • 4.07 kB
JavaScript
;
/*
Redirects so that all of the URL's for the old landing
pages work with the new next.js implementation.
The mapping is as follows, with the first match winning:
/index.html --> /
/policies/pricing.html --> /pricing
/policies/index.html --> /policies
/policies/[page].html -> /policies/[page]
/doc/software.html --> /software
/doc/software-[name].html --> /software/[name]
/doc/index.html --> /features
/doc/[page].html --> /features/[page]
This is defined in the "RULES" array below.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const logger_1 = require("@cocalc/hub/logger");
const base_path_1 = __importDefault(require("@cocalc/backend/base-path"));
const CODE = 301; // permanent redirect
const logger = (0, logger_1.getLogger)("landing-redirect");
const RULES = [
{ start: (0, path_1.join)(base_path_1.default, "index.html"), to: base_path_1.default },
{
start: (0, path_1.join)(base_path_1.default, "policies/pricing.html"),
to: (0, path_1.join)(base_path_1.default, "pricing"),
},
{
start: (0, path_1.join)(base_path_1.default, "policies/index.html"),
to: (0, path_1.join)(base_path_1.default, "policies"),
},
{
start: (0, path_1.join)(base_path_1.default, "policies/"),
to: (url) => url.slice(0, url.length - ".html".length),
},
{
start: (0, path_1.join)(base_path_1.default, "doc/software.html"),
to: (0, path_1.join)(base_path_1.default, "/software"),
},
{
start: (0, path_1.join)(base_path_1.default, "doc/software-"),
to: (url) => {
const i = url.lastIndexOf("/doc/software-");
return `${url.slice(0, i)}/software/${url.slice(i + "/doc/software-".length, url.length - ".html".length)}`;
},
},
{ start: (0, path_1.join)(base_path_1.default, "doc/index.html"), to: (0, path_1.join)(base_path_1.default, "/features") },
{
start: (0, path_1.join)(base_path_1.default, "doc/"),
to: (url) => {
const i = url.lastIndexOf("/doc/");
return `${url.slice(0, i)}/features/${url.slice(i + "/doc/".length, url.length - ".html".length)}`;
},
},
];
function redirect() {
const doc = (0, path_1.join)(base_path_1.default, "doc/");
const policies = (0, path_1.join)(base_path_1.default, "policies/");
const index_html = (0, path_1.join)(base_path_1.default, "index.html");
logger.info("creating landing pages legacy redirect");
return async (req, res, next) => {
const { url } = req;
//logger.http("redirect %s", url);
// Check for a quick obvious "no".
if (!url.endsWith(".html") ||
!(url.startsWith(doc) || url.startsWith(policies) || url == index_html)) {
// The url doesn't ended in html or it doesn't start with /doc or /policies,
// so clearly not going to redirect it.
next();
return;
}
// Now the url does start with either doc or policies and ends in .html,
// so we are definitely going to redirect (or fail), since NONE of the new
// nextjs pages end in .html and there's nothing else under /doc or /policies.
for (const rule of RULES) {
if (url.startsWith(rule.start)) {
const { to } = rule;
let dest;
if (typeof to == "string") {
dest = to;
}
else {
dest = to(url);
}
logger.http("landing redirect ", url, " --> ", dest);
res.redirect(CODE, dest);
return;
}
}
logger.http("landing redirect NOT found for ", url);
res.status(404).end();
};
}
exports.default = redirect;
//# sourceMappingURL=landing-redirect.js.map