diff --git a/cypress/fixtures/pages/api/redirect.js b/cypress/fixtures/pages/api/redirect.js new file mode 100644 index 0000000..9923aa3 --- /dev/null +++ b/cypress/fixtures/pages/api/redirect.js @@ -0,0 +1,6 @@ +export default async function preview(req, res) { + const { query } = req; + const { to } = query; + + res.redirect(`/shows/${to}`); +} diff --git a/cypress/integration/default_spec.js b/cypress/integration/default_spec.js index 3f9180a..4df81ee 100644 --- a/cypress/integration/default_spec.js +++ b/cypress/integration/default_spec.js @@ -526,6 +526,14 @@ describe("API endpoint", () => { ); }); }); + + it("redirects with res.redirect", () => { + cy.visit("/api/redirect?to=999"); + + cy.url().should("include", "/shows/999"); + cy.get("h1").should("contain", "Show #999"); + cy.get("p").should("contain", "Flash Gordon"); + }); }); describe("Preview Mode", () => { diff --git a/lib/templates/createResponseObject.js b/lib/templates/createResponseObject.js index 23335a4..78c0a28 100644 --- a/lib/templates/createResponseObject.js +++ b/lib/templates/createResponseObject.js @@ -24,6 +24,10 @@ const createResponseObject = ({ onResEnd }) => { res.writeHead = (status, headers) => { response.statusCode = status; if (headers) res.headers = Object.assign(res.headers, headers); + + // Return res object to allow for chaining + // Fixes: https://github.com/netlify/next-on-netlify/pull/74 + return res; }; res.write = (chunk) => { if (!response.body) {