Hi Netlify Folks!
I recently decided to jump real deep into Netlify from scratch and have had a blast doing it, but want to clarify and culminate some findings into a single place for confirmation for both myself and others, and for clarity on the current state of things (April 29, 2020) to make sure my headās in the right place. Iāve scoured tons of docs, threads, and community posts here so Iāll try to reference as many as I can but this is a new community account and I may be limited on links
For starters: Netlify is awesome. What an incredible platform and system. I canāt wait to see how it grows and hope I can help in any way possible. The dev process and framework are so phenomenal. Thanks to the whole team for all the efforts and progress made. I enjoy using this platform. It brings me joy. That is not something that happens unintentionally, so thank you.
Early apologies for the length of this post. Iāve dug in pretty deep and want to make sure Iām getting things right based on my observations.
Environment
First and foremost, I want to clarify the role and correct injection of environment variables. I have some āprodā tier environment variables defined in the Netlify UI for my site, but I would like some non-prod tier env vars injected locally and/or in branch deploys and/or in preview deploys ā across both site assets / files build-time and function runtimes. Docs: āBuild Environment Variablesā and Docs: āFile-based Configurationā sub-section: āDeploy Contextsā both have a lot of syntax to imply that everything will be distributed correctly if you use the UI env vars and/or TOML env vars (and thatās true for my prod build using my prod env vars) but this community thread with @perry and @jaredh159 suggests that environment variables defined in the TOML do not get injected into functions in particular. Is that correct?
Proposing an example: I have a page called buy.html
which I inject an env var of STRIPE_PUBLIC_KEY
into and a process.js
Function that I pull STRIPE_SECRET_KEY
in to make the transaction.
So if I put my Live (āprodā) API key values into the Netlify UI, everything works great on my production build. It pulls in both live keys correctly and runs things fine , but if I also have Test (ānot-prodā) API key values defined in my netlify.toml file for the build context of ā¦anything not Production, those vars only get injected into the site static build and not the Function runtime? Is that (still) correct?
Is the current ābest-optionā to follow @foolās response in that thread and hook into $CONTEXT
?
Also thank you for the context (no pun intended) in your response, @fool!
AWS only accepts function deployments with up to 4k worth of environment variables, when appended in a string like
VAR1=val1,VAR2=val2
, and base64 encoded - more will cause the deploy to fail!
Lame ^ darn you, AWS!
Lastly, and also really importantly, does hooking into the $CONTEXT
work when running the local dev functions server? Hoping to maintain the parity of dev workflow where I can run my full site env locally using .env
variable values then push it up and have it run perfect on āprodā values when deployed ā OR have it run in a similarly valid ātestā state when itās a branch deploy or deploy preview and then swap over and run fine on āprodā values when promoted to my production deploy.
I also realize that the thread I linked to just kicked back into active discussion a few hours ago, so if others are reading this, please check there first
Dev (CLI)
Outside of whatās mentioned above, and recognizing that dev
is still in beta, is it still currently the case that event-based function hooks are not built into dev? I traced down and found the original PR where functions:invoke
was added to the (then) nelify-dev-plugin before it got merged to the CLI repo, and Iāve used this locally to kick off a submission-created.js
function, but admittedly it was tricky to figure out exactly what the shape/nesting of my --payload ./foo.json
data had to be to match production and itās a bit manual for my taste, but it does workā¦ well on 2.46.0. Iāve had a few issues related to the framework
development on 2.47.0 and 2.48.0 but thatās neither here nor there.
For others in the future: if you want to use the payload functionality (which you do), this is the exact shape your json file needs to have. Command: ntl functions:invoke submission-created --payload "./some-data.json"
file contents:
{
"payload": {
"number": 4,
"title": "Some Value",
"email": "test@example.com",
"body": null,
"data": {
"idempotency-token": "bc7d6880-8809-11ea-8af5-18d798294576",
"Business Name": "Example Biz",
"Contact Name": "Example Person",
"Contact Signature": "Example Person",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36",
"referrer": "https://example.com/contact"
},
}
And, a related question - since thereās no way to bind to local event triggers (since thereās no local forms anyway ) but you can use local functions when called explicitly, is there any actual benefit to using event-driven Functions rather than just using a ānormalā function and injecting a fetch()
into the form submission event then running the normal .submit()
for tracking in the Netlify UI / post-submission hooks (Zapier, etc.)?
I only ask because that workflow would allow the local function to run without a manual invoke.
btw, --live
is absolutely brilliant. Saves me some Ngrok time and setup thank you thank you thank you.
Environment workflow
Iām a bit confused about deploys when it comes to their capabilities as reflected in the Netlify Web UI. The web UI feels like it only shows information and data for whatās currently live in the production deploy. Forms and Functions are primary examples of this - if I create a new Form in a branch then that branch gets deployed, there is no Form listed in the web UIā¦ which makes sense if the web UI is really only for viewing production āstuffā - but is my new form actually built and setup on Netlifyās back end? Is there a way to see those submissions in a web UI at all in a branch or preview deploy?
Question applies to functions too - beyond the environment variables stuff, if I create a new function in a branch or PR, then deploy that branch or preview, that function isnāt listed in the web ui for that Site. Did it get spun up somewhere? Is it running? I saw a forum post somewhere that I canāt seem to trace down again that said āyesā to those things and that the logs for all of those functions would ultimately culminate into the log viewer for the live function (currently in āproductionā) ā which makes sense if Iām making diffs to an existing function, but how would I see the logs of a brand new function in a branch deploy if itās not listed in the site web UI?
Apologies again for the massive post. Appreciate you all and hope I can contribute answers to others with the results of all the digging Iāve done and the answers yāall have posted previously
ā
Jon