|
| 1 | +--- |
| 2 | +title: Gatsby Slice API |
| 3 | +--- |
| 4 | + |
| 5 | +> Support for the Gatsby Slice API was added in `gatsby@5.0.0`. |
| 6 | +
|
| 7 | +Gatsby includes a `<Slice>` React component and a `createSlice` action in `gatsby-node` to help speed up common updates across your site. By pulling out common components into separate HTML files, common components can be built separately and "stitched" together with existing pages. |
| 8 | + |
| 9 | +## `createSlice` action |
| 10 | + |
| 11 | +The [`createSlice`](/docs/reference/config-files/actions/#createSlice) action from the [`createPages`](/docs/reference/config-files/gatsby-node/#createPages) API is the first step in creating a new Slice. |
| 12 | + |
| 13 | +```js:title=gatsby-node.js |
| 14 | +exports.createPages = async ({ actions }) => { |
| 15 | + actions.createSlice({ |
| 16 | + id: `navigation-bar`, |
| 17 | + component: require.resolve(`./src/components/navigation-bar.js`), |
| 18 | + context: { |
| 19 | + jokeOfTheDay: `What's blue and not heavy? Light blue.`, |
| 20 | + } |
| 21 | + }) |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +| Argument | Type | Description | |
| 26 | +| ---------------------- | -------- | -------------------------------------------------------------------------- | |
| 27 | +| `id` (Required) | `string` | A unique identifier for this specific Slice. See also: [Aliases](#aliases) | |
| 28 | +| `component` (Required) | `string` | The path to the component being used as a Slice. | |
| 29 | +| `context` | `object` | An object passed to the `component` as `sliceContext`. | |
| 30 | + |
| 31 | +## `<Slice>` component |
| 32 | + |
| 33 | +The `<Slice>` component requires an `alias` prop. Any additional props will be passed to the underlying component. |
| 34 | + |
| 35 | +```jsx |
| 36 | +<Slice alias="unique-name" /> |
| 37 | +``` |
| 38 | + |
| 39 | +```jsx |
| 40 | +<Slice alias="unique-name" additionalProp="hello world" /> |
| 41 | +``` |
| 42 | + |
| 43 | +| Prop | Type | Description | |
| 44 | +| ------------------ | -------- | ------------------------------------------------------------------------------------------- | |
| 45 | +| `alias` (Required) | `string` | The Slice created in `gatsby-node` to replace this component. See also: [Aliases](#aliases) | |
| 46 | + |
| 47 | +## Aliases |
| 48 | + |
| 49 | +An "`alias`" for a Slice is the string value a page will use to identify which Slice to render. The reason `alias` is used (as opposed to `id` from [`createSlice`](/docs/reference/config-files/actions/#createSlice)) is an alias is a one-to-one mapping for each page created. By default, an `alias` is always created for each `id` given in [`createSlice`](/docs/reference/config-files/actions/#createSlice). Therefore, if `<Slice>` is given an `alias` prop of `"my-image"`, the Slice with the `id` of `"my-image"` will be used. |
| 50 | + |
| 51 | +However, if you need to customize which Slice is utilized based on the page, you can pass an `alias`-to-`id` map in [`createPage`](/docs/reference/config-files/actions/#createPage) through the `slices` key. If you map `"my-image"` to `"my-image--dog"`, any time the `"my-image"` Slice is used, it'll use the Slice with the id of `"my-image--dog"` on that page. |
| 52 | + |
| 53 | +```js:title=gatsby-node.js |
| 54 | +exports.createPages = async ({ actions }) => { |
| 55 | + const animals = ['dog', 'cat', 'giraffe'] |
| 56 | + |
| 57 | + for (const animal of animals) { |
| 58 | + // create a slice for each animal, i.e. `my-image--dog` |
| 59 | + actions.createSlice({ |
| 60 | + // highlight-next-line |
| 61 | + id: `my-image--${animal}`, |
| 62 | + component: require.resolve(`./src/components/my-image-slice.js`), |
| 63 | + context: { |
| 64 | + imagePath: `./images/${animal}.jpg`, |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + actions.createPage({ |
| 69 | + path: `/animals/${animal}`, |
| 70 | + // a page component that utilizes the MyImage slice |
| 71 | + component: require.resolve(`./src/templates/page.js`), |
| 72 | + slices: { |
| 73 | + // Any time `<Slice alias="my-image">` is seen on this page, |
| 74 | + // use the `my-image--${animal}` id |
| 75 | + // highlight-next-line |
| 76 | + 'my-image': `my-image--${animal}` |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Restrictions on using `Slice` |
| 84 | + |
| 85 | +### Nested Slices |
| 86 | + |
| 87 | +Gatsby does not support nested Slices. This means if you have a high level `<Layout>` component as a slice, other Slices cannot exist within that `<Layout>` component anywhere in the tree. |
| 88 | + |
| 89 | +### Contexts |
| 90 | + |
| 91 | +Slices do not inherit contexts from their parent components. If a context is needed, it's provider can either be added to [`wrapRootElement`](/docs/reference/config-files/gatsby-browser/#wrapRootElement) or it's value can be passed directly to the Slice (as long is it follows restrictions for [other props](#others)) |
| 92 | + |
| 93 | +```js |
| 94 | +export function MyImage() { |
| 95 | + const location = useLocation() |
| 96 | + |
| 97 | + // highlight-next-line |
| 98 | + return <Slice alias="my-image" location={location} /> |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Props |
| 103 | + |
| 104 | +#### `alias` |
| 105 | + |
| 106 | +The `alias` prop must be statically analyzable, which means it must be an inline string. |
| 107 | + |
| 108 | +```js |
| 109 | +// ⚠️ Doesn't work |
| 110 | + |
| 111 | +export function MyImage() { |
| 112 | + const alias = "my-image" |
| 113 | + |
| 114 | + // You can't use computed values for alias |
| 115 | + // highlight-next-line |
| 116 | + return <Slice alias={alias} /> |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +```js |
| 121 | +// ⚠️ Doesn't work |
| 122 | + |
| 123 | +export function MyImage() { |
| 124 | + const type = "image" |
| 125 | + |
| 126 | + // You can't use computed values for alias |
| 127 | + // highlight-next-line |
| 128 | + return <Slice alias={`my-${type}`} /> |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +```js |
| 133 | +// OK |
| 134 | + |
| 135 | +export function MyImage() { |
| 136 | + // highlight-next-line |
| 137 | + return <Slice alias="my-image" /> |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +#### `children` |
| 142 | + |
| 143 | +The children prop does not have any restrictions and can be used in typical fashion. |
| 144 | + |
| 145 | +```js |
| 146 | +// OK |
| 147 | + |
| 148 | +export function MyImage() { |
| 149 | + return ( |
| 150 | + <Slice alias="my-image"> |
| 151 | + // highlight-next-line |
| 152 | + <p>I am a caption, neat!</p> |
| 153 | + </Slice> |
| 154 | + ) |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +#### Others |
| 159 | + |
| 160 | +Any props passed to the Slice component must be serializable. |
| 161 | + |
| 162 | +This does not work: |
| 163 | + |
| 164 | +```js |
| 165 | +// ⚠️ Doesn't work |
| 166 | + |
| 167 | +export function MyImage() { |
| 168 | + const fetchImage = () => { |
| 169 | + return "/static/images/img.jpg" |
| 170 | + } |
| 171 | + |
| 172 | + // You can't use function props, as they are not serializable |
| 173 | + // highlight-next-line |
| 174 | + return <Slice alias="my-image" fetchImage={fetchImage} /> |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +However, a prop that ends with (for example) a string does work: |
| 179 | + |
| 180 | +```js |
| 181 | +// OK |
| 182 | + |
| 183 | +export function MyImage() { |
| 184 | + const fetchImage = () => { |
| 185 | + return "/static/images/img.jpg" |
| 186 | + } |
| 187 | + |
| 188 | + // `image` ends up being a string when passed to Slice |
| 189 | + // highlight-next-line |
| 190 | + return <Slice alias="my-image" image={fetchImage()} /> |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## Additional Resources |
| 195 | + |
| 196 | +- [Slices How-To Guide](/docs/how-to/performance/using-slices) |
| 197 | +- [Enable Slices API Optimizations](/docs/how-to/cloud/slices-optimization/) |
0 commit comments