Skip to content

Commit 68b0821

Browse files
gatsbybotpieh
andauthored
fix(gatsby): more robust adapter zero-conf handling (#38778) (#38800)
* feat: prefer github for adapters version manifest * fix(gatsby): more robust adapter autoinstallation handling to prevent broken deploys * test: always allow using installed adapter version in e2e-tests/adapter (cherry picked from commit 7f08e7b) Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent 366b54c commit 68b0821

6 files changed

Lines changed: 918 additions & 131 deletions

File tree

e2e-tests/adapters/gatsby-config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ if (shouldUseDebugAdapter) {
1515
configOverrides = {
1616
adapter: debugAdapter(),
1717
}
18+
} else {
19+
process.env.GATSBY_ADAPTERS_MANIFEST = /* javascript */ `
20+
module.exports = [
21+
{
22+
name: 'Netlify',
23+
module: 'gatsby-adapter-netlify',
24+
test: () => !!process.env.NETLIFY || !!process.env.NETLIFY_LOCAL,
25+
versions: [
26+
{
27+
gatsbyVersion: '*',
28+
moduleVersion: '*',
29+
}
30+
],
31+
}
32+
]
33+
`
1834
}
1935

2036
const config: GatsbyConfig = {

packages/gatsby/src/utils/__tests__/get-latest-gatsby-files.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,42 @@ describe(`default behavior: has network connectivity`, () => {
109109

110110
describe(`getLatestAdapters`, () => {
111111
beforeEach(() => {
112+
delete process.env.GATSBY_ADAPTERS_MANIFEST
113+
})
114+
it(`loads .js modules (prefers github)`, async () => {
112115
axios.get.mockResolvedValueOnce({ data: latestAdaptersMarker })
116+
const data = await getLatestAdapters()
117+
118+
expect(axios.get).toHaveBeenCalledWith(
119+
expect.stringContaining(`raw.githubusercontent.com`),
120+
expect.any(Object)
121+
)
122+
123+
expect(axios.get).not.toHaveBeenCalledWith(
124+
expect.stringContaining(`unpkg.com`),
125+
expect.any(Object)
126+
)
127+
128+
expect(fs.writeFile).toHaveBeenCalledWith(
129+
expect.stringContaining(`latest-adapters.js`),
130+
latestAdaptersMarker,
131+
expect.any(String)
132+
)
133+
134+
expect(data).toEqual(mockAdaptersManifest)
113135
})
114136

115-
it(`loads .js modules`, async () => {
137+
it(`loads .js modules (fallbacks to unkpg of github fails)`, async () => {
138+
axios.get.mockRejectedValueOnce(new Error(`does not matter`))
139+
axios.get.mockResolvedValueOnce({ data: latestAdaptersMarker })
140+
116141
const data = await getLatestAdapters()
117142

143+
expect(axios.get).toHaveBeenCalledWith(
144+
expect.stringContaining(`raw.githubusercontent.com`),
145+
expect.any(Object)
146+
)
147+
118148
expect(axios.get).toHaveBeenCalledWith(
119149
expect.stringContaining(`unpkg.com`),
120150
expect.any(Object)
@@ -128,6 +158,37 @@ describe(`default behavior: has network connectivity`, () => {
128158

129159
expect(data).toEqual(mockAdaptersManifest)
130160
})
161+
162+
it(`uses GATSBY_ADAPTERS_MANIFEST env var if set`, async () => {
163+
process.env.GATSBY_ADAPTERS_MANIFEST = `custom_manifest`
164+
165+
axios.get.mockRejectedValueOnce(
166+
new Error(`does not matter and should't be called`)
167+
)
168+
axios.get.mockRejectedValueOnce(
169+
new Error(`does not matter and should't be called`)
170+
)
171+
172+
const data = await getLatestAdapters()
173+
174+
expect(axios.get).not.toHaveBeenCalledWith(
175+
expect.stringContaining(`raw.githubusercontent.com`),
176+
expect.any(Object)
177+
)
178+
179+
expect(axios.get).not.toHaveBeenCalledWith(
180+
expect.stringContaining(`unpkg.com`),
181+
expect.any(Object)
182+
)
183+
184+
expect(fs.writeFile).toHaveBeenCalledWith(
185+
expect.stringContaining(`latest-adapters.js`),
186+
process.env.GATSBY_ADAPTERS_MANIFEST,
187+
expect.any(String)
188+
)
189+
190+
expect(data).toEqual(mockAdaptersManifest)
191+
})
131192
})
132193
})
133194

0 commit comments

Comments
 (0)