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,
|
||||
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<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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue