From a0f591c00960e85989bb06cb6a1318bd42e505d2 Mon Sep 17 00:00:00 2001 From: Zack Stickles Date: Wed, 29 Jun 2022 12:25:59 -0700 Subject: [PATCH] feat: load SVGs from local cache --- gatsby-node.js | 5 +- src/components/QuickstartImg.js | 93 +++++++++++++++++--------------- src/components/QuickstartTile.js | 2 - src/pages/index.jsx | 2 + 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 16df8af1..82b3a4ce 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -84,8 +84,9 @@ exports.onCreateNode = async ({ node, actions, createNodeId, getCache }) => { value: `${resolveQuickstartSlug(node.name, node.id)}`, }); - // If a logo URL is provided, source the file and save the ID for use with sharp - if (node.logoUrl && path.extname(node.logoUrl) !== '.svg') { + // if a logoUrl is provided + if (node.logoUrl) { + // fetch the logo and create a "File" node const fileNode = await createRemoteFileNode({ url: node.logoUrl, parentNodeId: node.id, diff --git a/src/components/QuickstartImg.js b/src/components/QuickstartImg.js index 91f71aa1..b7e1540f 100644 --- a/src/components/QuickstartImg.js +++ b/src/components/QuickstartImg.js @@ -18,49 +18,11 @@ const getNameAcronym = (name) => .map((word) => word.charAt(0)) .join(''); -const QuickstartImg = ({ className, logoUrl, packName, imageNode }) => { - const image = getImage(imageNode); - - // If we have an image for sharp to optimize, use GatsbyImage - if (image) { - return ( - - ); - } - - // if we don't have a sharp-capable image, but we have a URL, it's an - // SVG (already performant). - if (logoUrl) { - return ( - {packName} { - e.preventDefault(); - e.target.src = DEFAULT_IMAGE; - }} - className={className} - /> - ); - } - - // If no images available, create a "placeholder" using the acronym - // for the quickstart name. - const acronym = getNameAcronym(packName); +/** + * Displays the Quickstart name as a acronym with a subtle background. + */ +const FallbackImg = ({ className, name }) => { + const acronym = getNameAcronym(name); return (
{ ); }; +const QuickstartImg = ({ className, packName, imageNode }) => { + if (imageNode) { + // If we have an image for sharp to optimize, use GatsbyImage + const image = getImage(imageNode); + + if (image) { + return ( + + ); + } + + // If we don't have a sharp-capable image, but we have a URL, it's an + // SVG (already performant) and is already built with the site. + const { ext, publicURL } = imageNode; + + if (ext === '.svg' && publicURL) { + return ( + {packName} + ); + } + } + + // In all other cases, render the fallback. + return ; +}; + QuickstartImg.propTypes = { packName: PropTypes.string.isRequired, - logoUrl: PropTypes.string, className: PropTypes.string, imageNode: PropTypes.object, }; diff --git a/src/components/QuickstartTile.js b/src/components/QuickstartTile.js index c2b7ecb1..b5248cc1 100644 --- a/src/components/QuickstartTile.js +++ b/src/components/QuickstartTile.js @@ -21,7 +21,6 @@ const QuickstartTile = ({ name, fields, logo, - logoUrl, level, className, summary, @@ -122,7 +121,6 @@ const QuickstartTile = ({ >