Skip to content

Commit b455188

Browse files
fix: fix downloadUrl when downloading Gatsby datastore file from CDN (#392)
* fix: fix downloadUrl for datastore file * chore: cleanup - remove .only Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent db412a2 commit b455188

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

plugin/src/templates/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export async function prepareFilesystem(
108108

109109
const dataMetadataPath = join(process.cwd(), '.cache', 'dataMetadata.json')
110110
const { fileName } = await readJSON(dataMetadataPath)
111-
const downloadUrl = `${siteUrl}/${fileName}`
111+
const downloadUrl = new URL(`/${fileName}`, siteUrl).toString()
112112

113113
console.log('Downloading data file from', downloadUrl)
114114

plugin/test/unit/templates/utils.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { tmpdir } from 'os'
22
import { resolve, join, dirname } from 'path'
33

44
import Chance from 'chance'
5-
import { copy, ensureDir, existsSync, readFileSync, unlink } from 'fs-extra'
5+
import {
6+
readJSON,
7+
copy,
8+
ensureDir,
9+
existsSync,
10+
readFileSync,
11+
unlink,
12+
} from 'fs-extra'
613
import { dir as getTmpDir } from 'tmp-promise'
714

815
// eslint-disable-next-line import/no-namespace
@@ -67,6 +74,29 @@ describe('prepareFilesystem', () => {
6774
TEST_TIMEOUT,
6875
)
6976

77+
it(
78+
'uses the correct URLs to download file from the CDN when GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is enabled',
79+
async () => {
80+
const domain = chance.url({ path: '' })
81+
const url = `${domain}${chance.word()}/${chance.word()}`
82+
83+
process.env.GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE = 'true'
84+
await moveGatsbyDir()
85+
86+
const cacheDir = resolve('.cache')
87+
const contents = await readJSON(`${cacheDir}/dataMetadata.json`)
88+
const datastoreFileName = contents.fileName
89+
90+
await templateUtils.prepareFilesystem(cacheDir, url)
91+
92+
expect(downloadFileSpy).toHaveBeenCalledTimes(1)
93+
expect(downloadFileSpy.mock.calls[0][0]).toEqual(
94+
`${domain}${datastoreFileName}`,
95+
)
96+
},
97+
TEST_TIMEOUT,
98+
)
99+
70100
it(
71101
'does not download file from the CDN when GATSBY_EXCLUDE_DATASTORE_FROM_BUNDLE is not enabled',
72102
async () => {

0 commit comments

Comments
 (0)