Skip to content

Commit

Permalink
Fix error when accessing clientAddress on prerendered routes (#10977)
Browse files Browse the repository at this point in the history
  • Loading branch information
BryceRussell authored May 9, 2024
1 parent f9b6f4f commit 59571e8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-lies-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Improve error message when accessing `clientAddress` on prerendered routes
13 changes: 13 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 59571e8

Please sign in to comment.