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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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
headersandadapteroption 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-depsFollow its README to either deploy it to Netlify, run
ntl serveor use the local adapter.You can also individually install two packages and its canary versions:
gatsby@alpha-adaptersgatsby-adapter-netlify@alpha-adaptersYou 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
gatsbywill automatically installgatsby-adapter-netlify. You can manually install it inside yourdependenciesif 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
dependenciesand add it to yourgatsby-configlike 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
headersoption: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-configduring 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:
headersoption 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:
AdapterInittype. It takes in a generic which should be your adapter options if you have anygatsby-adapter-Xor@scope/gatsby-adapter-Xcachehandlers receive thedirectoriesthat should be cached/restored for a build. At the moment these are.cacheandpublic. The cache restoration happens at the correct time during the build so that as little work as possible needs to be redone.adaptfunction receivesroutesManifestandfunctionsManifest(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.reporterinstance 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 inverbosemode.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
headersfurther below.The
routesManifestarray 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
pathToEntryPointfiles 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 tofetchAPI 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 thetestfunction. 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/adaptersand resolve itIf you define an adapter through the
adapteroption insidegatsby-configthe 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
headersoption insidegatsby-configaccepts an array of objects in the following shape:sourceis a request path pattern, theheadersare key-value pairs. You won’t need to worry about trailing slashes forsourcepaths 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
headersarray doesn’t matter. You’ll see two examples further below.Routes inside the
routesManifestwill 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-pathare 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-netlifyadapter 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-Xnaming scheme (not an enforcement but encouraged)gatsby-adapter-netlifyHow we teach this
We’ll add some guides:
headersoption ingatsby-configWhere necessary, guides related to deployment will also be adjusted.
How can you help?
routesManifestandfunctionsManifestthat would be critical for your use case?functionsManifestwill be Express-like handlers. How important would it be for you to have these in the format offetchAPI Response/Request format?Beta Was this translation helpful? Give feedback.
All reactions