Skip to content

Commit

Permalink
Make Astro.redirect use a 302 status code (#3700)
Browse files Browse the repository at this point in the history
* Make Astro.redirect use a 302 status code

* Adds a changeset

* Add a package.json
  • Loading branch information
matthewp authored Jun 24, 2022
1 parent 69c955b commit 47c81ef
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/stupid-steaks-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Make Astro.redirect use a 302 status code
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function createResult(args: CreateResultArgs): SSRResult {
redirect: args.ssr
? (path: string) => {
return new Response(null, {
status: 301,
status: 302,
headers: {
Location: path,
},
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-redirect/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-redirect",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
return Astro.redirect('/login');
---
27 changes: 27 additions & 0 deletions packages/astro/test/ssr-redirect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

describe('Astro.redirect', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-redirect/',
experimental: {
ssr: true,
},
adapter: testAdapter(),
});
await fixture.build();
});

it('Returns a 302 status', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/secret');
const response = await app.render(request);
expect(response.status).to.equal(302);
expect(response.headers.get('location')).to.equal('/login');
});
});
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 47c81ef

Please sign in to comment.