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

Commit a093007

Browse files
FinnWoelmlindsaylevine
authored andcommitted
Add support for res.redirect in API routes
Copy over the files from the next-aws-lambda package and manually bundle them into our Netlify Functions. This gives us more flexibility to customize the compatibility layer between Netlify Functions and Next.js. For now, no changes have been made to the next-aws-lambda files and they have been copied as-is. next-aws-lambda source: https://github.com/serverless-nextjs/serverless-next.js/tree/master/packages/compat-layers/apigw-lambda-compat
1 parent 6f1a1b3 commit a093007

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed
+6
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

+8
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

+4
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)