mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
prevent warning: Astro.request.headers
is not available in "static" output mode (#10196)
This commit is contained in:
parent
5d4ff093a2
commit
8fb32f390d
2 changed files with 30 additions and 27 deletions
5
.changeset/metal-schools-type.md
Normal file
5
.changeset/metal-schools-type.md
Normal file
|
@ -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.
|
|
@ -36,7 +36,8 @@ export class RenderContext {
|
||||||
readonly routeData: RouteData,
|
readonly routeData: RouteData,
|
||||||
public status: number,
|
public status: number,
|
||||||
readonly cookies = new AstroCookies(request),
|
readonly cookies = new AstroCookies(request),
|
||||||
readonly params = getParams(routeData, pathname)
|
readonly params = getParams(routeData, pathname),
|
||||||
|
readonly url = new URL(request.url)
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
static create({
|
static create({
|
||||||
|
@ -124,20 +125,18 @@ export class RenderContext {
|
||||||
|
|
||||||
createAPIContext(props: APIContext['props']): APIContext {
|
createAPIContext(props: APIContext['props']): APIContext {
|
||||||
const renderContext = this;
|
const renderContext = this;
|
||||||
const { cookies, i18nData, params, pipeline, request } = this;
|
const { cookies, params, pipeline, request, url } = this;
|
||||||
const { currentLocale, preferredLocale, preferredLocaleList } = i18nData;
|
|
||||||
const generator = `Astro v${ASTRO_VERSION}`;
|
const generator = `Astro v${ASTRO_VERSION}`;
|
||||||
const redirect = (path: string, status = 302) =>
|
const redirect = (path: string, status = 302) =>
|
||||||
new Response(null, { status, headers: { Location: path } });
|
new Response(null, { status, headers: { Location: path } });
|
||||||
const site = pipeline.site ? new URL(pipeline.site) : undefined;
|
const site = pipeline.site ? new URL(pipeline.site) : undefined;
|
||||||
const url = new URL(request.url);
|
|
||||||
return {
|
return {
|
||||||
cookies,
|
cookies,
|
||||||
currentLocale,
|
get currentLocale() { return renderContext.computeCurrentLocale() },
|
||||||
generator,
|
generator,
|
||||||
params,
|
params,
|
||||||
preferredLocale,
|
get preferredLocale() { return renderContext.computePreferredLocale() },
|
||||||
preferredLocaleList,
|
get preferredLocaleList() { return renderContext.computePreferredLocaleList() },
|
||||||
props,
|
props,
|
||||||
redirect,
|
redirect,
|
||||||
request,
|
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.
|
* 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.
|
* So, it is computed and saved here on creation of the first APIContext and reused for later ones.
|
||||||
*/
|
*/
|
||||||
#i18nData?: Pick<APIContext, 'currentLocale' | 'preferredLocale' | 'preferredLocaleList'>;
|
#currentLocale: APIContext['currentLocale'];
|
||||||
|
computeCurrentLocale() {
|
||||||
get i18nData() {
|
const { url, pipeline: { i18n }, routeData } = this;
|
||||||
if (this.#i18nData) return this.#i18nData;
|
if (!i18n) return;
|
||||||
const {
|
|
||||||
pipeline: { i18n },
|
|
||||||
request,
|
|
||||||
routeData,
|
|
||||||
} = this;
|
|
||||||
if (!i18n)
|
|
||||||
return {
|
|
||||||
currentLocale: undefined,
|
|
||||||
preferredLocale: undefined,
|
|
||||||
preferredLocaleList: undefined,
|
|
||||||
};
|
|
||||||
const { defaultLocale, locales, strategy } = i18n;
|
const { defaultLocale, locales, strategy } = i18n;
|
||||||
return (this.#i18nData = {
|
return this.#currentLocale ??= computeCurrentLocale(routeData.route, locales, strategy, defaultLocale);
|
||||||
currentLocale: computeCurrentLocale(routeData.route, locales, strategy, defaultLocale),
|
}
|
||||||
preferredLocale: computePreferredLocale(request, locales),
|
|
||||||
preferredLocaleList: computePreferredLocaleList(request, locales),
|
#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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue