Skip to content

Commit

Permalink
fix(gatsby): more robust adapter zero-conf handling (#38778) (#38800)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
gatsbybot and pieh authored Jan 9, 2024
1 parent 366b54c commit 68b0821
Show file tree
Hide file tree
Showing 6 changed files with 918 additions and 131 deletions.
16 changes: 16 additions & 0 deletions e2e-tests/adapters/gatsby-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ if (shouldUseDebugAdapter) {
configOverrides = {
adapter: debugAdapter(),
}
} else {
process.env.GATSBY_ADAPTERS_MANIFEST = /* javascript */ `
module.exports = [
{
name: 'Netlify',
module: 'gatsby-adapter-netlify',
test: () => !!process.env.NETLIFY || !!process.env.NETLIFY_LOCAL,
versions: [
{
gatsbyVersion: '*',
moduleVersion: '*',
}
],
}
]
`
}

const config: GatsbyConfig = {
Expand Down
63 changes: 62 additions & 1 deletion packages/gatsby/src/utils/__tests__/get-latest-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,42 @@ describe(`default behavior: has network connectivity`, () => {

describe(`getLatestAdapters`, () => {
beforeEach(() => {
delete process.env.GATSBY_ADAPTERS_MANIFEST
})
it(`loads .js modules (prefers github)`, async () => {
axios.get.mockResolvedValueOnce({ data: latestAdaptersMarker })
const data = await getLatestAdapters()

expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining(`raw.githubusercontent.com`),
expect.any(Object)
)

expect(axios.get).not.toHaveBeenCalledWith(
expect.stringContaining(`unpkg.com`),
expect.any(Object)
)

expect(fs.writeFile).toHaveBeenCalledWith(
expect.stringContaining(`latest-adapters.js`),
latestAdaptersMarker,
expect.any(String)
)

expect(data).toEqual(mockAdaptersManifest)
})

it(`loads .js modules`, async () => {
it(`loads .js modules (fallbacks to unkpg of github fails)`, async () => {
axios.get.mockRejectedValueOnce(new Error(`does not matter`))
axios.get.mockResolvedValueOnce({ data: latestAdaptersMarker })

const data = await getLatestAdapters()

expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining(`raw.githubusercontent.com`),
expect.any(Object)
)

expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining(`unpkg.com`),
expect.any(Object)
Expand All @@ -128,6 +158,37 @@ describe(`default behavior: has network connectivity`, () => {

expect(data).toEqual(mockAdaptersManifest)
})

it(`uses GATSBY_ADAPTERS_MANIFEST env var if set`, async () => {
process.env.GATSBY_ADAPTERS_MANIFEST = `custom_manifest`

axios.get.mockRejectedValueOnce(
new Error(`does not matter and should't be called`)
)
axios.get.mockRejectedValueOnce(
new Error(`does not matter and should't be called`)
)

const data = await getLatestAdapters()

expect(axios.get).not.toHaveBeenCalledWith(
expect.stringContaining(`raw.githubusercontent.com`),
expect.any(Object)
)

expect(axios.get).not.toHaveBeenCalledWith(
expect.stringContaining(`unpkg.com`),
expect.any(Object)
)

expect(fs.writeFile).toHaveBeenCalledWith(
expect.stringContaining(`latest-adapters.js`),
process.env.GATSBY_ADAPTERS_MANIFEST,
expect.any(String)
)

expect(data).toEqual(mockAdaptersManifest)
})
})
})

Expand Down
Loading

0 comments on commit 68b0821

Please sign in to comment.