RFC: Gatsby Functions #27667
Replies: 10 comments 16 replies
-
Sounds like a great idea! 🎉 Have you considered about streaming responses and/or websockets? |
Beta Was this translation helpful? Give feedback.
-
Will this feature be paid like Netlify? Where you pay if you exceed a certain number of calls per month? |
Beta Was this translation helpful? Give feedback.
-
I might be wrong, but doesn't Gatsby Cloud build the site and then host it on a provider of your choice? Would the site still be deployed on Netlify, Vercel, Firebase, etc but the serverless functions be hosted by Gatsby Cloud? |
Beta Was this translation helpful? Give feedback.
-
Excited to see this. One thing I didn't see mentioned above -- Netlify functions (and probably other clouds' equivalents?) have environment variable support where the set of environment variables that you configure to be available in the build are also available within Netlify functions via |
Beta Was this translation helpful? Give feedback.
-
If anyone is excited to try this out, I built a small repo that deploys functions inside |
Beta Was this translation helpful? Give feedback.
-
Interesting! Have you considered Frontend Application Bundles (FABs) as a compile target? They've already got deployment adapters for Cloudflare Workers, AWS Lambda (& Lambda@Edge) and self-hosted NodeJS, and have a native Fetch-based API for server-side logic. |
Beta Was this translation helpful? Give feedback.
-
First Class TypeScript support would be 👌🏻 Also, the ability to run the lastest NodeJS release |
Beta Was this translation helpful? Give feedback.
-
i would vote for (3)custom paths and (1)custom src path, if a website has already an page with this generic name |
Beta Was this translation helpful? Give feedback.
-
👋 Howdy to everyone who commented along or was following our original conversations around Functions. We've added a Beta for our initial version and you can check it out and get started here: https://www.gatsbyjs.com/docs/how-to/functions/. Functions are included as part of the Hosting plans on Gatsby Cloud (including the Free tier), and you read more about the limits on our pricing page. Happy building, and let us know what you think! There's an umbrella issue where you can add any feedback: #30735 |
Beta Was this translation helpful? Give feedback.
-
Hello everyone! With Gatsby v3.7 (https://www.gatsbyjs.com/docs/reference/release-notes/v3.7) Gatsby Functions are available to everyone now. You can also use them on Gatsby Cloud now: https://www.gatsbyjs.com/blog/announcing-launch-of-functions-on-gatsby-cloud If you have questions or feature requests regarding Gatsby Functions, please use GitHub discussions: https://github.com/gatsbyjs/gatsby/discussions If you have found a bug, please report an issue: https://github.com/gatsbyjs/gatsby/issues/new?assignees=&labels=type%3A+bug&template=bug_report.md |
Beta Was this translation helpful? Give feedback.
-
Summary
Gatsby Functions are serverless functions hosted on Gatsby Cloud that are deployed along with your Gatsby site. Creating a new function is as simple as creating a file in the src/functions directory of your Gatsby site.
Gatsby Functions are available in your local development environment automatically and running a production build will now contain not just your static assets, but also your functions compiled with their dependencies into a single file, ready to upload to Gatsby Cloud.
Basic example
Let’s look at a quick example of a Gatsby Function. Create a folder in your Gatsby site’s
src
directory calledapi
and create a file calledtime.js
.Now, type following code in
time.js
And that’s it! You just wrote your first Gatsby Function. This will be available on
/api/time
in your local development environment and in production when you push to Gatsby Cloud.Detailed design
Gatsby Functions are native to Gatsby Cloud. Functions are auto discovered from your project’s
src/api
directory.Every Gatsby Function is an HTTP function and is deployed to an HTTP endpoint. To author a Gatsby Function, a user can write an express compatible handler as a default export in a file.
Dependencies for Functions can be included in the project’s package.json and they will be bundled on build.
Hence, each function receives two parameters,
request
andresponse
Both of these will broadly conform to the Node HTTP spec including
request.body
,request.headers
andresponse.json
,response.status
,response.send
.Configuration for Functions may be specified in a named export called config in the function’s file. All configuration is completely optional. Options include HTTP method and timeout, CORS domain whitelist.
During
gatsby develop
, all functions in a project are automatically available on localhost:8000When running
gatsby build
, the functions for a project will be built and saved into the.cache
directory. This directory will contain a single built file for each function. This file will be a transpiled and a bundled output file including all of the function’s dependencies.When a user runs
gatsby serve
, functions for a project will also be hosted on the express server on localhost:9000. This should make local development simple and straightforward. Logs for Gatsby Functions will be included inline with other logs for thedevelop
orserve
process.TypeScript is supported out of the box for Functions.
Since all Gatsby Functions get deployed to HTTP endpoints, consuming them is like calling any other API. For example, calling our time function that we created above is as simple as calling it via fetch.
Users can assume that functions will be hosted on the same domain as the static assets and call them via relative urls.
Motivation
30% of our Gatsby users (from a sample size of a 175 on this poll) said they use serverless functions often when building a Gatsby site. These are typically used to access some dynamic data at runtime or as an endpoint to POST some data to a third party service or database.
Currently these users use services like Netlify Functions, Vercel, AWS Lambda and many more. These services are great but their development experience requires users to step out of Gatsby to write a serverless function. They also require users to run other CLI tools as part of their development environment.
Gatsby Functions aims to make this much simpler by enabling our users to create serverless functions right in their Gatsby repository, without needing to setup any other service.
Drawbacks
One possible drawback is that Gatsby Functions will only support Gatsby Cloud on initial release. However, we will work with the community to enable support on Netlify, Vercel and other existing platforms via plugins like
gatsby-plugin-netlify
andgatsby-plugin-vercel
.Alternatives
An alternative strategy would include designing Gatsby Functions to support multiple platforms out of the box. We are currently not considering this because:
Several platforms have their own accepted function signatures and configuration file formats that may vary considerably from one to another. Supporting all of these would add significant complexity and increase maintenance burden.
Platforms have their own limitations. An example of this is the lack of multipart form data support on Netlify. We would prefer not to limit functionality in our implementation of Functions due to a limitation of a supported platform.
Adoption strategy
This is not a breaking change at the moment. We expect users to adopt this for their next Gatsby project when they find the need to use a serverless function. Codemods for existing projects can be complex so we do not intend to provide these at release.
However, given that our API function signature intends to converge as much as possible with the majority of the ecosystem, we expect incremental adoption to not be too difficult and definitely worth the simplified development experience.
How we teach this
Gatsby Functions should be documented with the rest of the Gatsby documentation at gatsbyjs.com. We should also create a few quick start videos on how to create Gatsby Functions. These can live on egghead.io with the rest of our videos.
We should also create a new official starter that includes a Gatsby Function out of the box so users can create their new sites using this starter and edit the included Gatsby Function to get started quickly.
We should also create several examples on Gatsby Functions that perform common tasks including authentication, a POST handler for uploading an image, accepting a form response and writing it to a database and so on.
Unresolved questions
Please let us know what you think of this API, what are you missing? What use cases do you want us to cover? Looking forward to hearing more!
Beta Was this translation helpful? Give feedback.
All reactions