Skip to content

Commit f970600

Browse files
rburgstwardpeet
andauthored
fix(wordpress): ensure all file links are rewritten (#31652)
- in the case of either having a small `schema.perPage` setting or many many referenced resources on a single page only the first page of links were properly rewritten - now we ensure that all pages are fully processed until we resolve the promise fixes #31646 Co-authored-by: Ward Peeters <[email protected]>
1 parent 7897834 commit f970600

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
jest.mock(`../dist/utils/fetch-graphql`, () => jest.fn())
2+
3+
import { getHelpers } from "../dist/utils/get-gatsby-api"
4+
import fetchGraphql from "../dist/utils/fetch-graphql"
5+
import { fetchMediaItemsBySourceUrl } from "../dist/steps/source-nodes/fetch-nodes/fetch-referenced-media-items"
6+
import { createContentDigest } from "gatsby-core-utils"
7+
import store from "../dist/store"
8+
9+
const fakeReporter = {
10+
panic: msg => {
11+
console.error(msg)
12+
},
13+
info: msg => {
14+
console.log(msg)
15+
},
16+
}
17+
18+
const createApi = () => {
19+
return {
20+
actions: {
21+
createTypes: jest.fn(),
22+
createNode: jest.fn(),
23+
deleteNode: jest.fn(),
24+
},
25+
reporter: fakeReporter,
26+
createNodeId: jest.fn(),
27+
async getNode(id) {
28+
return {
29+
localFile: {
30+
id: id,
31+
},
32+
}
33+
},
34+
}
35+
}
36+
37+
describe(`fetchMediaItemsBySourceUrl`, () => {
38+
beforeAll(() => {
39+
store.dispatch.gatsbyApi.setState({
40+
pluginOptions: {
41+
schema: {
42+
perPage: 2,
43+
},
44+
},
45+
})
46+
})
47+
48+
afterEach(() => {
49+
jest.resetAllMocks()
50+
})
51+
52+
it(`should properly download multiple pages`, async () => {
53+
fetchGraphql
54+
.mockResolvedValueOnce({
55+
data: {
56+
mediaItem__index_0: null,
57+
mediaItem__index_1: null,
58+
},
59+
})
60+
.mockResolvedValueOnce({
61+
data: {
62+
mediaItem__index_2: {
63+
id: 2,
64+
mediaItemUrl: `https://wordpress.host/wp-content/uploads/2018/05/file1.mp3`,
65+
},
66+
mediaItem__index_3: {
67+
id: 3,
68+
mediaItemUrl: `https://wordpress.host/wp-content/uploads/2018/05/file1.mp3`,
69+
},
70+
},
71+
})
72+
.mockResolvedValueOnce({
73+
data: {
74+
mediaItem__index_4: null,
75+
mediaItem__index_5: null,
76+
},
77+
})
78+
.mockResolvedValueOnce({
79+
data: {
80+
mediaItem__index_6: null,
81+
mediaItem__index_7: null,
82+
},
83+
})
84+
const result = await fetchMediaItemsBySourceUrl({
85+
mediaItemUrls: [
86+
`https://wordpress.host/wp-content/uploads/2018/05/file1.mp3?_=7`,
87+
`https://wordpress.host/wp-content/uploads/2018/05/file2.mp3?_=7`,
88+
`https://wordpress.host/wp-content/uploads/2018/05/file1.mp3`,
89+
`https://wordpress.host/wp-content/uploads/2018/05/file2.mp3`,
90+
],
91+
createContentDigest,
92+
helpers: createApi(),
93+
})
94+
expect(result).toHaveLength(2)
95+
})
96+
})

packages/gatsby-source-wordpress/src/steps/source-nodes/fetch-nodes/fetch-referenced-media-items.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ const processAndDedupeImageUrls = urls =>
271271
}, urls)
272272
)
273273

274-
const fetchMediaItemsBySourceUrl = async ({
274+
export const fetchMediaItemsBySourceUrl = async ({
275275
mediaItemUrls,
276276
selectionSet,
277277
builtFragments,
@@ -319,10 +319,16 @@ const fetchMediaItemsBySourceUrl = async ({
319319
// we pass this resolve function into the queue function so it can let us
320320
// know when it's finished
321321
let resolveFutureNodes
322+
const allResolvedNodes = [...previouslyCachedMediaItemNodes]
323+
let resolveCountTogo = mediaItemUrlsPages.length
322324
const futureNodes = new Promise(resolve => {
323-
resolveFutureNodes = (nodes = []) =>
324-
// combine our resolved nodes we fetched with our cached nodes
325-
resolve([...nodes, ...previouslyCachedMediaItemNodes])
325+
// combine our resolved nodes we fetched with our cached nodes
326+
resolveFutureNodes = (nodes = []) => {
327+
allResolvedNodes.push(...nodes)
328+
if (--resolveCountTogo === 0) {
329+
resolve(allResolvedNodes)
330+
}
331+
}
326332
})
327333

328334
// we have no media items to fetch,

0 commit comments

Comments
 (0)