diff options
author | 2022-06-24 15:55:06 -0400 | |
---|---|---|
committer | 2022-06-24 15:55:06 -0400 | |
commit | 47c81effa69fb5d7f1e576f88c27d5071f1888e3 (patch) | |
tree | 3ba466b87af011599251a648e8ed6810db310a95 | |
parent | 69c955b2bf32bd5f0723b999a67257de50101321 (diff) | |
download | astro-47c81effa69fb5d7f1e576f88c27d5071f1888e3.tar.gz astro-47c81effa69fb5d7f1e576f88c27d5071f1888e3.tar.zst astro-47c81effa69fb5d7f1e576f88c27d5071f1888e3.zip |
Make Astro.redirect use a 302 status code (#3700)
* Make Astro.redirect use a 302 status code
* Adds a changeset
* Add a package.json
-rw-r--r-- | .changeset/stupid-steaks-hang.md | 5 | ||||
-rw-r--r-- | packages/astro/src/core/render/result.ts | 2 | ||||
-rw-r--r-- | packages/astro/test/fixtures/ssr-redirect/package.json | 8 | ||||
-rw-r--r-- | packages/astro/test/fixtures/ssr-redirect/src/pages/secret.astro | 3 | ||||
-rw-r--r-- | packages/astro/test/ssr-redirect.test.js | 27 | ||||
-rw-r--r-- | pnpm-lock.yaml | 6 |
6 files changed, 50 insertions, 1 deletions
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:' |