Skip to content

Commit 5580c73

Browse files
TylerBarneswardpeetLekoArts
authored
feat(gatsby-plugin-utils): add path prefix to image cdn urls (#36772)
* add path prefix to image cdn urls * add path prefix in jobs * move store out of file node * simplify * tests * oops wrong properties * Update packages/gatsby-plugin-utils/src/polyfill-remote-file/utils/url-generator.ts Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: Lennart <[email protected]>
1 parent 8a874ed commit 5580c73

File tree

6 files changed

+125
-39
lines changed

6 files changed

+125
-39
lines changed

packages/gatsby-plugin-utils/src/polyfill-remote-file/graphql/gatsby-image-resolver.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,17 @@ export async function gatsbyImageResolver(
205205
)
206206
}
207207

208-
const src = generateImageUrl(source, {
209-
width,
210-
height: Math.round(width / imageSizes.aspectRatio),
211-
format,
212-
cropFocus: args.cropFocus,
213-
quality: args.quality as number,
214-
})
208+
const src = generateImageUrl(
209+
source,
210+
{
211+
width,
212+
height: Math.round(width / imageSizes.aspectRatio),
213+
format,
214+
cropFocus: args.cropFocus,
215+
quality: args.quality as number,
216+
},
217+
store
218+
)
215219

216220
if (!fallbackSrc) {
217221
fallbackSrc = src

packages/gatsby-plugin-utils/src/polyfill-remote-file/graphql/public-url-resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function publicUrlResolver(
2323
)
2424
}
2525

26-
return generateFileUrl({ url: source.url, filename: source.filename })
26+
return generateFileUrl({ url: source.url, filename: source.filename }, store)
2727
}
2828

2929
export function generatePublicUrlFieldConfig(

packages/gatsby-plugin-utils/src/polyfill-remote-file/graphql/resize-resolver.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,16 @@ export async function resizeResolver(
9898
)
9999
}
100100

101-
const src = generateImageUrl(source, {
102-
...(args as IResizeArgs),
103-
width,
104-
height,
105-
format,
106-
})
101+
const src = generateImageUrl(
102+
source,
103+
{
104+
...(args as IResizeArgs),
105+
width,
106+
height,
107+
format,
108+
},
109+
store
110+
)
107111

