Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/content/docs/en/basics/astro-pages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ During development, if you have a `500.astro`, the error thrown at runtime is lo

<p><Since v="4.11.0" /></p>

`src/pages/500.astro` is a special page that is automatically passed an `error` prop for any error thrown during rendering. This allows you to use the details of an error (e.g. from a page, from middleware, etc.) to display information to your visitor.
`src/pages/500.astro` is a special page that is automatically passed an `error` prop for error thrown during rendering. This allows you to use the details of an error (e.g. from a page, from middleware, etc.) to display information to your visitor.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the change here? I don't think there was a problem with the original sentence. (And, the new sentence is now grammatically incorrect.) What did you mean to change here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @sarah11918 san.
Thank you for your comment!
I've removed the word "any" to make the description a bit more precise. Since some cases might not actually trigger a 500 error, I thought avoiding "any" would help prevent any potential confusion. This way, the documentation stays closer to the actual behavior.


The `error` prop's data type can be anything, which may affect how you type or use the value in your code:

Expand All @@ -132,6 +132,37 @@ const { error } = Astro.props;

To avoid leaking sensitive information when displaying content from the `error` prop, consider evaluating the error first, and returning appropriate content based on the error thrown. For example, you should avoid displaying the error's stack as it contains information about how your code is structured on the server.

:::caution
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder that we typically only use cautions for dangerous situations (your project won't build, you might lose data) and not just when describing "how things work."

Feel free to rework existing text it reads more like an explanation of how streaming works. That's what we normally do when we want to add more information or context! This doesn't have to be a "patch a note on existing text" situation. If we didn't explain it well, or completely, then our explanation should change!

Astro streams content to the browser as it is rendered. If an error occurs in a component template after the initial response headers have been sent, the server cannot trigger the 500 error page.
:::

To ensure that errors are captured by the 500 page, try to perform data fetching and validation in the frontmatter. Errors thrown here happen before rendering begins, allowing the server to display the error page.

If you must catch errors that occur during rendering (e.g. inside a component's template), you can use middleware to buffer the response. This forces the entire page to render in memory before sending anything to the browser.

```ts title="src/middleware.ts"
import { defineMiddleware } from "astro:middleware";

export const onRequest = defineMiddleware(async (_, next) => {
const response = await next();

// Important: must filter out non-HTML requests here!

try {
const html = await response.text();
return new Response(html, {
status: response.status,
headers: response.headers,
});
} catch (error) {
throw error;
}
});
```
:::note
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, we should not be using notes (or cautions) to just explain "how things just work." This often means that something wasn't properly described in the first place, and is a sign to think about the flow of information, and make sure it's all presented in appropriate places.

Buffering the response in middleware disables streaming for that page, which may delay the Time to First Byte (TTFB). Use this only if capturing rendering errors is critical for your application.
:::

## Page Partials

<p><Since v="3.4.0" /></p>
Expand Down