Skip to content

Commit

Permalink
prevent warning: Astro.request.headers is not available in "static"…
Browse files Browse the repository at this point in the history
… output mode (#10196)
  • Loading branch information
lilnasy committed Feb 24, 2024
1 parent 5d4ff09 commit 8fb32f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-schools-type.md
Original file line number Diff line number Diff line change
@@ -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.
52 changes: 25 additions & 27 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<APIContext, 'currentLocale' | 'preferredLocale' | 'preferredLocaleList'>;

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);
}
}

0 comments on commit 8fb32f3

Please sign in to comment.