108112
return {
109113
src,

packages/gatsby-plugin-utils/src/polyfill-remote-file/jobs/dispatchers.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ export function dispatchLocalFileServiceJob(
2323
store?: Store
2424
): void {
2525
const GATSBY_VERSION = getGatsbyVersion()
26-
const publicUrl = generateFileUrl({
27-
url,
28-
filename,
29-
}).split(`/`)
26+
const publicUrl = generateFileUrl(
27+
{
28+
url,
29+
filename,
30+
},
31+
store
32+
).split(`/`)
3033

3134
publicUrl.unshift(`public`)
3235
// get filename and remove querystring
@@ -83,7 +86,8 @@ export function dispatchLocalImageServiceJob(
8386
filename,
8487
internal: { contentDigest },
8588
},
86-
imageArgs
89+
imageArgs,
90+
store
8791
).split(`/`)
8892
publicUrl.unshift(`public`)
8993
// get filename and remove querystring

packages/gatsby-plugin-utils/src/polyfill-remote-file/utils/__tests__/url-generator.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import crypto from "crypto"
2+
import { Store } from "gatsby"
23
import url from "url"
34

45
import {
@@ -10,6 +11,60 @@ import {
1011
type ImageArgs = Parameters<typeof generateImageUrl>[1]
1112

1213
describe(`url-generator`, () => {
14+
it(`should work with pathPrefix`, () => {
15+
const source = {
16+
url: `https://example.com/image.jpg`,
17+
filename: `image.jpg`,
18+
mimeType: `image/jpeg`,
19+
internal: {
20+
contentDigest: `1234`,
21+
},
22+
}
23+
24+
const store = {
25+
getState: (): {
26+
program: { prefixPaths: boolean }
27+
config: { pathPrefix: string }
28+
} => {
29+
return {
30+
program: {
31+
prefixPaths: true,
32+
},
33+
config: {
34+
pathPrefix: `/prefix-test`,
35+
},
36+
}
37+
},
38+
}
39+
40+
expect(
41+
generateImageUrl(
42+
source,
43+
{
44+
width: 100,
45+
height: 100,
46+
cropFocus: `top`,
47+
format: `webp`,
48+
quality: 80,
49+
},
50+
store as unknown as Store
51+
)
52+
).toMatchInlineSnapshot(
53+
`"/prefix-test/_gatsby/image/18867d45576d8283d6fabb82406789c8/a5d4237c29c15bd781f3586364b7e168/image.webp?u=https%3A%2F%2Fexample.com%2Fimage.jpg&a=w%3D100%26h%3D100%26fit%3Dcrop%26crop%3Dtop%26fm%3Dwebp%26q%3D80&cd=1234"`
54+
)
55+
56+
const fileSource = {
57+
url: `https://example.com/file.pdf`,
58+
filename: `file.pdf`,
59+
}
60+
61+
expect(
62+
generateFileUrl(fileSource, store as unknown as Store)
63+
).toMatchInlineSnapshot(
64+
`"/prefix-test/_gatsby/file/9f2eba7a1dbc78363c52aeb0daec9031/file.pdf?u=https%3A%2F%2Fexample.com%2Ffile.pdf"`
65+
)
66+
})
67+
1368
describe(`URL encryption`, () => {
1469
function decryptImageCdnUrl(
1570
key: string,

packages/gatsby-plugin-utils/src/polyfill-remote-file/utils/url-generator.ts

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { URL } from "url"
44
import { createContentDigest } from "gatsby-core-utils/create-content-digest"
55
import { isImage } from "../types"
66
import type { ImageCropFocus, WidthOrHeight } from "../types"
7+
import type { Store } from "gatsby"
78

89
// this is an arbitrary origin that we use #branding so we can construct a full url for the URL constructor
910
const ORIGIN = `https://gatsbyjs.com`
@@ -53,20 +54,26 @@ function appendUrlParamToSearchParams(
5354
searchParams.append(paramName, finalUrl)
5455
}
5556

56-
export function generateFileUrl({
57-
url,
58-
filename,
59-
}: {
60-
url: string
61-
filename: string
62-
}): string {
57+
export function generateFileUrl(
58+
{
59+
url,
60+
filename,
61+
}: {
62+
url: string
63+
filename: string
64+
},
65+
store?: Store
66+
): string {
6367
const fileExt = extname(filename)
6468
const filenameWithoutExt = basename(filename, fileExt)
6569

6670
const parsedURL = new URL(
67-
`${ORIGIN}${generatePublicUrl({
68-
url,
69-
})}/${filenameWithoutExt}${fileExt}`
71+
`${ORIGIN}${generatePublicUrl(
72+
{
73+
url,
74+
},
75+
store
76+
)}/${filenameWithoutExt}${fileExt}`
7077
)
7178

7279
appendUrlParamToSearchParams(parsedURL.searchParams, url)
@@ -81,13 +88,14 @@ export function generateImageUrl(
8188
filename: string
8289
internal: { contentDigest: string }
8390
},
84-
imageArgs: Parameters<typeof generateImageArgs>[0]
91+
imageArgs: Parameters<typeof generateImageArgs>[0],
92+
store?: Store
8593
): string {
8694
const filenameWithoutExt = basename(source.filename, extname(source.filename))
8795
const queryStr = generateImageArgs(imageArgs)
8896

8997
const parsedURL = new URL(
90-
`${ORIGIN}${generatePublicUrl(source)}/${createContentDigest(
98+
`${ORIGIN}${generatePublicUrl(source, store)}/${createContentDigest(
9199
queryStr
92100
)}/${filenameWithoutExt}.${imageArgs.format}`
93101
)
@@ -102,17 +110,28 @@ export function generateImageUrl(
102110
return `${parsedURL.pathname}${parsedURL.search}`
103111
}
104112

105-
function generatePublicUrl({
106-
url,
107-
mimeType,
108-
}: {
109-
url: string
110-
mimeType?: string
111-
}): string {
113+
function generatePublicUrl(
114+
{
115+
url,
116+
mimeType,
117+
}: {
118+
url: string
119+
mimeType?: string
120+
},
121+
store?: Store
122+
): string {
123+
const state = store?.getState()
124+
125+
const pathPrefix = state?.program?.prefixPaths
126+
? state?.config?.pathPrefix
127+
: ``
128+
112129
const remoteUrl = createContentDigest(url)
113130

114131
let publicUrl =
115-
mimeType && isImage({ mimeType }) ? `/_gatsby/image/` : `/_gatsby/file/`
132+
pathPrefix +
133+
(mimeType && isImage({ mimeType }) ? `/_gatsby/image/` : `/_gatsby/file/`)
134+
116135
publicUrl += `${remoteUrl}`
117136

118137
return publicUrl

0 commit comments

Comments
 (0)