Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw when using Response.redirect from SSG site #5116

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Throw when using Response.redirect from SSG site
  • Loading branch information
matthewp committed Oct 18, 2022
commit 5dbda0c863ff6866a01b68bb35acf41078abb98c
14 changes: 13 additions & 1 deletion packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -367,6 +377,7 @@ async function handleRoute(
res
);
}
throwIfRedirectNotAllowed(result.response, config);
await writeWebResponse(res, result.response);
} else {
let contentType = 'text/plain';
Expand All @@ -389,6 +400,7 @@ async function handleRoute(
}
} else {
const result = await renderPage(options);
throwIfRedirectNotAllowed(result, config);
return await writeSSRResult(result, res);
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/test/dev-routing.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Development Routing', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
const anotherURL = new URL('./another/', Astro.url);
return Response.redirect(anotherURL.toString());
---