Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit ee478cf

Browse files
authored
Add support for res.redirect in API routes (#93)
1 parent 6f1a1b3 commit ee478cf

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default async function preview(req, res) {
2+
const { query } = req;
3+
const { to } = query;
4+
5+
res.redirect(`/shows/${to}`);
6+
}

cypress/integration/default_spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,14 @@ describe("API endpoint", () => {
526526
);
527527
});
528528
});
529+
530+
it("redirects with res.redirect", () => {
531+
cy.visit("/api/redirect?to=999");
532+
533+
cy.url().should("include", "/shows/999");
534+
cy.get("h1").should("contain", "Show #999");
535+
cy.get("p").should("contain", "Flash Gordon");
536+
});
529537
});
530538

531539
describe("Preview Mode", () => {

lib/templates/createResponseObject.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const createResponseObject = ({ onResEnd }) => {
2424
res.writeHead = (status, headers) => {
2525
response.statusCode = status;
2626
if (headers) res.headers = Object.assign(res.headers, headers);
27+
28+
// Return res object to allow for chaining
29+
// Fixes: https://github.com/netlify/next-on-netlify/pull/74
30+
return res;
2731
};
2832
res.write = (chunk) => {
2933
if (!response.body) {

0 commit comments

Comments
 (0)