UNPKG

@prezly/theme-kit-nextjs

Version:

Data layer and utility library for developing Prezly themes with NextJS

88 lines (87 loc) 3.31 kB
import type { NewsroomLanguageSettings } from '@prezly/sdk'; import { AsyncResolvable } from '@prezly/theme-kit-core'; import { Locale } from '@prezly/theme-kit-intl'; import type { NextRequest } from 'next/server'; import { NextResponse } from 'next/server'; export type Configuration = { router: Router; /** * The newsroom default locale. */ defaultLocale: AsyncResolvable<Locale.Code>; /** * Locale codes in the order of preference (in case of locale slug ambiguity). */ locales: AsyncResolvable<Locale.Code[]>; /** * Map locale to its URL slug. This will override the locale * slug shortening logic we have implemented by default. * See [CARE-3532]. */ toLocaleSlug?(locale: Locale.Code, context: { defaultLocale: Locale.Code; locales: Locale.Code[]; }): Locale.UrlSlug; /** * The middleware will attach the request `:localeSlug` parameter to the * rewritten request headers. Needed to know the request locale on the * "not-found" pages, which do not have access to request path. */ localeCodeHeader?: string; }; type Awaitable<T> = T | Promise<T> | PromiseLike<T>; export type Router = { match(pathname: string, searchParams: URLSearchParams, context: { isSupportedLocale(locale: string): boolean; }): Match | undefined; }; export type Route = { generate(params: Params): string; rewrite(params: Params): string; resolveLocale?(params: Params): Awaitable<Locale.Code | undefined>; }; type Params = Record<string, unknown>; type Match = { route: Route; params: Params & { localeSlug?: string; }; }; /** @see Configuration.localeCodeHeader */ export declare const DEFAULT_LOCALE_CODE_HEADER = "X-Prezly-Locale-Code"; /** @see Configuration.requestOriginHeader */ export declare const DEFAULT_REQUEST_ORIGIN_HEADER = "X-Prezly-Request-Origin"; export declare function handle(request: NextRequest, config: Configuration): Promise<NextResponse<unknown>>; export interface LocaleMatchContext { /** * Newsroom languages configuration. */ languages: Pick<NewsroomLanguageSettings, 'code' | 'is_default' | 'public_stories_count'>[]; /** * Expected URL for the current page. */ generateUrl: (localeCode: Locale.Code) => string; /** * Map locale to its URL slug. This will override the locale * slug shortening logic we have implemented by default. * See [CARE-3532]. */ toLocaleSlug?(locale: Locale.Code, context: { defaultLocale: Locale.Code; locales: Locale.Code[]; }): Locale.UrlSlug; } /** * Get the locale code attached to the request headers. * * WARNING: Calling this function will opt out of Next.js optimizations. * Only call it when absolutely necessary. * See https://nextjs.org/docs/app/building-your-application/rendering/server-components#dynamic-functions * * The only currently foreseen use-case for this function is rendering * a 404 page in the right locale, as not-found error pages do not have * access to the request URL, like normal pages. * See https://nextjs.org/docs/app/api-reference/file-conventions/not-found#props */ export declare function getLocaleCodeFromHeader(localeCodeHeader?: string): Locale.Code | undefined; export {};