@jss-rule-engine/edge
Version:
68 lines (67 loc) • 2.97 kB
JavaScript
var MiddlewareBase = /** @class */ (function () {
function MiddlewareBase(config) {
this.config = config;
this.SITE_SYMBOL = 'sc_site';
this.defaultHostname = config.defaultHostname || 'localhost';
}
/**
* Determines if mode is preview
* @param {NextRequest} req request
* @returns {boolean} is preview
*/
MiddlewareBase.prototype.isPreview = function (req) {
var _a, _b;
return !!(((_a = req.cookies.get('__prerender_bypass')) === null || _a === void 0 ? void 0 : _a.value) || ((_b = req.cookies.get('__next_preview_data')) === null || _b === void 0 ? void 0 : _b.value));
};
MiddlewareBase.prototype.excludeRoute = function (pathname) {
var _a, _b;
return (pathname.startsWith('/api/') || // Ignore Next.js API calls
pathname.startsWith('/sitecore/') || // Ignore Sitecore API calls
pathname.startsWith('/_next') || // Ignore next service calls
(((_a = this.config) === null || _a === void 0 ? void 0 : _a.excludeRoute) && ((_b = this.config) === null || _b === void 0 ? void 0 : _b.excludeRoute(pathname))));
};
/**
* Safely extract all headers for debug logging
* Necessary to avoid middleware issue https://github.com/vercel/next.js/issues/39765
* @param {Headers} incomingHeaders Incoming headers
* @returns Object with headers as key/value pairs
*/
MiddlewareBase.prototype.extractDebugHeaders = function (incomingHeaders) {
var headers = {};
incomingHeaders.forEach(function (value, key) { return (headers[key] = value); });
return headers;
};
/**
* Provides used language
* @param {NextRequest} req request
* @returns {string} language
*/
MiddlewareBase.prototype.getLanguage = function (req) {
return req.nextUrl.locale || req.nextUrl.defaultLocale || 'en';
};
/**
* Extract 'host' header
* @param {NextRequest} req request
*/
MiddlewareBase.prototype.getHostHeader = function (req) {
var _a;
return (_a = req.headers.get('host')) === null || _a === void 0 ? void 0 : _a.split(':')[0];
};
/**
* Get site information.
* Can not be used in **Preview** mode, since site will not be resolved
* @param {NextRequest} req request
* @param {NextResponse} [res] response
* @returns {SiteInfo} site information
*/
MiddlewareBase.prototype.getSite = function (req, res) {
var _a;
var siteNameCookie = (_a = res === null || res === void 0 ? void 0 : res.cookies.get(this.SITE_SYMBOL)) === null || _a === void 0 ? void 0 : _a.value;
if (siteNameCookie)
return this.config.siteResolver.getByName(siteNameCookie);
var hostname = this.getHostHeader(req) || this.defaultHostname;
return this.config.siteResolver.getByHost(hostname);
};
return MiddlewareBase;
}());
export { MiddlewareBase };