diff options
Diffstat (limited to '.changeset/khaki-glasses-raise.md')
-rw-r--r-- | .changeset/khaki-glasses-raise.md | 48 |
1 files changed, 0 insertions, 48 deletions
diff --git a/.changeset/khaki-glasses-raise.md b/.changeset/khaki-glasses-raise.md deleted file mode 100644 index 4a0622a42..000000000 --- a/.changeset/khaki-glasses-raise.md +++ /dev/null @@ -1,48 +0,0 @@ ---- -'astro': minor ---- - -## Integration Hooks to add Middleware - -It's now possible in Astro for an integration to add middleware on behalf of the user. Previously when a third party wanted to provide middleware, the user would need to create a `src/middleware.ts` file themselves. Now, adding third-party middleware is as easy as adding a new integration. - -For integration authors, there is a new `addMiddleware` function in the `astro:config:setup` hook. This function allows you to specify a middleware module and the order in which it should be applied: - -```js -// my-package/middleware.js -import { defineMiddleware } from 'astro:middleware'; - -export const onRequest = defineMiddleware(async (context, next) => { - const response = await next(); - - if(response.headers.get('content-type') === 'text/html') { - let html = await response.text(); - html = minify(html); - return new Response(html, { - status: response.status, - headers: response.headers - }); - } - - return response; -}); -``` - -You can now add your integration's middleware and specify that it runs either before or after the application's own defined middleware (defined in `src/middleware.{js,ts}`) - -```js -// my-package/integration.js -export function myIntegration() { - return { - name: 'my-integration', - hooks: { - 'astro:config:setup': ({ addMiddleware }) => { - addMiddleware({ - entrypoint: 'my-package/middleware', - order: 'pre' - }); - } - } - }; -} -``` |