Skip to content

Commit

Permalink
remove code for /cdn-cgi/image
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Jul 3, 2023
1 parent 67c69f2 commit d1aa353
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 65 deletions.
32 changes: 0 additions & 32 deletions packages/next-on-pages/templates/_worker.js/utils/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,6 @@ export function getResizingProperties(
};
}

/**
* Builds a URL to the Cloudflare CDN image resizing endpoint.
*
* @param requestUrl Incoming request's URL.
* @param imageUrl Image URL to resize.
* @param properties Image resizing properties.
* @returns URL to the Cloudflare CDN image resizing endpoint.
*/
export function buildCdnCgiImageUrl(
requestUrl: URL,
imageUrl: URL,
{ width, quality, format }: RequestInitCfPropertiesImage
): string {
const opts = [];

if (width) opts.push(`width=${width}`);
if (quality) opts.push(`quality=${quality}`);
if (format) opts.push(`format=${format}`);

const imageHref =
requestUrl.origin === imageUrl.origin
? imageUrl.pathname.slice(1)
: imageUrl.href;

return `${requestUrl.origin}/cdn-cgi/image/${opts.join(',')}/${imageHref}`;
}

/**
* Formats the given response to match the images configuration spec from the Vercel build output
* config.
Expand Down Expand Up @@ -171,11 +144,6 @@ export async function handleImageResizingRequest(
// const imageResp = await fetch(imageReq, { cf: { image: options } });
// if (imageResp.status === 200) return formatResp(imageResp, imageUrl, config);

// NOTE: Pages also doesn't seem to support calling the `/cdn-cgi/image` endpoint either.
// const cdnCgiImageUrl = buildCdnCgiImageUrl(requestUrl, imageUrl, options);
// const cdnCgiResp = await fetch(cdnCgiImageUrl, { headers: request.headers });
// if (cdnCgiResp.status === 200) return formatResp(cdnCgiResp, imageUrl, config);

const imageResp = await fetch(imageUrl, { headers: request.headers });
return formatResp(imageResp, imageUrl, config);
}
33 changes: 0 additions & 33 deletions packages/next-on-pages/tests/templates/utils/images.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, test } from 'vitest';
import {
buildCdnCgiImageUrl,
formatResp,
getResizingProperties,
isRemotePatternMatch,
Expand Down Expand Up @@ -229,38 +228,6 @@ describe('getResizingProperties', () => {
});
});

describe('buildCdnCgiImageUrl', () => {
test('builds a valid URL for relative images', () => {
const requestUrl = new URL(`${baseValidUrl}&w=640`);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { imageUrl, options } = getResizingProperties(
new Request(requestUrl),
baseConfig
)!;

const result = buildCdnCgiImageUrl(requestUrl, imageUrl, options);
expect(result).toEqual(
'https://localhost/cdn-cgi/image/width=640,quality=75/images/1.jpg'
);
});

test('builds a valid URL for external images', () => {
const requestUrl = new URL(
`${baseUrl}https%3A%2F%2Fexample.com%2Fimage.jpg&w=640`
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { imageUrl, options } = getResizingProperties(
new Request(requestUrl),
baseConfig
)!;

const result = buildCdnCgiImageUrl(requestUrl, imageUrl, options);
expect(result).toEqual(
'https://localhost/cdn-cgi/image/width=640,quality=75/https://example.com/image.jpg'
);
});
});

describe('formatResp', () => {
test('applies content security policy from the config', () => {
const config = { ...baseConfig, contentSecurityPolicy: 'default-src' };
Expand Down

0 comments on commit d1aa353

Please sign in to comment.