Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document new behavior(vercel, netlify): edge middleware verification #6786

Merged
merged 7 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 4 additions & 2 deletions src/content/docs/en/guides/integrations-guide/netlify.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ declare namespace App {

This is not available on prerendered pages.

### Running Astro middleware in Edge Functions
### Running Astro middleware on Netlify Edge Functions

Any Astro middleware is applied to pre-rendered pages at build-time, and to on-demand-rendered pages at runtime.

Expand All @@ -144,7 +144,9 @@ export default defineConfig({
});
```

Configuring `edgeMiddleware: true` will deploy your middleware as an Edge Function, and run it on all routes - including pre-rendered pages. However, locals specified in the middleware won't be available to any pre-rendered pages, because they've already been fully-rendered at build-time.
When `edgeMiddleware` is enabled, an edge function will execute your middleware code for all requests including static assets, prerendered pages, and on-demand rendered pages.

For on-demand rendered pages, the `context.locals` obect is serialized using JSON and sent in a header for the serverless function, which performs the rendering. As a security measure, the serverless function will refuse to serve requests with a `403 Forbidden` response unless they come from the generated edge function.
lilnasy marked this conversation as resolved.
Show resolved Hide resolved

### Netlify Image CDN support

Expand Down
12 changes: 8 additions & 4 deletions src/content/docs/en/guides/integrations-guide/vercel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ export default defineConfig({

### Vercel Edge Middleware

You can use Vercel Edge middleware to intercept a request and redirect before sending a response. Vercel middleware can run for Edge, SSR, and Static deployments. You may not need to install this package for your middleware. `@vercel/edge` is only required to use some middleware features such as geolocation. For more information see [Vercel’s middleware documentation](https://vercel.com/docs/concepts/functions/edge-middleware).
You can use [Vercel Edge middleware](https://vercel.com/docs/functions/edge-middleware) to intercept a request and redirect before sending a response. Vercel middleware can run for Edge, SSR, and Static deployments.

You may not need to install this package for your middleware. `@vercel/edge` is only required to use some middleware features such as geolocation. For more information, see [Vercel’s middleware documentation](https://vercel.com/docs/concepts/functions/edge-middleware).

1. Add a `middleware.js` file to the root of your project:

Expand Down Expand Up @@ -333,11 +335,13 @@ You can use Vercel Edge middleware to intercept a request and redirect before se
**Trying to rewrite?** Currently rewriting a request with middleware only works for static files.
:::

### Vercel Edge Middleware with Astro middleware
### Running Astro middleware on Vercel Edge Functions

The `@astrojs/vercel/serverless` adapter can create an [edge function](https://vercel.com/docs/functions/edge-functions) from an Astro middleware in your code base. When `edgeMiddleware` is enabled, an edge function will execute your middleware code for all requests including static assets, prerendered pages, and on-demand rendered pages.

The `@astrojs/vercel/serverless` adapter can automatically create the Vercel Edge middleware from an Astro middleware in your code base.
For on-demand rendered pages, the `context.locals` object is serialized using JSON and sent in a header for the serverless function, which performs the rendering. As a security measure, the serverless function will refuse to serve requests with a `403 Forbidden` response unless they come from the generated edge function.

This is an opt-in feature, and the `edgeMiddleware` option needs to be set to `true`:
This is an opt-in feature. To enable it, set `edgeMiddleware` to `true`:

```js title="astro.config.mjs" "edgeMiddleware: true"
import { defineConfig } from 'astro/config';
Expand Down
Loading