0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

Fix error when accessing clientAddress on prerendered routes (#10977)

This commit is contained in:
Bryce Russell 2024-05-09 03:16:29 -05:00 committed by GitHub
parent f9b6f4fc2f
commit 59571e8812
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 8 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Improve error message when accessing `clientAddress` on prerendered routes

View file

@ -66,6 +66,19 @@ export const ClientAddressNotAvailable = {
message: (adapterName: string) =>
`\`Astro.clientAddress\` is not available in the \`${adapterName}\` adapter. File an issue with the adapter to add support.`,
} satisfies ErrorData;
/**
* @docs
* @see
* - [Opting-in to pre-rendering](https://docs.astro.build/en/guides/server-side-rendering/#opting-in-to-pre-rendering-in-server-mode)
* - [Astro.clientAddress](https://docs.astro.build/en/reference/api-reference/#astroclientaddress)
* @description
* The `Astro.clientAddress` property cannot be used inside prerendered routes.
*/
export const PrerenderClientAddressNotAvailable = {
name: 'PrerenderClientAddressNotAvailable',
title: '`Astro.clientAddress` cannot be used inside prerendered routes.',
message: `\`Astro.clientAddress\` cannot be used inside prerendered routes`,
} satisfies ErrorData;
/**
* @docs
* @see

View file

@ -455,16 +455,23 @@ export class RenderContext {
if (clientAddressSymbol in request) {
return Reflect.get(request, clientAddressSymbol) as string;
}
if (pipeline.serverLike) {
if (request.body === null) {
throw new AstroError(AstroErrorData.PrerenderClientAddressNotAvailable);
}
if (pipeline.adapterName) {
throw new AstroError({
...AstroErrorData.ClientAddressNotAvailable,
message: AstroErrorData.ClientAddressNotAvailable.message(pipeline.adapterName),
});
} else {
throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable);
}
}
throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable);
}
/**
* 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.

View file

@ -135,7 +135,7 @@ export function createDevelopmentManifest(settings: AstroSettings): SSRManifest
assets: new Set(),
entryModules: {},
routes: [],
adapterName: '',
adapterName: settings?.adapter?.name || '',
clientDirectives: settings.clientDirectives,
renderers: [],
base: settings.config.base,