Replies: 5 comments 8 replies
-
I'm so excited to see this happening. The initial read looks like this covers a lot of struggles I've seen in the development of My Questions:
Answers to your questions
|
Beta Was this translation helpful? Give feedback.
-
Hi @moonmeister 👋 Glad to hear that you’re excited, we are, too :) I want to preface my answers with this: With that out of the way, here are some answers:
In regards to Fetch/Express-like handlers: While Adapters is a new API, the functions we compile stayed the same since they were introduced in Gatsby 3.7. We unfortunately can’t just change the signature of those functions, as Gatsby Cloud and other hosts rely on those being Express-like. Ideally, in the next Gatsby major we’d make the switch. We were just curious how much interest there would be about this :) Standardizing on fetch API (both compiled functions and how users would write functions) is definitely the way we see things going forward but we need to be mindful about scope-creep and breaking changes. |
Beta Was this translation helpful? Give feedback.
-
A status update since the last post and since the initial release of the RFC:
Follow the milestone for more details: https://github.com/gatsbyjs/gatsby/milestone/16 We've published a new canary version, you can delete your lock files and install the new version of |
Beta Was this translation helpful? Give feedback.
-
Hi, I was wondering if it is possible to deploy processed Gatsby functions to Netlify manually (with Netlify CLI)? The processed function in |
Beta Was this translation helpful? Give feedback.
-
It seems to me that at the moment gatsby-adapter-netlify does not support Image CDN, i.e. creating a function when |
Beta Was this translation helpful? Give feedback.
-
Summary
We intend to add an additional type of plugin to Gatsby called “Adapter”. Adapters are responsible for taking the production output from Gatsby and turning it into something your deployment platform (e.g. Netlify, Vercel, Self-Hosted, etc.) understands.
We want to make it easier to deploy and host Gatsby on any platform.
As part of this work we also intend to add a
headers
andadapter
option togatsby-config
.You can follow the Adapters milestone to see the progress and PRs.
Try it out
Using a demo project
Clone the existing starter gatsby-adapters-alpha-demo
cd gatsby-adapters-alpha-demo npm install --legacy-peer-deps
Follow its README to either deploy it to Netlify, run
ntl serve
or use the local adapter.You can also individually install two packages and its canary versions:
gatsby@alpha-adapters
gatsby-adapter-netlify@alpha-adapters
You can find in-progress documentation here: #38233
Using an online IDE
Basic example
By default, Gatsby will have a manifest of popular adapters it’ll reference during a build. This enables zero-configuration deployments on those deployment platforms.
For example, on Netlify
gatsby
will automatically installgatsby-adapter-netlify
. You can manually install it inside yourdependencies
if you want to skip the auto-install on each build. The adapter will then setup everything needed to host Gatsby on Netlify.For this zero-configuration deployments no manual user interaction is needed (e.g. no installation/usage of a “auto” package).
If you want to use an adapter that is not part of Gatsby’s manifest, you can install it into your
dependencies
and add it to yourgatsby-config
like so:If the adapter is taking in options, you can set them:
As part of this RFC we also saw the need to allow setting HTTP headers for request paths which you’ll be able to do with the new
headers
option:Motivation
Right now, plugins for deployment platforms like gatsby-plugin-netlify, @vercel/gatsby-plugin-vercel-builder or gatsby-plugin-fastify are reaching into Gatsby’s internals to figure out the same things:
These information are currently only available inside Gatsby plugins and thus the deployment platforms needs to add their plugin to
gatsby-config
during a build, messing with a user’s configuration. This is brittle.We want to make it easier to write glue-code between Gatsby and a deployment platform by providing all these information (including headers) in a structured way. In the end, the hard problems should be solved by Gatsby itself and the adapter for the platform should just consume all the information and adapt it.
For popular platforms no user interaction at all should be necessary.
Detailed design
You might be familiar with our proposed APIs so, credit where credit's due, we’d like to point out:
headers
option is inspired by Next.js (Setting headers)Adapters
An adapter is a npm package that can be authored in CJS, ESM, or TS and for now has to be compiled to CJS. You’ll be able to publish your adapter as ESM once we implement ESM in TS support as part of ESM in Gatsby files.
The entrypoint of the adapter has to export a function as a default export with the following shape. Here’s an example adapter and what it could do:
Here’s an explanation of the code from top to bottom:
AdapterInit
type. It takes in a generic which should be your adapter options if you have anygatsby-adapter-X
or@scope/gatsby-adapter-X
cache
handlers receive thedirectories
that should be cached/restored for a build. At the moment these are.cache
andpublic
. The cache restoration happens at the correct time during the build so that as little work as possible needs to be redone.adapt
function receivesroutesManifest
andfunctionsManifest
(which will be explained further below) and this is the core part of the adapter. With these information provided there you’ll hopefully be able to adapt the output to your deployment platform.reporter
instance you’re used to, so you can output structured logs to the user’s terminal. However, please don’t overdo it and keep the output to a minimum. The user will already get information what adapter is used and can debug things further by running inverbose
mode.routesManifest & functionsManifest
Both the routesManifest and functionsManifest are an array of objects. Here are the type definitions for routesManifest:
You’ll learn more about the
headers
further below.The
routesManifest
array is sorted by specificity, with the highest one at the top. This way you can use the first entry that matches your URL/pattern.And here’s the functionsManifest:
The
pathToEntryPoint
files are being generated by Gatsby and should allow for an easier deployment. This includes some built-in guardrails for SSR/DSG functions. The functions export Express-like handlers that the platform can use (or further adapt). We’re considering to potentially change those function signatures tofetch
API Response/Request format — please let us know if that would be something you’d also be interested in.We hope that these manifests give you all the information you need to deploy all artifacts to various platforms. If you’re missing something, please let us know!
How it works under the hood
So that’s how you author an adapter and what information is available. Now, if you’re interested you can read up on how it’s all done on Gatsby’s side.
Gatsby has a manifest file in the shape of:
This manifest is responsible for the zero-configuration deployments available on certain deployment targets. We’ll only be adding entries to this manifest if the adapter is meeting our quality standards and is a widely used platform.
When a build is triggered, Gatsby fetches this manifest (first from unpkg.com, falling back to the file inside
gatsby
) and finds the adapter to use via thetest
function. If no adapter is found, things just continue without any changes.Then Gatsby tries to resolve the adapter in 3 steps:
dependency
/devDependency
), resolve it.cache/adapters
.cache/adapters
and resolve itIf you define an adapter through the
adapter
option insidegatsby-config
the manifest and zero-configuration codepaths are completely skipped and your defined adapter is directly used. Either way, once Gatsby figured out which adapter to run, its functions are run and the adapter can do its work.Headers
The
headers
option insidegatsby-config
accepts an array of objects in the following shape:source
is a request path pattern, theheaders
are key-value pairs. You won’t need to worry about trailing slashes forsource
paths as it all gets normalized.If headers match the same path and set the same header key, the header with the highest specificity will override the other entries. Otherwise headers will be merged together. The order inside the
headers
array doesn’t matter. You’ll see two examples further below.Routes inside the
routesManifest
will already have some default headers (which you can override if you want). If your header path pattern contains dynamic segments like:
or*
the routes will be scored and sorted by specificity, where the most specific route will “win” (be the last entry to override everything). Routes like/some-path
are the most specific ones.As an example, the static route wins against the dynamic and default header because it has the highest specificity:
And for dynamic segments:
Adoption strategy
While we’d have liked to make this backwards compatible, it’s not feasible and therefore this feature set will only be available in Gatsby 5 and newer. The existing plugins for the deployment platforms will continue to work as usual and there is no need to deprecate/remove any of the APIs that they are using at this time.
As mentioned in the beginning, Gatsby will have a manifest of popular adapters for auto-installation. If you’re not deploying to those platforms and are also not manually installing & using an adapter, nothing will change in the Gatsby build.
If your deployment platform already has a Gatsby plugin, you’ll need to write an adapter first before people can migrate.
We’ll author the
gatsby-adapter-netlify
adapter and are happy to assist with other adapters.If you then want your adapter to be added to the manifest for zero-configuration deployments, the adapter has to meet our quality standards (e.g. all features are supported) and is a widely used platform. Not every adapter will be added to the manifest.
Users who want to convert existing plugins or author new adapters should:
gatsby-adapter-X
naming scheme (not an enforcement but encouraged)gatsby-adapter-netlify
How we teach this
We’ll add some guides:
headers
option ingatsby-config
Where necessary, guides related to deployment will also be adjusted.
How can you help?
routesManifest
andfunctionsManifest
that would be critical for your use case?functionsManifest
will be Express-like handlers. How important would it be for you to have these in the format offetch
API Response/Request format?Beta Was this translation helpful? Give feedback.
All reactions