@maskedeng-tom/ssrsx
Version:
server side renderer with tsx
62 lines • 2.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Route = void 0;
const jsx_runtime_1 = require("ssrsxjsx/jsx-runtime");
const support_1 = require("../server/support");
const joinPath_1 = require("./lib/joinPath");
const matchPath_1 = require("./lib/matchPath");
const addSlash_1 = require("./lib/addSlash");
////////////////////////////////////////////////////////////////////////////////
const Route = ({ path, sensitive, element, children, _ssrsxFunctionContext }) => {
var _a, _b;
const parseContext = (0, support_1.getParseContext)('router');
const server = (0, support_1.getServer)();
// resolved
if ((_a = parseContext.routes) === null || _a === void 0 ? void 0 : _a.resolved) {
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {});
}
// all
if (path === '*') {
if (parseContext.routes) {
parseContext.routes.resolved = true;
}
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children !== null && children !== void 0 ? children : element });
}
// get url
const url = (0, support_1.getPathname)(server);
// base path
const base = parseContext.matched;
// test match
const match = (0, matchPath_1.matchPath)((0, addSlash_1.removeFirstSlash)(path), url.slice(base.length), { sensitive: sensitive !== null && sensitive !== void 0 ? sensitive : false });
if (!match.match && !match.subMatch) {
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {});
}
// update matched
if (match.match) {
parseContext.matched = url;
if (parseContext.routes) {
parseContext.routes.resolved = true;
}
}
else if (match.subMatch) {
parseContext.matched = (0, joinPath_1.joinPath)(base, (_b = match.offsetPath) !== null && _b !== void 0 ? _b : '', '/');
}
// set params
Object.assign(parseContext.params, match.params);
// output
const result = (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children !== null && children !== void 0 ? children : element });
if (_ssrsxFunctionContext) {
_ssrsxFunctionContext.finalize = () => {
var _a;
parseContext.matched = base;
if (!((_a = parseContext.routes) === null || _a === void 0 ? void 0 : _a.resolved)) {
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {});
}
};
}
return result;
};
exports.Route = Route;
////////////////////////////////////////////////////////////////////////////////
exports.default = Route;
//# sourceMappingURL=Route.js.map