Skip to content

fix(pages-components): don't use URL.canParse #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/pages-components/src/components/image/fileUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Env, Partition } from "./url.js";
import { Env, isValidHttpUrl, Partition } from "./url.js";

// FileURL is a URL for a file stored in the Yext CDN.
//
Expand Down Expand Up @@ -54,7 +54,7 @@ export type FileUrl = {
};

export const parseFileUrl = (rawUrl: string) => {
if (!URL.canParse(rawUrl)) {
if (!isValidHttpUrl(rawUrl)) {
console.error(`Invalid image url: ${rawUrl}.`);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/pages-components/src/components/image/image.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";
import { useEffect, useRef, useState } from "react";
import { ImageProps, ImageLayout, ImageLayoutOption } from "./types.js";
import { getImageUrl } from "./url.js";
import { getImageUrl, isValidHttpUrl } from "./url.js";

/**
* Renders an image based from the Yext Knowledge Graph. Example of using the component to render
Expand Down Expand Up @@ -57,7 +57,7 @@ export const Image = ({
}

// The image is invalid, only try to load the placeholder
if (!URL.canParse(imageData.url)) {
if (!isValidHttpUrl(imageData.url)) {
console.error(`Invalid image url: ${imageData.url}`);
return <>{placeholder != null && placeholder}</>;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/pages-components/src/components/image/photoUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Env } from "./url.js";
import { Env, isValidHttpUrl } from "./url.js";

// PhotoURL is a URL for an image stored in the legacy Yext CDN, generated by the old PhotoServer.
//
Expand Down Expand Up @@ -62,7 +62,7 @@ export type PhotoUrl = {
};

export const parsePhotoUrl = (rawUrl: string) => {
if (!URL.canParse(rawUrl)) {
if (!isValidHttpUrl(rawUrl)) {
console.error(`Invalid image url: ${rawUrl}.`);
return;
}
Expand Down
14 changes: 13 additions & 1 deletion packages/pages-components/src/components/image/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type Partition = "us" | "eu";
* If the raw url cannot be parsed, it's simply returned as-is and used as a passthrough.
*/
export const getImageUrl = (rawUrl: string, width: number, height: number) => {
if (!URL.canParse(rawUrl)) {
if (!isValidHttpUrl(rawUrl)) {
console.error(`Invalid image url: ${rawUrl}.`);
return;
}
Expand All @@ -29,3 +29,15 @@ export const getImageUrl = (rawUrl: string, width: number, height: number) => {

return rawUrl;
};

export const isValidHttpUrl = (rawUrl: string) => {
let url;

try {
url = new URL(rawUrl);
} catch (_) {
return false;
}

return url.protocol === "http:" || url.protocol === "https:";
};
Loading