Skip to content

Commit 7f33656

Browse files
authored
fix(prismic): use unsplash image proxy where necessary (nuxt#1614)
1 parent d2e4eb0 commit 7f33656

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

src/runtime/providers/prismic.ts

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import { joinURL, parseQuery, parseURL, stringifyQuery } from 'ufo'
1+
import { getQuery, withBase, withQuery } from 'ufo'
22
import type { ProviderGetImage } from '../../module'
33
import { operationsGenerator } from './imgix'
4+
import { getImage as getUnsplashImage, unsplashCDN } from './unsplash'
45

5-
const PRISMIC_IMGIX_BUCKET = 'https://images.prismic.io'
6+
export const prismicCDN = 'https://images.prismic.io/'
67

7-
// Prismic image bucket is left configurable in order to test on other environments
8-
export const getImage: ProviderGetImage = (
9-
src,
10-
{ modifiers = {}, baseURL = PRISMIC_IMGIX_BUCKET } = {},
11-
) => {
12-
const operations = operationsGenerator(modifiers)
13-
14-
const parsedURL = parseURL(src)
8+
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = prismicCDN } = {}, ctx) => {
9+
// Some images served by Prismic are from unsplash, so we use the unsplash provider for those
10+
if (src.startsWith(unsplashCDN)) {
11+
return getUnsplashImage(src, { modifiers }, ctx)
12+
}
1513

14+
const operations = operationsGenerator(modifiers)
15+
// withQuery requires query parameters as an object, so I parse the modifiers into an object with getQuery
1616
return {
17-
url: joinURL(
18-
baseURL,
19-
parsedURL.pathname + '?'
20-
// Remove duplicated keys, prioritizing override from developers
21-
+ stringifyQuery(Object.assign(parseQuery(parsedURL.search), parseQuery(operations))),
22-
),
17+
url: withQuery(withBase(src, baseURL), getQuery('?' + operations)),
2318
}
2419
}

src/runtime/providers/unsplash.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getQuery, withBase, withQuery } from 'ufo'
44
import type { ProviderGetImage } from '../../module'
55
import { operationsGenerator } from './imgix'
66

7-
const unsplashCDN = 'https://images.unsplash.com/'
7+
export const unsplashCDN = 'https://images.unsplash.com/'
88

99
export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = unsplashCDN } = {}) => {
1010
const operations = operationsGenerator(modifiers)

test/providers.ts

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const images = [
1919
netlifyLargeMedia: { url: '/test.png' },
2020
prepr: { url: 'https://projectName.stream.prepr.io/image-test-300x450-png' },
2121
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=100&h=100' },
22+
prismicUnsplash: { url: 'https://images.unsplash.com/test.png?auto=compress,format&rect=0,0,200,200&w=100&h=100' },
2223
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?auto=format' },
2324
contentful: { url: '/test.png' },
2425
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png' },
@@ -54,6 +55,7 @@ export const images = [
5455
netlifyLargeMedia: { url: '/test.png?w=200&nf_resize=fit' },
5556
prepr: { url: 'https://projectName.stream.prepr.io/w_200/image-test-300x450-png' },
5657
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=100' },
58+
prismicUnsplash: { url: 'https://images.unsplash.com/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=100' },
5759
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?w=200&auto=format' },
5860
contentful: { url: '/test.png?w=200' },
5961
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png?width=200' },
@@ -89,6 +91,7 @@ export const images = [
8991
netlifyLargeMedia: { url: '/test.png?h=200&nf_resize=fit' },
9092
prepr: { url: 'https://projectName.stream.prepr.io/h_200/image-test-300x450-png' },
9193
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=100&h=200' },
94+
prismicUnsplash: { url: 'https://images.unsplash.com/test.png?auto=compress,format&rect=0,0,200,200&w=100&h=200' },
9295
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?h=200&auto=format' },
9396
contentful: { url: '/test.png?h=200' },
9497
cloudimage: { url: 'https://demo.cloudimg.io/v7/test.png?height=200' },
@@ -123,6 +126,7 @@ export const images = [
123126
netlifyImageCdn: { url: '/.netlify/images?w=200&h=200&url=%2Ftest.png' },
124127
netlifyLargeMedia: { url: '/test.png?w=200&h=200&nf_resize=fit' },
125128
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200' },
129+
prismicUnsplash: { url: 'https://images.unsplash.com/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200' },
126130
prepr: { url: 'https://projectName.stream.prepr.io/w_200,h_200/image-test-300x450-png' },
127131
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?w=200&h=200&auto=format' },
128132
contentful: { url: '/test.png?w=200&h=200' },
@@ -158,6 +162,7 @@ export const images = [
158162
netlifyImageCdn: { url: '/.netlify/images?w=200&h=200&fit=contain&url=%2Ftest.png' },
159163
netlifyLargeMedia: { url: '/test.png?w=200&h=200&nf_resize=fit' },
160164
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200&fit=fill' },
165+
prismicUnsplash: { url: 'https://images.unsplash.com/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200&fit=fill' },
161166
prepr: { url: 'https://projectName.stream.prepr.io/w_200,h_200,fit_contain/image-test-300x450-png' },
162167
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?w=200&h=200&fit=fill&auto=format&bg=ffffff' },
163168
contentful: { url: '/test.png?w=200&h=200&fit=fill' },
@@ -193,6 +198,7 @@ export const images = [
193198
netlifyImageCdn: { url: '/.netlify/images?w=200&h=200&fit=contain&fm=jpg&url=%2Ftest.png' },
194199
netlifyLargeMedia: { url: '/test.png?w=200&h=200&nf_resize=fit' },
195200
prismic: { url: '/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200&fit=fill&fm=jpeg' },
201+
prismicUnsplash: { url: 'https://images.unsplash.com/test.png?auto=compress,format&rect=0,0,200,200&w=200&h=200&fit=fill&fm=jpeg' },
196202
sanity: { url: 'https://cdn.sanity.io/images/projectid/production/test-300x450.png?w=200&h=200&fit=fill&fm=jpg&bg=ffffff' },
197203
prepr: { url: 'https://projectName.stream.prepr.io/w_200,h_200,fit_contain,format_jpg/image-test-300x450-png' },
198204
contentful: { url: '/test.png?w=200&h=200&fit=fill&fm=jpg' },

test/unit/providers.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,21 @@ describe('Providers', () => {
337337
}
338338
})
339339

340+
it('prismic (unsplash)', () => {
341+
const providerOptions = {
342+
baseURL: '', // Use empty base URL for the sake of simplicity
343+
}
344+
345+
const EXISTING_QUERY_PARAMETERS
346+
= '?auto=compress,format&rect=0,0,200,200&w=100&h=100'
347+
348+
for (const image of images) {
349+
const [, modifiers] = image.args
350+
const generated = prismic.getImage(`${image.prismicUnsplash.url.split('?').shift()}${EXISTING_QUERY_PARAMETERS}`, { modifiers, ...providerOptions }, emptyContext)
351+
expect(generated).toMatchObject(image.prismicUnsplash)
352+
}
353+
})
354+
340355
it('sanity', () => {
341356
const providerOptions = {
342357
baseURL: '',

0 commit comments

Comments
 (0)