-
Notifications
You must be signed in to change notification settings - Fork 67
Expose event and context of Netlify Function in Next.js pages and API routes #119
Changes from 4 commits
1e35de0
c537545
356046e
1ba11f8
5fa6c5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default async function context(req, res) { | ||
res.json({ req, res }); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const Context = ({ context }) => <pre>{JSON.stringify(context, 2, " ")}</pre>; | ||
|
||
export const getServerSideProps = async (context) => { | ||
return { | ||
props: { | ||
context, | ||
}, | ||
}; | ||
}; | ||
|
||
export default Context; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const WaitForEmptyEventLoop = () => <p>Successfully rendered page!</p>; | ||
|
||
export const getServerSideProps = async ({ params, req }) => { | ||
// Set up long-running process | ||
const timeout = setTimeout(() => {}, 100000); | ||
|
||
// Set behavior of whether to wait for empty event loop | ||
const wait = String(params.wait).toLowerCase() === "true"; | ||
const { context: functionContext } = req.netlifyFunction; | ||
functionContext.callbackWaitsForEmptyEventLoop = wait; | ||
|
||
return { | ||
props: {}, | ||
}; | ||
}; | ||
|
||
export default WaitForEmptyEventLoop; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ const http = require("http"); | |
// Based on API Gateway Lambda Compat | ||
// Source: https://github.com/serverless-nextjs/serverless-next.js/blob/master/packages/compat-layers/apigw-lambda-compat/lib/compatLayer.js | ||
|
||
const createRequestObject = ({ event }) => { | ||
const createRequestObject = ({ event, context }) => { | ||
const { | ||
requestContext = {}, | ||
path = "", | ||
|
@@ -52,6 +52,14 @@ const createRequestObject = ({ event }) => { | |
req.rawHeaders = []; | ||
req.headers = {}; | ||
|
||
// Expose Netlify Function event and callback on request object. | ||
// This makes it possible to access the clientContext, for example. | ||
// See: https://github.com/netlify/next-on-netlify/issues/20 | ||
// It also allows users to change the behavior of waiting for empty event | ||
// loop. | ||
// See: https://github.com/netlify/next-on-netlify/issues/66#issuecomment-719988804 | ||
req.netlifyFunction = { event, context }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this name is fine. maybe netlifyFunctionData since the value itself is not really a function but 🤷♀️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True! How about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good!!!!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's now |
||
|
||
for (const key of Object.keys(multiValueHeaders)) { | ||
for (const value of multiValueHeaders[key]) { | ||
req.rawHeaders.push(key); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AWESOMEEEE