0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

Throw when using Response.redirect from SSG site (#5116)

* Throw when using Response.redirect from SSG site

* Adding a changeset
This commit is contained in:
Matthew Phillips 2022-10-18 13:09:38 -07:00 committed by GitHub
parent 4c1ffef3e4
commit 500acb3c11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Throws when using Response.redirect in SSG mode

View file

@ -1,7 +1,7 @@
import type http from 'http'; import type http from 'http';
import mime from 'mime'; import mime from 'mime';
import type * as vite from 'vite'; import type * as vite from 'vite';
import type { AstroSettings, ManifestData } from '../@types/astro'; import type { AstroConfig, AstroSettings, ManifestData } from '../@types/astro';
import { DevelopmentEnvironment, SSROptions } from '../core/render/dev/index'; import { DevelopmentEnvironment, SSROptions } from '../core/render/dev/index';
import { Readable } from 'stream'; import { Readable } from 'stream';
@ -296,6 +296,16 @@ async function handleRequest(
} }
} }
function isRedirect(statusCode: number) {
return statusCode >= 300 && statusCode < 400;
}
function throwIfRedirectNotAllowed(response: Response, config: AstroConfig) {
if(config.output !== 'server' && isRedirect(response.status)) {
throw new Error(`Redirects are only available when using output: 'server'. Update your Astro config if you need SSR features.`);
}
}
async function handleRoute( async function handleRoute(
matchedRoute: AsyncReturnType<typeof matchRoute>, matchedRoute: AsyncReturnType<typeof matchRoute>,
url: URL, url: URL,
@ -367,6 +377,7 @@ async function handleRoute(
res res
); );
} }
throwIfRedirectNotAllowed(result.response, config);
await writeWebResponse(res, result.response); await writeWebResponse(res, result.response);
} else { } else {
let contentType = 'text/plain'; let contentType = 'text/plain';
@ -389,6 +400,7 @@ async function handleRoute(
} }
} else { } else {
const result = await renderPage(options); const result = await renderPage(options);
throwIfRedirectNotAllowed(result, config);
return await writeSSRResult(result, res); return await writeSSRResult(result, res);
} }
} }

View file

@ -1,5 +1,4 @@
import { expect } from 'chai'; import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js'; import { loadFixture } from './test-utils.js';
describe('Development Routing', () => { describe('Development Routing', () => {
@ -42,6 +41,11 @@ describe('Development Routing', () => {
const response = await fixture.fetch('/2'); const response = await fixture.fetch('/2');
expect(response.status).to.equal(404); expect(response.status).to.equal(404);
}); });
it('500 when redirecting in SSG mode', async () => {
const response = await fixture.fetch('/redirect');
expect(response.status).to.equal(500);
});
}); });
describe('No subpath used', () => { describe('No subpath used', () => {

View file

@ -0,0 +1,4 @@
---
const anotherURL = new URL('./another/', Astro.url);
return Response.redirect(anotherURL.toString());
---