diff --git a/.changeset/breezy-lies-sparkle.md b/.changeset/breezy-lies-sparkle.md new file mode 100644 index 0000000000..a5570ba618 --- /dev/null +++ b/.changeset/breezy-lies-sparkle.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Improve error message when accessing `clientAddress` on prerendered routes diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index de4e77801c..6646faff8b 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -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 diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index b9fa84030d..743bb967ec 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -455,14 +455,21 @@ export class RenderContext { if (clientAddressSymbol in request) { return Reflect.get(request, clientAddressSymbol) as string; } - if (pipeline.adapterName) { - throw new AstroError({ - ...AstroErrorData.ClientAddressNotAvailable, - message: AstroErrorData.ClientAddressNotAvailable.message(pipeline.adapterName), - }); - } else { - throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable); + + 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), + }); + } } + + throw new AstroError(AstroErrorData.StaticClientAddressNotAvailable); } /** diff --git a/packages/astro/src/vite-plugin-astro-server/plugin.ts b/packages/astro/src/vite-plugin-astro-server/plugin.ts index 3c6f06ee9a..f576602458 100644 --- a/packages/astro/src/vite-plugin-astro-server/plugin.ts +++ b/packages/astro/src/vite-plugin-astro-server/plugin.ts @@ -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,