From c978554215ae119a8ce86e3df6f11acb018b2395 Mon Sep 17 00:00:00 2001 From: tate Date: Mon, 15 Apr 2024 13:39:21 +1000 Subject: [PATCH] optimised? --- functions/modifier/ElementsCreator.ts | 17 ++- functions/rewriter/address.ts | 99 ++++++++++------ functions/rewriter/profile.ts | 107 ++++++++++++------ .../pages/profile/[name]/Profile.tsx | 43 ++++--- src/pages/address.tsx | 46 ++++---- 5 files changed, 193 insertions(+), 119 deletions(-) diff --git a/functions/modifier/ElementsCreator.ts b/functions/modifier/ElementsCreator.ts index 019db2fc8..364177045 100644 --- a/functions/modifier/ElementsCreator.ts +++ b/functions/modifier/ElementsCreator.ts @@ -1,15 +1,12 @@ +export type ElementCreation = { + tagName: string + attributes: Record +} + export class ElementsCreator { - private elements: { - tagName: string - attributes: Record - }[] + private elements: ElementCreation[] - constructor( - elements: { - tagName: string - attributes: Record - }[], - ) { + constructor(elements: ElementCreation[]) { this.elements = elements } diff --git a/functions/rewriter/address.ts b/functions/rewriter/address.ts index aa0d6a29d..73692327e 100644 --- a/functions/rewriter/address.ts +++ b/functions/rewriter/address.ts @@ -2,7 +2,7 @@ import type { Address } from 'viem' import { AttributeModifier } from '../modifier/AttributeModifier' import { ContentModifier } from '../modifier/ContentModifier' -import { ElementsCreator } from '../modifier/ElementsCreator' +import { ElementCreation, ElementsCreator } from '../modifier/ElementsCreator' import { BASE_OG_IMAGE_URL, isFarcasterRequest } from '../utils' type ApiAddressHandlerResponse = { @@ -30,23 +30,59 @@ export const addressRewriter = async ({ const newTitle = `${address.slice(0, 7)}...${address.slice(-5)} on ENS` const newDescription = `${address}'s profile on the Ethereum Name Service` + const elements: ElementCreation[] = [ + /* opengraph */ + { + tagName: 'meta', + attributes: { property: 'og:image', content: ogImageUrl }, + }, + { + tagName: 'meta', + attributes: { property: 'og:title', content: newTitle }, + }, + { + tagName: 'meta', + attributes: { property: 'og:description', content: newDescription }, + }, + /* twitter */ + { + tagName: 'meta', + attributes: { name: 'twitter:image', content: ogImageUrl }, + }, + { + tagName: 'meta', + attributes: { name: 'twitter:title', content: newTitle }, + }, + { + tagName: 'meta', + attributes: { name: 'twitter:description', content: newDescription }, + }, + /* farcaster */ + { + tagName: 'meta', + attributes: { name: 'fc:frame:image', content: ogImageUrl }, + }, + { + tagName: 'meta', + attributes: { name: 'fc:frame:button:1', content: 'View address' }, + }, + { + tagName: 'meta', + attributes: { name: 'fc:frame:button:1:action', content: 'link' }, + }, + { + tagName: 'meta', + attributes: { + name: 'fc:frame:button:1:target', + content: `https://ens.app/${address}`, + }, + }, + ] + const rewriter = new HTMLRewriter() .on('title', new ContentModifier(newTitle)) .on('meta[name="description"]', new AttributeModifier('content', newDescription)) - /* opengraph */ - .on('meta[property="og:image"]', new AttributeModifier('content', ogImageUrl)) - .on('meta[property="og:title"]', new AttributeModifier('content', newTitle)) - .on('meta[property="og:description"]', new AttributeModifier('content', newDescription)) - /* farcaster */ - .on('meta[name="fc:frame:image"]', new AttributeModifier('content', ogImageUrl)) - .on( - 'meta[name="fc:frame:button:1:target"]', - new AttributeModifier('content', `https://ens.app/${address}`), - ) - /* twitter */ - .on('meta[name="twitter:image"]', new AttributeModifier('content', ogImageUrl)) - .on('meta[name="twitter:title"]', new AttributeModifier('content', newTitle)) - .on('meta[name="twitter:description"]', new AttributeModifier('content', newDescription)) + .on('head', new ElementsCreator(elements)) if (!isFarcasterRequest(request)) return rewriter @@ -57,25 +93,22 @@ export const addressRewriter = async ({ if (!data) return rewriter if (data.primaryName) { - rewriter.on( - 'head', - new ElementsCreator([ - { - tagName: 'meta', - attributes: { name: 'fc:frame:button:2', content: 'View profile' }, - }, - { - tagName: 'meta', - attributes: { name: 'fc:frame:button:2:action', content: 'link' }, - }, - { - tagName: 'meta', - attributes: { - name: 'fc:frame:button:2:target', - content: `https://ens.app/${data.primaryName}`, - }, + elements.push( + { + tagName: 'meta', + attributes: { name: 'fc:frame:button:2', content: 'View profile' }, + }, + { + tagName: 'meta', + attributes: { name: 'fc:frame:button:2:action', content: 'link' }, + }, + { + tagName: 'meta', + attributes: { + name: 'fc:frame:button:2:target', + content: `https://ens.app/${data.primaryName}`, }, - ]), + }, ) } diff --git a/functions/rewriter/profile.ts b/functions/rewriter/profile.ts index 07c088914..ea7228a44 100644 --- a/functions/rewriter/profile.ts +++ b/functions/rewriter/profile.ts @@ -2,7 +2,7 @@ import type { Address } from 'viem' import { AttributeModifier } from '../modifier/AttributeModifier' import { ContentModifier } from '../modifier/ContentModifier' -import { ElementsCreator } from '../modifier/ElementsCreator' +import { ElementCreation, ElementsCreator } from '../modifier/ElementsCreator' import { BASE_OG_IMAGE_URL, isFarcasterRequest } from '../utils' type ApiNameHandlerResponse = @@ -43,27 +43,63 @@ export const profileRewriter = async ({ ? `${BASE_OG_IMAGE_URL}/image/name/${encodeURIComponent(normalisedName)}` : `${BASE_OG_IMAGE_URL}/image/name/` - const rewriter = new HTMLRewriter() - .on('title', new ContentModifier(newTitle)) - .on('meta[name="description"]', new AttributeModifier('content', newDescription)) + const elements: ElementCreation[] = [ /* opengraph */ - .on('meta[property="og:image"]', new AttributeModifier('content', ogImageUrl)) - .on('meta[property="og:title"]', new AttributeModifier('content', newTitle)) - .on('meta[property="og:description"]', new AttributeModifier('content', newDescription)) + { + tagName: 'meta', + attributes: { property: 'og:image', content: ogImageUrl }, + }, + { + tagName: 'meta', + attributes: { property: 'og:title', content: newTitle }, + }, + { + tagName: 'meta', + attributes: { property: 'og:description', content: newDescription }, + }, /* twitter */ - .on('meta[name="twitter:image"]', new AttributeModifier('content', ogImageUrl)) - .on('meta[name="twitter:title"]', new AttributeModifier('content', newTitle)) - .on('meta[name="twitter:description"]', new AttributeModifier('content', newDescription)) + { + tagName: 'meta', + attributes: { name: 'twitter:image', content: ogImageUrl }, + }, + { + tagName: 'meta', + attributes: { name: 'twitter:title', content: newTitle }, + }, + { + tagName: 'meta', + attributes: { name: 'twitter:description', content: newDescription }, + }, + ...(normalisedName + ? [ + /* farcaster */ + { + tagName: 'meta', + attributes: { name: 'fc:frame:image', content: ogImageUrl }, + }, + { + tagName: 'meta', + attributes: { name: 'fc:frame:button:1', content: 'View profile' }, + }, + { + tagName: 'meta', + attributes: { name: 'fc:frame:button:1:action', content: 'link' }, + }, + { + tagName: 'meta', + attributes: { + name: 'fc:frame:button:1:target', + content: `https://ens.app/${normalisedName}`, + }, + }, + ] + : []), + ] - /* farcaster */ - if (normalisedName) { - rewriter - .on('meta[name="fc:frame:image"]', new AttributeModifier('content', ogImageUrl)) - .on( - 'meta[name="fc:frame:button:1:target"]', - new AttributeModifier('content', `https://ens.app/${normalisedName}`), - ) - } + const rewriter = new HTMLRewriter() + .on('title', new ContentModifier(newTitle)) + .on('meta[name="description"]', new AttributeModifier('content', newDescription)) + .on('head', new ElementsCreator(elements)) if (!isFarcasterRequest(request) || !normalisedName) return rewriter @@ -74,25 +110,22 @@ export const profileRewriter = async ({ if (!data || 'error' in data) return rewriter if (data.ethAddress) { - rewriter.on( - 'head', - new ElementsCreator([ - { - tagName: 'meta', - attributes: { name: 'fc:frame:button:2', content: 'View address' }, - }, - { - tagName: 'meta', - attributes: { name: 'fc:frame:button:2:action', content: 'link' }, - }, - { - tagName: 'meta', - attributes: { - name: 'fc:frame:button:2:target', - content: `https://ens.app/${data.ethAddress}`, - }, + elements.push( + { + tagName: 'meta', + attributes: { name: 'fc:frame:button:2', content: 'View address' }, + }, + { + tagName: 'meta', + attributes: { name: 'fc:frame:button:2:action', content: 'link' }, + }, + { + tagName: 'meta', + attributes: { + name: 'fc:frame:button:2:target', + content: `https://ens.app/${data.ethAddress}`, }, - ]), + }, ) } diff --git a/src/components/pages/profile/[name]/Profile.tsx b/src/components/pages/profile/[name]/Profile.tsx index 57d187534..ab6f9f58d 100644 --- a/src/components/pages/profile/[name]/Profile.tsx +++ b/src/components/pages/profile/[name]/Profile.tsx @@ -239,28 +239,35 @@ const ProfileContent = ({ isSelf, isLoading: parentIsLoading, name }: Props) => {titleContent} - {/* opengraph */} - - - - {/* farcaster */} + - - - - - {profile?.address && ( + {!!process.env.NEXT_PUBLIC_IPFS && ( <> - - - + {/* opengraph */} + + + + {/* farcaster */} + + + + + {profile?.address && ( + <> + + + + + )} + {/* twitter */} + + + )} - {/* twitter */} - - - - { {titleContent} - {/* opengraph */} - - - - {/* farcaster */} - - - - - {primaryProfile && ( + + {!!process.env.NEXT_PUBLIC_IPFS && ( <> - - - + {/* opengraph */} + + + + {/* farcaster */} + + + + + {primaryProfile && ( + <> + + + + + )} + {/* twitter */} + + + )} - {/* twitter */} - - - - {{