Add changeset & docs for next @astrojs/netlify release (#7700)

Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
This commit is contained in:
Chris Swithinbank 2023-07-18 17:57:19 +02:00 committed by GitHub
parent b3b9fc52b3
commit a77741d25e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 1 deletions

View file

@ -0,0 +1,8 @@
---
'@astrojs/netlify': minor
---
When a project uses the new `build.excludeMiddleware` Astro config option, the `@astrojs/netlify/functions` adapter will bundle your middleware to run in a [Netlify Edge Function](https://docs.netlify.com/edge-functions/overview/).
See the [Netlify adapter documentation](https://docs.astro.build/en/guides/integrations-guide/netlify/#run-middleware-in-edge-functions) for more details.

View file

@ -57,7 +57,7 @@ If you prefer to install the adapter manually instead, complete the following tw
### Edge Functions
Netlify has two serverless platforms, Netlify Functions and [Netlify's experimental Edge Functions](https://docs.netlify.com/netlify-labs/experimental-features/edge-functions/#app). With Edge Functions your code is distributed closer to your users, lowering latency.
Netlify has two serverless platforms, [Netlify Functions](https://docs.netlify.com/functions/overview/) and [Netlify Edge Functions](https://docs.netlify.com/edge-functions/overview/). With Edge Functions your code is distributed closer to your users, lowering latency.
To deploy with Edge Functions, use `netlify/edge-functions` in the Astro config file instead of `netlify/functions`.
@ -72,6 +72,56 @@ export default defineConfig({
});
```
### Run middleware in Edge Functions
When deploying to Netlify Functions, you can choose to use an Edge Function to run your Astro middleware.
To enable this, set the `build.excludeMiddleware` Astro config option to `true`:
```js ins={9}
// astro.config.mjs
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify/functions';
export default defineConfig({
output: 'server',
adapter: netlify(),
build: {
excludeMiddleware: true,
},
});
```
#### Pass edge context to your site
Netlify Edge Functions provide a [context object](https://docs.netlify.com/edge-functions/api/#netlify-specific-context-object) including metadata about the request, such as a users IP, geolocation data, and cookies.
To expose values from this context to your site, create a `netlify-edge-middleware.ts` (or `.js`) file in your projects [source directory](https://docs.astro.build/en/reference/configuration-reference/#srcdir). This file must export a function that returns the data to add to [Astros `locals` object](https://docs.astro.build/en/guides/middleware/#locals), which is available in middleware and Astro routes.
In this example, `visitorCountry` and `hasEdgeMiddleware` would both be added to Astros `locals` object:
```ts
// src/netlify-edge-middleware.ts
import type { Context } from 'https://edge.netlify.com';
export default function ({ request, context }: { request: Request; context: Context }) {
// Return serializable data to add to Astro.locals
return {
visitorCountry: context.geo.country.name,
hasEdgeMiddleware: true,
};
}
```
> **Note**
> Netlify Edge Functions run in [a Deno environment](https://docs.netlify.com/edge-functions/api/#runtime-environment), so import statements in this file must use Denos URL syntax.
`netlify-edge-middleware.ts` must provide a function as its default export. This function:
- must return a JSON-serializable object, which cannot include types like `Map`, `function`, `Set`, etc.
- will always run first, before any other middleware and routes.
- cannot return a response or redirect.
### Per-page functions
The Netlify adapter builds to a single function by default. Astro 2.7 added support for splitting your build into separate entry points per page. If you use this configuration, the Netlify adapter will generate a separate function for each page. This can help reduce the size of each function so they are only bundling code used on that page.