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:
parent
4c1ffef3e4
commit
500acb3c11
4 changed files with 27 additions and 2 deletions
5
.changeset/tall-trainers-do.md
Normal file
5
.changeset/tall-trainers-do.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Throws when using Response.redirect in SSG mode
|
|
@ -1,7 +1,7 @@
|
|||
import type http from 'http';
|
||||
import mime from 'mime';
|
||||
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 { 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(
|
||||
matchedRoute: AsyncReturnType<typeof matchRoute>,
|
||||
url: URL,
|
||||
|
@ -367,6 +377,7 @@ async function handleRoute(
|
|||
res
|
||||
);
|
||||
}
|
||||
throwIfRedirectNotAllowed(result.response, config);
|
||||
await writeWebResponse(res, result.response);
|
||||
} else {
|
||||
let contentType = 'text/plain';
|
||||
|
@ -389,6 +400,7 @@ async function handleRoute(
|
|||
}
|
||||
} else {
|
||||
const result = await renderPage(options);
|
||||
throwIfRedirectNotAllowed(result, config);
|
||||
return await writeSSRResult(result, res);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { expect } from 'chai';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { loadFixture } from './test-utils.js';
|
||||
|
||||
describe('Development Routing', () => {
|
||||
|
@ -42,6 +41,11 @@ describe('Development Routing', () => {
|
|||
const response = await fixture.fetch('/2');
|
||||
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', () => {
|
||||
|
|
4
packages/astro/test/fixtures/without-site-config/src/pages/redirect.astro
vendored
Normal file
4
packages/astro/test/fixtures/without-site-config/src/pages/redirect.astro
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
const anotherURL = new URL('./another/', Astro.url);
|
||||
return Response.redirect(anotherURL.toString());
|
||||
---
|
Loading…
Reference in a new issue