diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 437cf4c38..fefdec2bf 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1805,7 +1805,7 @@ export type MiddlewareNext = () => Promise; export type MiddlewareHandler = ( context: APIContext, next: MiddlewareNext -) => Promise | Promise | void; +) => Promise | R | Promise | void; export type MiddlewareResponseHandler = MiddlewareHandler; export type MiddlewareEndpointHandler = MiddlewareHandler; diff --git a/packages/astro/test/fixtures/ssr-redirect/src/middleware.ts b/packages/astro/test/fixtures/ssr-redirect/src/middleware.ts new file mode 100644 index 000000000..5f8243a43 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-redirect/src/middleware.ts @@ -0,0 +1,13 @@ +import { defineMiddleware } from 'astro/middleware'; + +export const onRequest = defineMiddleware(({ request }, next) => { + if(new URL(request.url).pathname === '/middleware-redirect/') { + return new Response(null, { + status: 301, + headers: { + 'Location': '/' + } + }); + } + return next(); +}); diff --git a/packages/astro/test/fixtures/ssr-redirect/src/pages/middleware-redirect.astro b/packages/astro/test/fixtures/ssr-redirect/src/pages/middleware-redirect.astro new file mode 100644 index 000000000..04f62cd89 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-redirect/src/pages/middleware-redirect.astro @@ -0,0 +1,10 @@ +--- +--- + + + This page should have been redirected + + +

This page should have been redirected

+ + diff --git a/packages/astro/test/redirects.test.js b/packages/astro/test/redirects.test.js index fde343bcf..df041baa2 100644 --- a/packages/astro/test/redirects.test.js +++ b/packages/astro/test/redirects.test.js @@ -14,7 +14,7 @@ describe('Astro.redirect', () => { adapter: testAdapter(), redirects: { '/api/redirect': '/' - } + }, }); await fixture.build(); }); @@ -67,6 +67,9 @@ describe('Astro.redirect', () => { fixture = await loadFixture({ root: './fixtures/ssr-redirect/', output: 'static', + experimental: { + middleware: true + }, redirects: { '/one': '/', '/two': '/', @@ -109,5 +112,11 @@ describe('Astro.redirect', () => { expect(html).to.include('http-equiv="refresh'); expect(html).to.include('url=/articles/two'); }); + + it('Generates redirect pages for redirects created by middleware', async () => { + let html = await fixture.readFile('/middleware-redirect/index.html'); + expect(html).to.include('http-equiv="refresh'); + expect(html).to.include('url=/'); + }); }); });