-
-
Notifications
You must be signed in to change notification settings - Fork 463
feat(lambda-at-edge, nextjs-component): allow handler
input for custom handler code
#649
feat(lambda-at-edge, nextjs-component): allow handler
input for custom handler code
#649
Conversation
handler
input for custom handlerhandler
input for custom handler
handler
input for custom handlerhandler
input for custom handler
handler
input for custom handlerhandler
input for custom handler
12.x build seems to be failing due to S3 error |
Don't worry about that, it's because forks don't have access to AWS keys, I will check how we can detect this is a fork and skip this. I thought I had added a check but it seems it may not be working. |
Sounds good! Ideally we could add an example custom handler somewhere in the docs but I wasn't sure where to put that or if I should start a new section.
|
handler
input for custom handlerhandler
input for custom handler
handler
input for custom handlerhandler
input for custom handler code
370d9ac
to
9d46627
Compare
Maybe start a new section? Also does this override both API and default handlers? |
@dphang Yes, it overrides both handlers. I could separate it out if desired |
I think it's ok now, any thoughts @danielcondemarin? |
Probably best to make it consistent with the other lambda inputs like memory, name etc. See readLambdaInputValue |
@danielcondemarin a custom handler probably doesn't need lambda inputs because it does not add a new lambda function. It is only a wrapper around the default lambda and it gets bundled and deployed with the default lambda handler. @emhagman @dphang can I contribute to this pull request in any way to get it merged? We're about to start using the fork of the component with custom handler in production 😄 , so I really hope this feature makes it to the library eventually. |
Yeah if you fix the conflicts I think I'm fine with it |
I fixed the conflicts. One thing to note is that now that we have We should either say somewhere in the docs that custom handlers aren't minified or pull in a tool to minify them separately. @dphang Thoughts? |
Codecov Report
@@ Coverage Diff @@
## master #649 +/- ##
==========================================
+ Coverage 80.09% 80.15% +0.05%
==========================================
Files 54 54
Lines 1718 1723 +5
Branches 358 363 +5
==========================================
+ Hits 1376 1381 +5
Misses 284 284
Partials 58 58
Continue to review full report at Codecov.
|
Yeah, I think we should just update the docs for that (you can do in another PR or I can update them). If you want, you can add Terser in another PR to minify custom handlers. I did think about minifying at build time but it makes more sense to minify in the distributed component since it does add a few seconds to build time. I figure most people won't care about how it's minified, so I had provided a sensible default set of Terser options. |
Sounds good! I can open another PR for the docs. Every year I do https://hacktoberfest.digitalocean.com as part of a Fall tradition. Do you mind adding |
Hi @emhagman, were the docs updated to show this off? I would like to write a custom handler to redirect users to a certain endpoint determined by CloudFront geolocation headers and this seems like a good fit. |
Hello! @emhagman or @dphang I have the following code that works in deploying a handler:
I put the handler in the /src directory which is why the I'm trying to add in a Lumigo tracer, which seems similar to Sentry. In your example you just require it, so I've tried the same:
and I'm not having any joy. It fails with:
What am I missing? I notice the example links to Sentry's docs which has a webpack config entry attached to it, but I don't have any similar thing to use from Lumigo. Thanks in advance. |
Just to say, I figured out a solution. For anyone else having the same issues as I am and who's not hugely hot on Serverless/NextJS internals or done a lot of webpack config (as is the case with me): In order to use a wrapper (I used Lumigo) you need to set the handler as documented here, but the code you run is bundled elsewhere and separate from the lambda you're attempting to run, so adding your dependency as a project dependency isn't going to work. What you have to do is actually:
e.g. If I want to do:
then what I have to do is actually:
which refers to:
Copy the bundle and deploy using the postBuild script: e.g.
(This assumes I manually copied the result of the dist bundle into The bundle generated in 2. is deployed in 3. and picked up in the handler in 1. Hope this helps. |
@ekasprzyk Did you ever get Lumigo Tracing working on the api-lambda. Using the process you suggest the other all work but get It seems to occur after the original handler completes so it doesn't look to be an issues on the serverless-nextjs side. |
@J3tto Yes we did, and started to expand our Lumigo tracing into another application, where it worked (except for a bug supposedly on the Lumigo end causing stats not to be reported, which is apparently fixed). We are using next/serverless versions slightly behind the current latest (10/1.x) so that might contribute. The fact that it's happening after the original handler returns is weird. |
I am trying to use custom handler in my application to forward Cloudfront headers back to browser. I have the following code in serverless.json "inputs": { "handler": "handler.handler",............}. Custom handler gets added only for default & Api lambdas. The logic written in handler works. But ISR stopped working from then. I could see the following error for Regeneration lambda in cloudwatch. This gets logged frequently after adding handler: This makes ISR to fail which is concern for my application. Is there an option to add handlers only for default lambda alone. or could we add handlers with out affecting Incremental Static Regeneration |
Allow custom Lambda handler to be set. See #648
Set the handler via
inputs.handler
and haslambda-at-edge
copysomeCustomFile.js
file fromthis.nextConfigDir
into the Lambda function folder by reading the first part ofsomeCustomFile.handler
and copying via Builder underneath:Useful for hooking into 3rd party tracing / error handling libraries such as:
https://docs.sentry.io/platforms/node/guides/aws-lambda/
For example:
{nextConfigDir}/someCustomFile.js