From 8fb32f390d40cfa12a82c0645928468d27218866 Mon Sep 17 00:00:00 2001 From: Arsh <69170106+lilnasy@users.noreply.github.com> Date: Sat, 24 Feb 2024 17:05:16 +0530 Subject: [PATCH] prevent warning: `Astro.request.headers` is not available in "static" output mode (#10196) --- .changeset/metal-schools-type.md | 5 +++ packages/astro/src/core/render-context.ts | 52 +++++++++++------------ 2 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 .changeset/metal-schools-type.md diff --git a/.changeset/metal-schools-type.md b/.changeset/metal-schools-type.md new file mode 100644 index 000000000000..9dc70ea13957 --- /dev/null +++ b/.changeset/metal-schools-type.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes an issue where a warning about headers being accessed in static mode is unnecessarily shown when i18n is enabled. diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index c0cc55c81d20..155b449b6b7d 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -36,7 +36,8 @@ export class RenderContext { readonly routeData: RouteData, public status: number, readonly cookies = new AstroCookies(request), - readonly params = getParams(routeData, pathname) + readonly params = getParams(routeData, pathname), + readonly url = new URL(request.url) ) {} static create({ @@ -124,20 +125,18 @@ export class RenderContext { createAPIContext(props: APIContext['props']): APIContext { const renderContext = this; - const { cookies, i18nData, params, pipeline, request } = this; - const { currentLocale, preferredLocale, preferredLocaleList } = i18nData; + const { cookies, params, pipeline, request, url } = this; const generator = `Astro v${ASTRO_VERSION}`; const redirect = (path: string, status = 302) => new Response(null, { status, headers: { Location: path } }); const site = pipeline.site ? new URL(pipeline.site) : undefined; - const url = new URL(request.url); return { cookies, - currentLocale, + get currentLocale() { return renderContext.computeCurrentLocale() }, generator, params, - preferredLocale, - preferredLocaleList, + get preferredLocale() { return renderContext.computePreferredLocale() }, + get preferredLocaleList() { return renderContext.computePreferredLocaleList() }, props, redirect, request, @@ -223,26 +222,25 @@ export class RenderContext { * API Context may be created multiple times per request, i18n data needs to be computed only once. * So, it is computed and saved here on creation of the first APIContext and reused for later ones. */ - #i18nData?: Pick; - - get i18nData() { - if (this.#i18nData) return this.#i18nData; - const { - pipeline: { i18n }, - request, - routeData, - } = this; - if (!i18n) - return { - currentLocale: undefined, - preferredLocale: undefined, - preferredLocaleList: undefined, - }; + #currentLocale: APIContext['currentLocale']; + computeCurrentLocale() { + const { url, pipeline: { i18n }, routeData } = this; + if (!i18n) return; const { defaultLocale, locales, strategy } = i18n; - return (this.#i18nData = { - currentLocale: computeCurrentLocale(routeData.route, locales, strategy, defaultLocale), - preferredLocale: computePreferredLocale(request, locales), - preferredLocaleList: computePreferredLocaleList(request, locales), - }); + return this.#currentLocale ??= computeCurrentLocale(routeData.route, locales, strategy, defaultLocale); + } + + #preferredLocale: APIContext['preferredLocale']; + computePreferredLocale() { + const { pipeline: { i18n }, request } = this; + if (!i18n) return; + return this.#preferredLocale ??= computePreferredLocale(request, i18n.locales); + } + + #preferredLocaleList: APIContext['preferredLocaleList']; + computePreferredLocaleList() { + const { pipeline: { i18n }, request } = this; + if (!i18n) return; + return this.#preferredLocaleList ??= computePreferredLocaleList(request, i18n.locales); } }