This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'expose-function-context' into main (#119)
When a page is being SSR-ed by a Netlify Function, allow users to access the function's event and context parameters. These can be accessed as a property on the `req` object in all SSR-ed pages and in API routes: - req.netlifyFunctionParams.event - req.netlifyFunctionParams.context ```js const Page = () => <p>Hello World!</p>; export const getServerSideProps = async ({ req }) => { // Get event and context from Netlify Function const { netlifyFunctionParams: { event, context }, } = req; // Access Netlify identity const { identity, user } = context.clientContext; // Modify callbackWaitsForEmptyEventLoop behavior context.callbackWaitsForEmptyEventLoop = false; // See how much time is remaining before function timeout const timeRemaining = context.getRemainingTimeInMillis(); return { props: {}, }; }; export default Page; ``` This allows users to access/leverage Netlify identity for their Next.js page (see #20). It also allows users to modify the callbackWaitsForEmptyEventLoop behavior (see #66 (comment)). Fixes #20
- Loading branch information
Showing
8 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
17 changes: 17 additions & 0 deletions
17
cypress/fixtures/pages/getServerSideProps/wait-on-empty-event-loop/[wait].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.netlifyFunctionParams; | ||
functionContext.callbackWaitsForEmptyEventLoop = wait; | ||
|
||
return { | ||
props: {}, | ||
}; | ||
}; | ||
|
||
export default WaitForEmptyEventLoop; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters