You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 10, 2021. It is now read-only.
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
-[Fallbacks for Pages with `getStaticPaths`](#fallbacks-for-pages-with-getstaticpaths)
52
53
-[Credits](#credits)
@@ -199,6 +200,52 @@ Currently, there is no support for redirects set in your `netlify.toml` file.
199
200
`next-on-netlify` creates one Netlify Function for each of your
200
201
SSR pages and API endpoints. It is currently not possible to create custom Netlify Functions. This feature is on our list to do.
201
202
203
+
#### Using Netlify Identity
204
+
205
+
You can use [Netlify Identity](https://docs.netlify.com/visitor-access/identity/) with `next-on-netlify`. For all pages with server-side rendering (getInitialProps*, getServerSideProps, and API routes), you can access the [clientContext object](https://docs.netlify.com/functions/functions-and-identity/#access-identity-info-via-clientcontext) via the `req` parameter.
To access Netlify Identity from pages without server-side rendering, you can create a [Next API route](https://nextjs.org/docs/api-routes/introduction) that performs identity-related logic:
230
+
231
+
```js
232
+
exportdefaultasyncfunctiongetUser(req, res) {
233
+
// Get event and context from Netlify Function
234
+
const {
235
+
netlifyFunctionParams: { event, context },
236
+
} = req;
237
+
238
+
// Access Netlify identity
239
+
const { user } =context.clientContext;
240
+
241
+
// Respond with user object
242
+
res.json({ user });
243
+
}
244
+
```
245
+
246
+
\* Note that pages using getInitialProps are only server-side rendered on initial page load and not when the user navigates client-side between pages.
0 commit comments