UNPKG

@inlang/paraglide-js

Version:

[![NPM Downloads](https://img.shields.io/npm/dw/%40inlang%2Fparaglide-js?logo=npm&logoColor=red&label=npm%20downloads)](https://www.npmjs.com/package/@inlang/paraglide-js) [![GitHub Issues](https://img.shields.io/github/issues-closed/opral/paraglide-js?lo

101 lines 3.51 kB
export type ShouldRedirectServerInput = { request: Request; /** * - Effective request URL to use for route matching, locale detection with the URL strategy, and redirect targets. */ effectiveRequestUrl?: string | URL; locale?: Locale; }; export type ShouldRedirectClientInput = { request?: undefined; url?: string | URL; locale?: Locale; }; export type ShouldRedirectInput = ShouldRedirectServerInput | ShouldRedirectClientInput; export type ShouldRedirectResult = { /** * - Indicates whether the consumer should perform a redirect. */ shouldRedirect: boolean; /** * - Locale resolved using the configured strategies. */ locale: Locale; /** * - Destination URL when a redirect is required. */ redirectUrl: URL | undefined; }; /** * @typedef {object} ShouldRedirectServerInput * @property {Request} request * @property {string | URL} [effectiveRequestUrl] - Effective request URL to use for route matching, locale detection with the URL strategy, and redirect targets. * @property {Locale} [locale] * * @typedef {object} ShouldRedirectClientInput * @property {undefined} [request] * @property {string | URL} [url] * @property {Locale} [locale] * * @typedef {ShouldRedirectServerInput | ShouldRedirectClientInput} ShouldRedirectInput * * @typedef {object} ShouldRedirectResult * @property {boolean} shouldRedirect - Indicates whether the consumer should perform a redirect. * @property {Locale} locale - Locale resolved using the configured strategies. * @property {URL | undefined} redirectUrl - Destination URL when a redirect is required. */ /** * Determines whether a redirect is required to align the current URL with the active locale. * * This helper mirrors the logic that powers `paraglideMiddleware`, but works in both server * and client environments. It evaluates the configured strategies in order, computes the * canonical localized URL, and reports when the current URL does not match. * * When called in the browser without arguments, the current `window.location.href` is used. * * @see https://paraglidejs.com/i18n-routing#redirects * * @example * // Client side usage (e.g. TanStack Router beforeLoad hook) * async function beforeLoad({ location }) { * const decision = await shouldRedirect({ url: location.href }); * * if (decision.shouldRedirect) { * throw redirect({ to: decision.redirectUrl.href }); * } * } * * @example * // Server side usage with a Request * export async function handle(request) { * const decision = await shouldRedirect({ request }); * * if (decision.shouldRedirect) { * return Response.redirect(decision.redirectUrl, 307); * } * * return render(request, decision.locale); * } * * @example * // Server side usage behind a proxy where request.url is not public-facing * export async function handle(request) { * const effectiveRequestUrl = new URL(request.url); * effectiveRequestUrl.protocol = "https:"; * effectiveRequestUrl.host = "example.com"; * * const decision = await shouldRedirect({ * request, * effectiveRequestUrl, * }); * * if (decision.shouldRedirect) { * return Response.redirect(decision.redirectUrl, 307); * } * } * * @param {ShouldRedirectInput} [input] * @returns {Promise<ShouldRedirectResult>} */ export declare function shouldRedirect(input?: ShouldRedirectInput): Promise<ShouldRedirectResult>; //# sourceMappingURL=should-redirect.d.ts.map