|
1 | 1 | # Gatsby Image CDN on Netlify |
2 | 2 |
|
3 | | -Gatsby Image CDN is a new feature available in the prerelease version of Gatsby. |
4 | | -Instead of downloading and processing images at build time, it defers processing |
5 | | -until request time. This can greatly improve build times for sites with remote |
6 | | -images, such as those that use a CMS. Netlify includes full support for Image |
7 | | -CDN, on all plans. |
8 | | - |
9 | | -When using the image CDN, Gatsby generates URLs of the form |
10 | | -`/_gatsby/image/...`. On Netlify, these are served by a |
11 | | -[builder function](https://docs.netlify.com/configure-builds/on-demand-builders/), |
12 | | -powered by [sharp](https://sharp.pixelplumbing.com/) and Nuxt's |
13 | | -[ipx image server](https://github.com/unjs/ipx/). It supports all image formats |
14 | | -supported by Gatsby, including AVIF and WebP. |
15 | | - |
16 | | -On first load there will be a one-time delay while the image is resized, but |
17 | | -subsequent requests will be super-fast as they are served from the edge cache. |
| 3 | +Gatsby Image CDN is a feature available since Gatsby v4.10.0. Instead of |
| 4 | +downloading and processing images at build time, it defers processing until |
| 5 | +request time. This can greatly improve build times for sites with remote images, |
| 6 | +such as those that use a CMS. Netlify includes full support for Image CDN, on |
| 7 | +all plans. |
18 | 8 |
|
19 | 9 | ## Enabling the Image CDN |
20 | 10 |
|
21 | | -To enable the Image CDN during the beta period, you should set the environment |
22 | | -variable `GATSBY_CLOUD_IMAGE_CDN` to `true`. |
| 11 | +To enable the Image CDN, you should set the environment variable |
| 12 | +`NETLIFY_IMAGE_CDN` to `true`. You will also need to declare allowed image URL |
| 13 | +patterns in `netlify.toml`: |
23 | 14 |
|
24 | | -Image CDN currently requires the beta version of Gatsby. This can be installed |
25 | | -using the `next` tag: |
| 15 | +```toml |
| 16 | +[build.environment] |
| 17 | +NETLIFY_IMAGE_CDN = "true" |
26 | 18 |
|
27 | | -```shell |
28 | | -npm install gatsby@next gatsby-plugin-image@next gatsby-plugin-sharp@next gatsby-transformer-sharp@next |
| 19 | +[images] |
| 20 | +remote_images = [ |
| 21 | + 'https://example1.com/*', |
| 22 | + 'https://example2.com/*' |
| 23 | +] |
29 | 24 | ``` |
30 | 25 |
|
31 | | -Currently Image CDN supports Contentful and WordPress, and these source plugins |
32 | | -should also be installed using the `next` tag: |
| 26 | +Exact URL patterns to use will depend on CMS you use and possibly your |
| 27 | +configuration of it. |
33 | 28 |
|
34 | | -```shell |
35 | | -npm install gatsby-source-wordpress@next |
36 | | -``` |
| 29 | +- `gatsby-source-contentful`: |
37 | 30 |
|
38 | | -or |
| 31 | + ```toml |
| 32 | + [images] |
| 33 | + remote_images = [ |
| 34 | + # <your-contentful-space-id> is specified in the `spaceId` option for the |
| 35 | + # gatsby-source-contentful plugin in your gatsby-config file. |
| 36 | + "https://images.ctfassets.net/<your-contentful-space-id>/*" |
| 37 | + ] |
| 38 | + ``` |
39 | 39 |
|
40 | | -```shell |
41 | | -npm install gatsby-source-contentful@next |
42 | | -``` |
| 40 | +- `gatsby-source-drupal`: |
| 41 | + |
| 42 | + ```toml |
| 43 | + [images] |
| 44 | + remote_images = [ |
| 45 | + # <your-drupal-base-url> is speciafied in the `baseUrl` option for the |
| 46 | + # gatsby-source-drupal plugin in your gatsby-config file. |
| 47 | + "<your-drupal-base-url>/*" |
| 48 | + ] |
| 49 | + ``` |
| 50 | + |
| 51 | +- `gatsby-source-wordpress`: |
| 52 | + |
| 53 | + ```toml |
| 54 | + [images] |
| 55 | + remote_images = [ |
| 56 | + # <your-wordpress-url> is specified in the `url` option for the |
| 57 | + # gatsby-source-wordpress plugin in your gatsby-config file. |
| 58 | + # There is no need to include `/graphql in the path here` |
| 59 | + "<your-wordpress-url>/*" |
| 60 | + ] |
| 61 | + ``` |
43 | 62 |
|
44 | | -Gatsby will be adding support to more source plugins during the beta period. |
45 | | -These should work automatically as soon as they are added. |
| 63 | +Above examples are the most likely ones to be needed. However if you configure |
| 64 | +your CMS to host assets on different domain or path, you might need to adjust |
| 65 | +the patterns accordingly. |
46 | 66 |
|
47 | | -## Using the Image CDN |
| 67 | +## How it works |
48 | 68 |
|
49 | | -Your GraphQL queries will need updating to use the image CDN. The details vary |
50 | | -depending on the source plugin. For more details see |
51 | | -[the Gatsby docs](https://support.gatsbyjs.com/hc/en-us/articles/4522338898579) |
| 69 | +When using the Image CDN, Gatsby generates URLs of the form |
| 70 | +`/_gatsby/image/...`. On Netlify, these are served by a function that translates |
| 71 | +Gatsby Image CDN URLs into Netlify Image CDN compatible URL of the form |
| 72 | +`/.netlify/images/...`. For more information about Netlify Image CDN, |
| 73 | +documentation can be found [here](https://docs.netlify.com/image-cdn/overview). |
0 commit comments