Skip to content

Commit 6cb5eb3

Browse files
committed
fix(cloudflare): handle app.baseURL and external image sources
1 parent 67b40ac commit 6cb5eb3

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

src/runtime/providers/cloudflare.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { encodeQueryItem, joinURL } from 'ufo'
1+
import { encodeQueryItem, hasProtocol, joinURL } from 'ufo'
22
import { createOperationsGenerator } from '../utils/index'
33
import { defineProvider } from '../utils/provider'
44

@@ -36,20 +36,20 @@ interface CloudflareOptions {
3636
baseURL?: string
3737
}
3838

39-
// https://developers.cloudflare.com/images/image-resizing/url-format/
39+
// https://developers.cloudflare.com/images/transform-images/transform-via-url/
4040
export default defineProvider<CloudflareOptions>({
41-
getImage: (src, {
42-
modifiers,
43-
baseURL = '/',
44-
}) => {
41+
getImage: (src, { modifiers, baseURL = '/' }, ctx) => {
4542
const mergeModifiers = { ...defaultModifiers, ...modifiers }
4643
const operations = operationsGenerator(mergeModifiers as any)
4744

45+
const isExternal = hasProtocol(src)
46+
const sourcePath = isExternal ? src : joinURL(ctx.options.nuxt.baseURL, src)
47+
4848
// https://<ZONE>/cdn-cgi/image/<OPTIONS>/<SOURCE-IMAGE>
49-
const url = operations ? joinURL(baseURL, 'cdn-cgi/image', operations, src) : src
49+
const url = operations
50+
? joinURL(baseURL, 'cdn-cgi/image', operations, sourcePath)
51+
: sourcePath
5052

51-
return {
52-
url,
53-
}
53+
return { url }
5454
},
5555
})

test/nuxt/providers.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,72 @@ describe('Providers', () => {
109109
}
110110
})
111111

112+
it('cloudflare with app.baseURL', () => {
113+
const ctx = { options: { ...emptyContext.options, nuxt: { baseURL: '/admin/' } } } as any
114+
115+
expect(cloudflare().getImage('/images/test.png', {
116+
modifiers: { width: 200 },
117+
baseURL: '/',
118+
}, ctx)).toMatchObject({ url: '/cdn-cgi/image/w=200/admin/images/test.png' })
119+
120+
expect(cloudflare().getImage('/images/test.png', {
121+
modifiers: {},
122+
baseURL: '/',
123+
}, ctx)).toMatchObject({ url: '/admin/images/test.png' })
124+
})
125+
126+
it('cloudflare with external image', () => {
127+
expect(cloudflare().getImage('https://external.com/photo.jpg', {
128+
modifiers: { width: 200 },
129+
baseURL: '/',
130+
}, emptyContext)).toMatchObject({ url: '/cdn-cgi/image/w=200/https://external.com/photo.jpg' })
131+
132+
expect(cloudflare().getImage('https://external.com/photo.jpg', {
133+
modifiers: {},
134+
baseURL: '/',
135+
}, emptyContext)).toMatchObject({ url: 'https://external.com/photo.jpg' })
136+
})
137+
138+
it('cloudflare cross-zone', () => {
139+
const ctx = { options: { ...emptyContext.options, nuxt: { baseURL: '/' } } } as any
140+
141+
expect(cloudflare().getImage('/images/test.png', {
142+
modifiers: { width: 200 },
143+
baseURL: 'https://abc.cz',
144+
}, ctx)).toMatchObject({ url: 'https://abc.cz/cdn-cgi/image/w=200/images/test.png' })
145+
146+
expect(cloudflare().getImage('/images/test.png', {
147+
modifiers: {},
148+
baseURL: 'https://abc.cz',
149+
}, ctx)).toMatchObject({ url: '/images/test.png' })
150+
})
151+
152+
it('cloudflare cross-zone with app.baseURL', () => {
153+
const ctx = { options: { ...emptyContext.options, nuxt: { baseURL: '/admin/' } } } as any
154+
155+
expect(cloudflare().getImage('/images/test.png', {
156+
modifiers: { width: 200 },
157+
baseURL: 'https://abc.cz',
158+
}, ctx)).toMatchObject({ url: 'https://abc.cz/cdn-cgi/image/w=200/admin/images/test.png' })
159+
160+
expect(cloudflare().getImage('/images/test.png', {
161+
modifiers: {},
162+
baseURL: 'https://abc.cz',
163+
}, ctx)).toMatchObject({ url: '/admin/images/test.png' })
164+
})
165+
166+
it('cloudflare cross-zone with external src', () => {
167+
expect(cloudflare().getImage('https://123.cz/images/test.png', {
168+
modifiers: { width: 200 },
169+
baseURL: 'https://abc.cz',
170+
}, emptyContext)).toMatchObject({ url: 'https://abc.cz/cdn-cgi/image/w=200/https://123.cz/images/test.png' })
171+
172+
expect(cloudflare().getImage('https://123.cz/images/test.png', {
173+
modifiers: {},
174+
baseURL: 'https://abc.cz',
175+
}, emptyContext)).toMatchObject({ url: 'https://123.cz/images/test.png' })
176+
})
177+
112178
it('cloudinary', () => {
113179
const providerOptions = {
114180
baseURL: '/',

0 commit comments

Comments
 (0)