Skip to content

Commit 85b8749

Browse files
lukemortonpieh
authored andcommitted
fix(gatsby-source-wordpress): adjust how endpoint urls are constructed to fix fetching for wordpress.com hosted sites and proxied urls (#10624)
## Description As reported in #10427 some WordPress.com sites return a `_links.$.self` with a fullpath `https://public-api.wordpress.com/wp/v2/sites/$site/users/me` and others without `https://public-api.wordpress.com/`. This means it is not a reliable way of removing the `baseUrl` from the `fullUrl`. Instead we pass the route key in as the `fullPath` rather than the `fullUrl`. We then extract the `basePath` from the `baseUrl` before finally removing the `basePath` from the `fullPath`. We also need to extract the raw entity type from the `fullPath` rather than route. We also need to build the `fullUrl` from `baseUrl` and `fullPath` so I introduced the `buildFullUrl` function to do just that. I’ve included tests for both WordPress.com and WordPress.org. ## Related Issues Fixes #10427.
1 parent bc7d472 commit 85b8749

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const fetch = require(`../fetch`)
2+
3+
describe(`Fetching`, () => {
4+
it(`gets raw entity type for wordpress.com`, () => {
5+
const key = `/wp/v2/sites/example.wordpress.com/posts`
6+
const path = fetch.getRawEntityType(key)
7+
expect(path).toEqual(`posts`)
8+
})
9+
10+
it(`gets route path for wordpress.com by removing base path from the full path`, () => {
11+
const key = `/wp/v2/sites/example.wordpress.com/posts`
12+
const baseUrl = `https://public-api.wordpress.com/wp/v2/sites/example.wordpress.com`
13+
const path = fetch.getRoutePath(baseUrl, key)
14+
expect(path).toEqual(`/posts`)
15+
})
16+
17+
it(`gets route path for wordpress.org by removing base path from the full path`, () => {
18+
const key = `/wp-json/wp/v2/posts`
19+
const baseUrl = `http://dev-gatbsyjswp.pantheonsite.io/wp-json`
20+
const path = fetch.getRoutePath(baseUrl, key)
21+
expect(path).toEqual(`/wp/v2/posts`)
22+
})
23+
24+
it(`builds full URL correctly for wordpress.com`, () => {
25+
const key = `/wp/v2/sites/example.wordpress.com/posts`
26+
const baseUrl = `https://public-api.wordpress.com/wp/v2/sites/example.wordpress.com`
27+
const fullUrl = fetch.buildFullUrl(baseUrl, key, true)
28+
expect(fullUrl).toEqual(
29+
`https://public-api.wordpress.com/wp/v2/sites/example.wordpress.com/posts`
30+
)
31+
})
32+
33+
it(`builds full URL correctly for wordpress.org`, () => {
34+
const key = `/wp/v2/posts`
35+
const baseUrl = `http://dev-gatbsyjswp.pantheonsite.io/wp-json`
36+
const fullUrl = fetch.buildFullUrl(baseUrl, key, false)
37+
expect(fullUrl).toEqual(
38+
`http://dev-gatbsyjswp.pantheonsite.io/wp-json/wp/v2/posts`
39+
)
40+
})
41+
})

packages/gatsby-source-wordpress/src/fetch.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const querystring = require(`querystring`)
22
const axios = require(`axios`)
33
const _ = require(`lodash`)
44
const minimatch = require(`minimatch`)
5+
const { URL } = require(`url`)
56
const colorized = require(`./output-color`)
67
const httpExceptionHandler = require(`./http-exception-handler`)
78
const requestInQueue = require(`./request-in-queue`)
@@ -133,6 +134,7 @@ Mama Route URL: ${url}
133134
url,
134135
_verbose,
135136
_useACF,
137+
_hostingWPCOM,
136138
_acfOptionPageIds,
137139
_includedRoutes,
138140
_excludedRoutes,
@@ -503,7 +505,7 @@ function getValidRoutes({
503505

504506
// A valid route exposes its _links (for now)
505507
if (route._links) {
506-
const entityType = getRawEntityType(route)
508+
const entityType = getRawEntityType(key)
507509

508510
// Excluding the "technical" API Routes
509511
const excludedTypes = [
@@ -517,7 +519,8 @@ function getValidRoutes({
517519
`/jwt-auth/**`,
518520
]
519521

520-
const routePath = getRoutePath(url, route._links.self)
522+
const routePath = getRoutePath(url, key)
523+
521524
const whiteList = _includedRoutes
522525
const blackList = [...excludedTypes, ..._excludedRoutes]
523526

@@ -564,7 +567,11 @@ function getValidRoutes({
564567
)}_${entityType.replace(/-/g, `_`)}`
565568
break
566569
}
567-
validRoutes.push({ url: route._links.self, type: validType })
570+
571+
validRoutes.push({
572+
url: buildFullUrl(url, key, _hostingWPCOM),
573+
type: validType,
574+
})
568575
} else {
569576
if (_verbose) {
570577
const invalidType = inBlackList ? `blacklisted` : `not whitelisted`
@@ -591,23 +598,40 @@ function getValidRoutes({
591598
}
592599

593600
/**
594-
* Extract the raw entity type from route
601+
* Extract the raw entity type from fullPath
595602
*
596-
* @param {any} route
603+
* @param {any} full path to extract raw entity from
597604
*/
598-
const getRawEntityType = route =>
599-
route._links.self.substring(
600-
route._links.self.lastIndexOf(`/`) + 1,
601-
route._links.self.length
602-
)
605+
const getRawEntityType = fullPath =>
606+
fullPath.substring(fullPath.lastIndexOf(`/`) + 1, fullPath.length)
603607

604608
/**
605609
* Extract the route path for an endpoint
606610
*
607611
* @param {any} baseUrl The base site URL that should be removed
608-
* @param {any} fullUrl The full URL to retrieve the route path from
612+
* @param {any} fullPath The full path to retrieve the route path from
609613
*/
610-
const getRoutePath = (baseUrl, fullUrl) => fullUrl.replace(baseUrl, ``)
614+
const getRoutePath = (baseUrl, fullPath) => {
615+
const baseUrlObj = new URL(baseUrl)
616+
const basePath = baseUrlObj.pathname
617+
return fullPath.replace(basePath, ``)
618+
}
619+
620+
/**
621+
* Build full URL from baseUrl and fullPath.
622+
* Method of contructing full URL depends on wether it's hosted on wordpress.com
623+
* or not as wordpress.com have slightly different (custom) REST structure
624+
*
625+
* @param {any} baseUrl The base site URL that should be prepended to full path
626+
* @param {any} fullPath The full path to build URL from
627+
* @param {boolean} _hostingWPCOM Is hosted on wordpress.com
628+
*/
629+
const buildFullUrl = (baseUrl, fullPath, _hostingWPCOM) => {
630+
if (_hostingWPCOM) {
631+
baseUrl = new URL(baseUrl).origin
632+
}
633+
return `${baseUrl}${fullPath}`
634+
}
611635

612636
/**
613637
* Extract the route manufacturer
@@ -617,4 +641,7 @@ const getRoutePath = (baseUrl, fullUrl) => fullUrl.replace(baseUrl, ``)
617641
const getManufacturer = route =>
618642
route.namespace.substring(0, route.namespace.lastIndexOf(`/`))
619643

644+
fetch.getRawEntityType = getRawEntityType
645+
fetch.getRoutePath = getRoutePath
646+
fetch.buildFullUrl = buildFullUrl
620647
module.exports = fetch

0 commit comments

Comments
 (0)