UNPKG

@jss-rule-engine/edge

Version:

69 lines (68 loc) 2.72 kB
import { SiteInfo } from '@sitecore-jss/sitecore-jss/site'; import { NextRequest, NextResponse } from 'next/server'; export type MiddlewareBaseConfig = { /** * function, determines if middleware should be turned off, based on cookie, header, or other considerations * @param {NextRequest} [req] request object from middleware handler * @param {NextResponse} [res] response object from middleware handler */ disabled?: (req?: NextRequest, res?: NextResponse) => boolean; /** * Function used to determine if route should be excluded. * By default, files (pathname.includes('.')), Next.js API routes (pathname.startsWith('/api/')), and Sitecore API routes (pathname.startsWith('/sitecore/')) are ignored. * This is an important performance consideration since Next.js Edge middleware runs on every request. * @param {string} pathname The pathname * @returns {boolean} Whether to exclude the route */ excludeRoute?: (pathname: string) => boolean; /** * Fallback hostname in case `host` header is not present * @default localhost */ defaultHostname?: string; /** * Site resolution implementation by name/hostname */ siteResolver: any; }; export declare abstract class MiddlewareBase { protected config: MiddlewareBaseConfig; protected SITE_SYMBOL: string; protected defaultHostname: string; constructor(config: MiddlewareBaseConfig); /** * Determines if mode is preview * @param {NextRequest} req request * @returns {boolean} is preview */ protected isPreview(req: NextRequest): boolean; protected excludeRoute(pathname: string): boolean | undefined; /** * 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 */ protected extractDebugHeaders(incomingHeaders: Headers): { [key: string]: string; }; /** * Provides used language * @param {NextRequest} req request * @returns {string} language */ protected getLanguage(req: NextRequest): string; /** * Extract 'host' header * @param {NextRequest} req request */ protected getHostHeader(req: NextRequest): string | undefined; /** * 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 */ protected getSite(req: NextRequest, res?: NextResponse): SiteInfo; }