Skip to content

Commit

Permalink
feature(docs): Document how functions work in plugins/themes (#33263)
Browse files Browse the repository at this point in the history
* feature(docs): Document how functions work in plugins/themes

* Update docs/docs/reference/functions/getting-started.md

Co-authored-by: Megan Sullivan <megan@gatsbyjs.com>

* Update docs/docs/reference/functions/getting-started.md

Co-authored-by: Megan Sullivan <megan@gatsbyjs.com>

* Update getting-started.md

* Update getting-started.md

* Update getting-started.md

Co-authored-by: Megan Sullivan <megan@gatsbyjs.com>
  • Loading branch information
KyleAMathews and meganesu committed Sep 21, 2021
1 parent a960906 commit 0566be4
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/docs/reference/functions/getting-started.md
Expand Up @@ -190,6 +190,16 @@ export default function FormPage() {
}
```

## Functions in plugins and themes

Plugins and themes can ship functions! This is powerful as it lets you pair frontend code with backend code. For example, if you built a plugin for an authorization service that includes a login component, you could ship alongside the component, a serverless function the component can use to connect to the remote API.

### Namespacing

Plugin/theme functions work exactly the same as normal functions except their routes must be created under the plugin's namespace e.g. `${PLUGIN_ROOT}/src/api/{pluginName}/my-plugin-function.js`.

Shadowing with functions works similar to how shadowing works in general. You can shadow a plugin/theme function by copying the file from the plugin/theme's `src` tree into your site's `src` tree. For example, to shadow the `/gatsby-plugin-cool/do-something` function from the `gatsby-plugin-cool` plugin, you'd copy `node_modules/gatsby-plugin-cool/src/api/gatsby-plugin-cool/do-something.js` to `src/api/gatsby-plugin-cool/do-something.js`. From there, you can overwrite the implementation of the `/do-something` function as you normally would.

## Limitations

- Gatsby Functions do not support dynamic routes in Gatsby Cloud at the moment
Expand Down

0 comments on commit 0566be4

Please sign in to comment.