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

Expose event and context of Netlify Function in Next.js pages and API routes #119

Merged
merged 5 commits into from
Dec 22, 2020

Conversation

FinnWoelm
Copy link
Collaborator

@FinnWoelm FinnWoelm commented Dec 15, 2020

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

Example

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;

Use Cases

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

This object is not needed on Netlify. It is a relict of next-aws-lambda.
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.netlifyFunction.event
- req.netlifyFunction.context

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))
Test that users can modify the Netlify Function's
callbackWaitsForEmptyEventLoop behavior via the newly exposed function
event and context objects. When callbackWaitsForEmptyEventLoop is true
(default), the function does not finish until all async processes and
timeouts are completed (or cleared). The user can set this to false to
not wait for other processes to finish.
@FinnWoelm
Copy link
Collaborator Author

@lindsaylevine @ehmicky: Is netlifyFunction the appopriate name for making the event and context objects available on the req object?

I opted for netlifyFunction, because I wanted a name that makes it clear that this is not Next.js behavior and thus won't work on any other platform. But if you think something else would be better, let's change it! 😊 (Best to change it now, because changing it later would probably be a breaking change and require a major version bump.)

@FinnWoelm
Copy link
Collaborator Author

PS: Why make the event and context objects available via the req parameter? Because the req and res objects are passed as-is through Next.js to pages and API routes (

if (isNextPage) return nextPage.render(req, res);
). We can add any number of custom properties onto the req (or res) object and they will be available to users.

@FinnWoelm FinnWoelm added enhancement type: feature code contributing to the implementation of a feature and/or user facing functionality labels Dec 15, 2020
Add instructions, details, and an example for using Netlify Identity 
with next-on-netlify.
Copy link
Contributor

@lindsaylevine lindsaylevine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THIS IS SO DOPE, YAY

```

\* Note that pages using getInitialProps are only server-side rendered on initial page load and not when the user navigates client-side between pages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWESOMEEEE

// 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 };
Copy link
Contributor

Choose a reason for hiding this comment

The 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 🤷‍♀️

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! How about netlifyFunctionParams, since it's not just data but also some functions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good!!!!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's now netlifyFunctionParams :)

Expose event and context of Netlify Function in Next.js pages and API
routes as `netlifyFunctionParams` (rather than `netlifyFunction`).
The new name is clearer, since we're just making the parameters of the
function available and not the function itself.
@FinnWoelm
Copy link
Collaborator Author

If we're happy with netlifyFunctionParams, this is ready to be merged! LMK and I can do the merging :)

@lindsaylevine
Copy link
Contributor

do it!!!

@FinnWoelm FinnWoelm merged commit f49155d into main Dec 22, 2020
@FinnWoelm FinnWoelm deleted the expose-function-context branch December 22, 2020 02:53
lindsaylevine added a commit that referenced this pull request Jan 3, 2021
- Support for i18n in Next 10 ([#75](#75))
- dependabot: node-notifier from 8.0.0 to 8.0.1 ([#125](#125))
- Expose Netlify function params as netlifyFunctionParams ([#119](#119))
- Fix: local cypress cache control ([#118](#118))
- Fix: add res.finished to createResponseObject ([#117](#117))
- Fix: Windows support ([#101](#101))
- Improve logs for specified functions/publish dirs ([#100](#100))
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Netlify Identity clientContext is not passed to API endpoints and pages
2 participants