netlify / next-on-netlify

Build and deploy Next.js applications with Server-Side Rendering on Netlify!
MIT License
715 stars 66 forks source link

Set custom function route for Netlify identity hooks #57

Closed BrandonKlotz closed 4 years ago

BrandonKlotz commented 4 years ago

Hi, I was wondering if there is a way to set functions as custom routes that will be picked up by Netlify's identity hooks.

The function that is produced in next-on-netlify: Screen Shot 2020-10-15 at 5 14 34 PM

What I think Netlify Identity will trigger when someone signs up: Screen Shot 2020-10-15 at 5 14 50 PM

Looking at the docs: https://docs.netlify.com/functions/functions-and-identity/#trigger-serverless-functions-on-identity-events

I looked at the issue related to (https://github.com/netlify/next-on-netlify#custom-netlify-functions) but don't know that it is the same need here.

This is a weird question/issue I don't know if it is a feature request or a misunderstanding of how these two things interact. Happy to make this an example and add to docs when I better understand what's going on 😂

lindsaylevine commented 4 years ago

hey! thanks for opening this :). glad you found the custom netlify functions caveat/related issue. it's high on my list of priorities to support and will be included in the next major release. a potential workaround if you really needed it would be to build with the CLI, copy your custom function into out_functions and then deploy that.

  1. netlify deploy --build
  2. copy identity-signup.js into out_functions
  3. netlify deploy --prod
BrandonKlotz commented 4 years ago

@lindsaylevine so I was trying to follow the steps you have to deploy from the CLI but also new to using Netlify CLI, so a lot of today I learned's but wasn't able to copy over the identity-signup into out_functions (I tried also taking this folder out of my .gitignore.

Thinking about this as a feature, it would be kind of interesting not change the file names in the same way we change api routes. I am imagining a list of all the function names that trigger by events and then in lib/helpers/setupNetlifyFunctionForPage.js when we find a file that has one of these functions ['deploy-building', 'deploy-locked', 'identity-signup'] we just copy that over to the specified functions folder likely out_functions

I am trying to test this out locally and will post an update if I make any progress 😄

BrandonKlotz commented 4 years ago

Made a work in progress/draft pull to work on this idea https://github.com/netlify/next-on-netlify/pull/60

side note the pre-commit tool that checks if you have run prettier is so rad, first time seeing something like that.

This may not be the most desirable way to implement this but it is definitely helping me understand some aspects of this package better.

Update after adding skip functionality for Netlify event trigger functions:

Screen Shot 2020-10-16 at 9 08 16 PM

Update, ran netlify deploy --prod and it seemed to work! But now I am trying to verify that when a user signs up that it fires this functions.

Screen Shot 2020-10-16 at 9 23 17 PM

💭 Think if this kind of feature is accepted or desired, I'm curious if it should be an option to these by passing a flag.

Something like:

next-on-netlify --withTriggerEvents
BrandonKlotz commented 4 years ago

The good news, I think it is closer to working! 🎉

Screen Shot 2020-10-16 at 9 44 21 PM

The other good news, I am about to learn how to write a working Netlify function, because that's definitely not working 🤣

Logs for the function

9:43:47 PM: 2020-10-17T01:43:47.931Z    INFO    [request] /.netlify/functions/identity-signup
9:43:47 PM: 2020-10-17T01:43:47.940Z    ERROR   TypeError: resolver is not a function
    at apiResolver (/var/task/src/out_functions/identity-signup/nextJsPage.js:812:7)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Source: https://github.com/BrandonKlotz/netlify-identity-hooks

Update, maybe less close to a solution than I initially thought. Something I'm thinking about now is maybe we don't need to worry about coercing the fileName for Trigger events but just write custom redirects for them.

BrandonKlotz commented 4 years ago

So I found out why I was running into some weirdness.

Steps to fixing:

  1. Found similar issue here
  2. yarn add micro@canary
  3. Chose canary 9.3.5-canary.3
  4. Commit and push those changes
  5. Then build and deploy from Netlify CLI so I can use the local version of next-on-netlify

After signing up, the trigger event function properly ran!

Screen Shot 2020-10-17 at 9 03 18 AM
lindsaylevine commented 4 years ago

hey @BrandonKlotz! thanks so much for working on this :). i'm going to be super distracted this week likely with other priorities but will try to check in on your progress/PR.

BrandonKlotz commented 4 years ago

Sweet! As I understood better what I needed, #60 is a quick out-of-the-box way to support the Netlify Event Trigger Functions for those using next-on-netlify.

The feature people have requested of custom function routes likely could also achieve the needs I have here, I could see that being the solution that eclipses this feature proposal. I had a lot of fun learning about this package through trying to make it support Trigger Functions.

Also was thinking another good issue I could probably add is write unit tests for the helpers/ folder. Think it would be a good way for me to contribute and learn more about each helper.

lindsaylevine commented 4 years ago

@BrandonKlotz sorry been swamped. i think you're right @ custom functions, which is Coming Soon TM. i'm glad you had fun working with next-on-netlify and trigger functions!!! :) we'd be lucky to have you contribute tests or contribute in general to the project!