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

Test that redirects can come from middleware #7213

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ export type MiddlewareNext<R> = () => Promise<R>;
export type MiddlewareHandler<R> = (
context: APIContext,
next: MiddlewareNext<R>
) => Promise<R> | Promise<void> | void;
) => Promise<R> | R | Promise<void> | void;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This type allows you to return a Response without it being an async function.


export type MiddlewareResponseHandler = MiddlewareHandler<Response>;
export type MiddlewareEndpointHandler = MiddlewareHandler<Response | EndpointOutput>;
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/test/fixtures/ssr-redirect/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
---
<html>
<head>
<title>This page should have been redirected</title>
</head>
<body>
<h1>This page should have been redirected</h1>
</body>
</html>
11 changes: 10 additions & 1 deletion packages/astro/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Astro.redirect', () => {
adapter: testAdapter(),
redirects: {
'/api/redirect': '/'
}
},
});
await fixture.build();
});
Expand Down Expand Up @@ -67,6 +67,9 @@ describe('Astro.redirect', () => {
fixture = await loadFixture({
root: './fixtures/ssr-redirect/',
output: 'static',
experimental: {
middleware: true
},
redirects: {
'/one': '/',
'/two': '/',
Expand Down Expand Up @@ -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=/');
});
});
});