Creating an Adapter
Examples
Introduction
If an adapter for your preferred deployment platform doesn’t exist yet, you can build your own. While the specifics of the adapter depend on the deployment platform and its requirements, the initial setup is the same for every adapter.
By the end of this guide you should be able to create and publish an adapter.
The adapters feature was added in gatsby@5.12.0
.
Authoring
An adapter’s entry point should have the following API:
TypeScript version
Gatsby makes a AdapterInit
type available that you can use to author your adapter. It also accepts a generic for the adapter options:
You can find all TypeScript types on GitHub.
The adapter should export a function as a default export with these object keys:
name
: Unique name of the adapter. Please follow the naming conventiongatsby-adapter-<name>
or@scope/gatsby-adapter-<name>
to make it easier for people to discover your adapter.cache
(Optional): Both handlers receivedirectories
which are the directories that should be cached/restored for a build and thereporter
instance.restore
: Hook to restoredirectories
from previous builds. Executed very early on in the build process. If the hook returnsfalse
, Gatsby will skip cache restoration.store
: Hook to storedirectories
for the current build. Executed as one of the last steps in the build process.
adapt
: Hook to take Gatsby’s output and prepare it for deployment on the adapter’s platform. Executed as one of the last steps in the build process. Details on the inputs are documented below.config
(Optional): Hook to pass information from the adapter to Gatsby so it can adjust its build process. Details on the required output is documented below.
If your adapter accepts custom options, consider setting default values to make the adapter easier to use.
cache.restore
, cache.store
, and adapt
receive the reporter
instance, so you can output structured logs to the user’s terminal. However, please don’t overdo it and keep the output to a minimum. If a user requires more details, they can always use the CLI with the --verbose
flag to get information about the adapter and logs for debugging.
adapt
The adapt
hook takes Gatsby’s output and prepares it for deployment on the adapter’s platform. It receives the following inputs:
routesManifest
: Array of objects with three different types:static
,function
, andredirect
. Each object contains all necessary information to deploy and apply these routes.static
routes will have defaultheaders
applied, which users can extend or overwrite with the HTTP headers option insidegatsby-config
. Routes will also have thetrailingSlash
option applied to their paths.functionsManifest
: Array of objects containing each function’s entry point and required files.pathPrefix
: Value of thepathPrefix
option insidegatsby-config
trailingSlash
: Value of thetrailingSlash
option insidegatsby-config
remoteFileAllowedUrls
: Array of objects containing allowed url for Gatsby Image and File CDN (if used source plugin provided those). Optionally used to automatically configure or instruct user how to configure Image and File CDN provided by adapter. Support for this field was added ingatsby@5.13.0
.
You can find the TypeScript types for these inputs on on GitHub.
The adapt
hook should do the following things:
- Apply HTTP headers to assets
- Apply redirects and rewrites. The adapter can also create its own redirects and rewrites, if necessary (e.g. mapping serverless functions to internal URLs).
- Wrap serverless functions coming from Gatsby with platform-specific code (if necessary). Gatsby will produce Express-like handlers.
- Apply trailing slash behavior and path prefix to URLs
- Possibly upload assets to CDN
config
The config
hook is useful for passing information from an adapter back to Gatsby. This optional hook can enable advanced features of adapters to e.g. workaround platform limitations. It can also warn users against using features that an adapter doesn’s currently support and would cause faulty deployments.
The config
hook has to return an object with the following keys:
deployURL
(Optional): URL representing the unique URL for an individual deployexcludeDatastoreFromEngineFunction
(Optional): Iftrue
, Gatsby will not include the LMDB datastore in the serverless functions used for SSR/DSG. Instead, it will place the LMDB datastore into thepublic
folder and later try to download the datastore from the givendeployURL
.supports
(Optional): Describe which features an adapters supportspathPrefix
(Optional): Iffalse
, Gatsby will fail the build if user tries to use pathPrefixtrailingSlash
(Optional): Provide an array of supportedtrailingSlash
options, e.g.['always']
pluginsToDisable
(Optional): Provide an array of plugin names that should be disabled when adapter is used. Purpose of this is to disable any potential plugins that serve similar role as adapter that would cause conflicts when both plugin and adapter is used at the same time.imageCDNUrlGeneratorModulePath
(Optional): Specifies the absolute path to a CommonJS module that exports a function to generate CDN URLs for images. This function, matching theImageCdnUrlGeneratorFn
type signature, takessource: ImageCdnSourceImage
,args: ImageCdnTransformArgs
andpathPrefix: string
as arguments to create optimized CDN URLs for image assets. It is particularly useful for adapting image paths to different CDN providers to optimize image delivery. Providing this will allow Gatsby from processingIMAGE_CDN
jobs during the build and instead offload the that work to be done at request time which will decrease build times for sites using Gatsby Image CDN. Support for this field was added ingatsby@5.13.0
.fileCDNUrlGeneratorModulePath
(Optional): Specifies the absolute path to a CommonJS module that exports a function to generate CDN URLs for files. This function, matching theFileCdnUrlGeneratorFn
type signature, takesFileCdnSourceImage
andpathPrefix: string
as arguments to create CDN URLs for file assets. Providing this will allow Gatsby from processingFILE_CDN
jobs during the build and instead offload the that work to be done at request time which will decrease build times for sites using Gatsby File CDN. Support for this field was added ingatsby@5.13.0
.
Running locally
If you want to test your adapter locally, you can use npm link, yarn link, or equivalent in other package managers. You’d use your adapter like so:
If you want to quickly prototype an adapter, you can also author your file(s) directly in an example project (before moving them to their own repository). Here’s how:
Create an adapter file called
gatsby-adapter-foo.js
at the root of your project:Import the adapter file into your
gatsby-config
:Once it works, don’t forget to publish your adapter so that the community can benefit from it.
Testing
You should test your adapter code to improve code quality and reliablity. We recommend using Jest or Vitest for unit testing and Cypress or Playwright for end-to-end testing.
You can copy Gatsby’s complete adapter end-to-end testing suite and use it for your own adapter.
Publishing
You’ll need to publish your adapter to npm so that others can use it. If this is your first time publishing to npm, consider following their guides.
Before you publish, keep in mind that once people start using your adapter, you’ll need to make changes responsibly (following semver) and potentially automate some of the maintenance work.
We recommend that you follow this checklist before you publish your adapter for the first time:
Choose a clear name for your adapter following this naming convention:
To publish the adapter under a scope, follow this naming convention:
Your
README
should explain to the user in concise steps how to install, use, and configure your adapter (also see How to write a plugin README). TheREADME
will be the first thing a user reviews so make sure that it’s accessible to everyone.Set
1.0.0
as theversion
field in your adapter’spackage.json
. For future releases, follow semantic versioning.Include a
keywords
field in your adapter’spackage.json
, containinggatsby
,gatsby-plugin
, andgatsby-adapter
. This way the adapter can be found through the plugin library.Include a
peerDependencies
field in your adapter’spackage.json
, containing thegatsby
version range that your adapter is compatible with.For example, if your adapter supports Gatsby 5 only (e.g. it uses an API only available in Gatsby 5), use:
If Gatsby releases a new major version and your adapter doesn’t require the new changes, you can mark your adapter as compatible with specific versions. For example, to mark your adapter as compatible with Gatsby 5 and Gatsby 6:
Add a
build
script to your adapter’spackage.json
. Thebuild
script should compile your source code to CommonJS (CJS) into adist
folder. If you’re authoring the code in TypeScript, also consider generating type definitions. If you’re authoring the code in CJS already there is no need for abuild
script.Depending on your tooling (e.g. microbundle, Babel, Parcel, tsup) adjust your build script:
Since your compiled information will be in the
dist
folder, you need to also add amain
andfiles
key to thepackage.json
.If you’ve generated TypeScript types, consider adding a
types
key.Add a
prepare
script to your adapter’spackage.json
that runs beforenpm publish
. If thebuild
script doesn’t automatically clear thedist
folder before a new build, add an additionalclean
script. This ensures that old artifacts aren’t accidentally published. For example, if you rename a file and don’t runclean
, the old file will still be published throughdist
. You could use del-cli to achieve this. It would look something like this:Follow the other recommendations from npm’s Creating a package.json file documentation, e.g. adding a
description
orauthor
field.