From 47c81effa69fb5d7f1e576f88c27d5071f1888e3 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 24 Jun 2022 15:55:06 -0400 Subject: [PATCH] Make Astro.redirect use a 302 status code (#3700) * Make Astro.redirect use a 302 status code * Adds a changeset * Add a package.json --- .changeset/stupid-steaks-hang.md | 5 ++++ packages/astro/src/core/render/result.ts | 2 +- .../test/fixtures/ssr-redirect/package.json | 8 ++++++ .../ssr-redirect/src/pages/secret.astro | 3 +++ packages/astro/test/ssr-redirect.test.js | 27 +++++++++++++++++++ pnpm-lock.yaml | 6 +++++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 .changeset/stupid-steaks-hang.md create mode 100644 packages/astro/test/fixtures/ssr-redirect/package.json create mode 100644 packages/astro/test/fixtures/ssr-redirect/src/pages/secret.astro create mode 100644 packages/astro/test/ssr-redirect.test.js diff --git a/.changeset/stupid-steaks-hang.md b/.changeset/stupid-steaks-hang.md new file mode 100644 index 000000000..157e89480 --- /dev/null +++ b/.changeset/stupid-steaks-hang.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Make Astro.redirect use a 302 status code diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index 154fb797b..cfbc48521 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -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, }, diff --git a/packages/astro/test/fixtures/ssr-redirect/package.json b/packages/astro/test/fixtures/ssr-redirect/package.json new file mode 100644 index 000000000..9017715ce --- /dev/null +++ b/packages/astro/test/fixtures/ssr-redirect/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-redirect", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-redirect/src/pages/secret.astro b/packages/astro/test/fixtures/ssr-redirect/src/pages/secret.astro new file mode 100644 index 000000000..f8fc808e7 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-redirect/src/pages/secret.astro @@ -0,0 +1,3 @@ +--- +return Astro.redirect('/login'); +--- diff --git a/packages/astro/test/ssr-redirect.test.js b/packages/astro/test/ssr-redirect.test.js new file mode 100644 index 000000000..29fe701f7 --- /dev/null +++ b/packages/astro/test/ssr-redirect.test.js @@ -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'); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b42e76d32..666af6789 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1636,6 +1636,12 @@ importers: '@astrojs/partytown': link:../../../../integrations/partytown astro: link:../../.. + packages/astro/test/fixtures/ssr-redirect: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/ssr-scripts: specifiers: '@astrojs/preact': 'workspace:'