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) =>
|
message: (adapterName: string) =>
|
||||||
`\`Astro.clientAddress\` is not available in the \`${adapterName}\` adapter. File an issue with the adapter to add support.`,
|
`\`Astro.clientAddress\` is not available in the \`${adapterName}\` adapter. File an issue with the adapter to add support.`,
|
||||||
} satisfies ErrorData;
|
} 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
|
* @docs
|
||||||
* @see
|
* @see
|
||||||
|
|
|
@ -455,14 +455,21 @@ export class RenderContext {
|
||||||
if (clientAddressSymbol in request) {
|
if (clientAddressSymbol in request) {
|
||||||
return Reflect.get(request, clientAddressSymbol) as string;
|
return Reflect.get(request, clientAddressSymbol) as string;
|
||||||
}
|
}
|
||||||
if (pipeline.adapterName) {
|
|
||||||
throw new AstroError({
|
if (pipeline.serverLike) {
|
||||||
...AstroErrorData.ClientAddressNotAvailable,
|
if (request.body === null) {
|
||||||
message: AstroErrorData.ClientAddressNotAvailable.message(pipeline.adapterName),
|
throw new AstroError(AstroErrorData.PrerenderClientAddressNotAvailable);
|
||||||
});
|
}
|
||||||
} else {
|
|
||||||
throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable);
|
if (pipeline.adapterName) {
|
||||||
|
throw new AstroError({
|
||||||
|
...AstroErrorData.ClientAddressNotAvailable,
|
||||||
|
message: AstroErrorData.ClientAddressNotAvailable.message(pipeline.adapterName),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -135,7 +135,7 @@ export function createDevelopmentManifest(settings: AstroSettings): SSRManifest
|
||||||
assets: new Set(),
|
assets: new Set(),
|
||||||
entryModules: {},
|
entryModules: {},
|
||||||
routes: [],
|
routes: [],
|
||||||
adapterName: '',
|
adapterName: settings?.adapter?.name || '',
|
||||||
clientDirectives: settings.clientDirectives,
|
clientDirectives: settings.clientDirectives,
|
||||||
renderers: [],
|
renderers: [],
|
||||||
base: settings.config.base,
|
base: settings.config.base,
|
||||||
|
|
Loading…
Reference in a new issue