Skip to content

Commit e0cb9f6

Browse files
authored
feat(gatsby-source-drupal): Provide proxyUrl in addition to baseUrl to allow using CDN, API gateway, etc. (#36819)
1 parent f595257 commit e0cb9f6

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

packages/gatsby-source-drupal/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,29 @@ module.exports = {
250250
}
251251
```
252252

253+
## CDN
254+
255+
You can add an optional CDN or API gateway URL `proxyUrl` param. The URL can be a simple proxy of the Drupal
256+
`baseUrl`, or another URL (even containing a path) where the Drupal JSON API resources can be retrieved.
257+
258+
This option is required as Drupal doesn't know about the CDN so it returns URLs pointing to the `baseUrl`. With `proxyUrl` set, the plugin will rewrite URLs returned from Drupal to keep pointing at the `proxyUrl`
259+
260+
```javascript
261+
// In your gatsby-config.js
262+
module.exports = {
263+
plugins: [
264+
{
265+
resolve: `gatsby-source-drupal`,
266+
options: {
267+
baseUrl: `https://live-contentacms.pantheonsite.io/`,
268+
proxyUrl: `https://xyz.cloudfront.net/`, // optional, defaults to the value of baseUrl
269+
apiBase: `api`, // optional, defaults to `jsonapi`
270+
},
271+
},
272+
],
273+
}
274+
```
275+
253276
## GET Search Params
254277

255278
You can append optional GET request params to the request url using `params` option.

packages/gatsby-source-drupal/src/__tests__/index.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import got from "got"
2+
3+
const baseUrl = `http://fixture`
4+
const proxyUrl = `http://fixture-proxy`
5+
16
jest.mock(`got`, () =>
27
jest.fn(path => {
8+
if (path.includes(proxyUrl)) {
9+
path = path.replace(proxyUrl, baseUrl)
10+
}
11+
312
let last = ``
413
if (path.includes(`i18n-test`)) {
514
last = `i18n-test-`
@@ -59,7 +68,6 @@ const { handleWebhookUpdate } = require(`../utils`)
5968
describe(`gatsby-source-drupal`, () => {
6069
let nodes = {}
6170
const createNodeId = id => `generated-id-${id}`
62-
const baseUrl = `http://fixture`
6371
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
6472
const { objectContaining } = expect
6573
const actions = {
@@ -450,6 +458,38 @@ describe(`gatsby-source-drupal`, () => {
450458
expect(nodes[createNodeId(`und.article-3`)]).toBeDefined()
451459
})
452460

461+
it(`Can use the proxyUrl plugin option to use a different API url for sourcing`, async () => {
462+
got.mockClear()
463+
nodes = {}
464+
await sourceNodes(args, { baseUrl, proxyUrl })
465+
466+
let callSkipCount = 0
467+
for (const [index, call] of got.mock.calls.entries()) {
468+
if (call[0] === `http://fixture/jsonapi`) {
469+
callSkipCount++
470+
continue
471+
}
472+
473+
expect(got).toHaveBeenNthCalledWith(
474+
index + 1,
475+
expect.stringContaining(proxyUrl),
476+
expect.anything()
477+
)
478+
}
479+
480+
expect(callSkipCount).toBe(1)
481+
expect(got).toBeCalledTimes(8)
482+
483+
expect(Object.keys(nodes).length).not.toEqual(0)
484+
expect(nodes[createNodeId(`und.file-1`)]).toBeDefined()
485+
expect(nodes[createNodeId(`und.file-2`)]).toBeDefined()
486+
expect(nodes[createNodeId(`und.tag-1`)]).toBeDefined()
487+
expect(nodes[createNodeId(`und.tag-2`)]).toBeDefined()
488+
expect(nodes[createNodeId(`und.article-1`)]).toBeDefined()
489+
expect(nodes[createNodeId(`und.article-2`)]).toBeDefined()
490+
expect(nodes[createNodeId(`und.article-3`)]).toBeDefined()
491+
})
492+
453493
it(`Verify JSON:API includes relationships`, async () => {
454494
// Reset nodes and test includes relationships.
455495
Object.keys(nodes).forEach(key => delete nodes[key])

packages/gatsby-source-drupal/src/gatsby-node.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ exports.sourceNodes = async (
155155
globalReporter = reporter
156156
const {
157157
baseUrl,
158+
proxyUrl = baseUrl,
158159
apiBase = `jsonapi`,
159160
basicAuth = {},
160161
filters,
@@ -534,6 +535,11 @@ ${JSON.stringify(webhookBody, null, 4)}`
534535
}
535536
}
536537

538+
// If proxyUrl is defined, use it instead of baseUrl to get the content.
539+
if (proxyUrl !== baseUrl) {
540+
url = url.replace(baseUrl, proxyUrl)
541+
}
542+
537543
let d
538544
try {
539545
d = await requestQueue.push([
@@ -833,6 +839,9 @@ exports.pluginOptionsSchema = ({ Joi }) =>
833839
baseUrl: Joi.string()
834840
.required()
835841
.description(`The URL to root of your Drupal instance`),
842+
proxyUrl: Joi.string().description(
843+
`The CDN URL equivalent to your baseUrl`
844+
),
836845
apiBase: Joi.string().description(
837846
`The path to the root of the JSONAPI — defaults to "jsonapi"`
838847
),

0 commit comments

Comments
 (0)