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:
parent
f9b6f4fc2f
commit
59571e8812
4 changed files with 33 additions and 8 deletions
5
.changeset/breezy-lies-sparkle.md
Normal file
5
.changeset/breezy-lies-sparkle.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Improve error message when accessing `clientAddress` on prerendered routes
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue