Skip to content

Commit

Permalink
edge function for image request
Browse files Browse the repository at this point in the history
  • Loading branch information
boid-com committed Nov 2, 2024
1 parent 15c1779 commit 68fccc9
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 163 deletions.
22 changes: 18 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
"editor.guides.bracketPairs": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.codeActionsOnSave": ["source.fixAll.eslint"],
"eslint.validate": ["javascript", "javascriptreact", "typescript", "vue"],
"editor.codeActionsOnSave": [
"source.fixAll.eslint"
],
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"vue"
],
"typescript.tsdk": "node_modules/typescript/lib",
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand All @@ -14,5 +21,12 @@
},
"[html]": {
"editor.defaultFormatter": "vscode.html-language-features"
}
}
},
"deno.enable": true,
"deno.enablePaths": [
"netlify/edge-functions"
],
"deno.unstable": true,
"deno.importMap": ".netlify/edge-functions-import-map.json",
"deno.path": "/Users/boidcom/Library/Preferences/netlify/deno-cli/deno"
}
37 changes: 16 additions & 21 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,33 @@
<html lang="en">

<head>
<meta charset="utf-8">
<link rel="preconnect" href="https://umami.fiddl.animus.is">
<script defer src="https://umami.fiddl.animus.is/script.js"
data-website-id="a6295092-8b12-42b8-ab4c-de684673248d"></script>

<link rel="canonical" href="https://fiddl.art" id="canonical-link">

<!-- Primary Meta Tags -->
<title>Fiddl.art</title>
<meta name="title" content="Fiddl.art" />
<meta name="description" content="Create and earn with generative art." />
<meta name="description" content="Create and earn with generative art.">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="robots" content="index, follow">

<!-- Open Graph / Facebook -->
<meta property="og:type" content="website" />
<!-- <meta property="og:url" content="https://alpha.fiddl.art/r/StxXwHQySwGBzFrsGFJKIQ?index=0" /> -->
<meta property="og:title" content="Fiddl.art" />
<meta property="og:description" content="Create and earn with generative art." />
<meta property="og:image" content="/fiddlSocialCard.jpg" />
<meta property="og:type" content="website">
<meta property="og:title" content="Fiddl.art">
<meta property="og:description" content="Create and earn with generative art.">
<meta property="og:image" content="https://fiddl.art/fiddlSocialCard.jpg">

<!-- Twitter -->
<meta property="twitter:card" content="summary" />
<!-- <meta property="twitter:url" content="https://alpha.fiddl.art/r/StxXwHQySwGBzFrsGFJKIQ?index=0" /> -->
<meta property="twitter:title" content="Fidd.art" />
<meta property="twitter:description" content="Create and earn with generative art." />
<meta property="twitter:image" content="/fiddlSocialCard" />
<meta property="twitter:card" content="summary">
<meta property="twitter:title" content="Fiddl.art">
<meta property="twitter:description" content="Create and earn with generative art.">
<meta property="twitter:image" content="https://fiddl.art/fiddlSocialCard.jpg">
<meta name="twitter:site" content="@fiddlart">

<meta name="robots" content="index, follow">
<meta charset="utf-8">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport"
content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width<% if (ctx.mode.cordova || ctx.mode.capacitor) { %>, viewport-fit=cover<% } %>">

<!-- Favicon -->
<link rel="icon" type="image/png" sizes="128x128" href="icons/favicon-128x128.png">
<link rel="icon" type="image/png" sizes="96x96" href="icons/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="32x32" href="icons/favicon-32x32.png">
Expand Down
6 changes: 5 additions & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
base = "."
publish = "dist/spa"
command = "yarn build"
# functions = "netlify/functions/dist"

[[edge_functions]]
function = "handleRequestPage"
path = "/r/*"

[functions]
included_files = ["netlify/edge-functions/lib/**"]

# [[redirects]]
# from = "/r/*"
# to = "/.netlify/functions/renderMetadata"
Expand All @@ -21,3 +23,5 @@

[images]
remote_images = ["https://api.fiddl.art/images/.*"]


3 changes: 3 additions & 0 deletions netlify/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
25 changes: 11 additions & 14 deletions netlify/edge-functions/handleRequestPage.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { Context } from "@netlify/edge-functions"

import { setSocialMetadata } from "./lib/util.ts"
export default async (request: Request, context: Context) => {
const url = new URL(request.url)
// request.
// Since this function is only triggered by the path defined in netlify.toml, no need to check the URL path
const metaTags = `
<meta name="description" content="Special content for ${url.pathname}">
<meta property="og:title" content="Custom Page ${url.pathname}">
<meta property="og:description" content="Detailed description for ${url.pathname}">
`

const response = await context.next()
const text = await response.text()
for (const [key] of url.searchParams) {
if (key !== "index") {
url.searchParams.delete(key)
}
}

// Modify the HTML response
let text = await response.text()
text = text.replace("</head>", `${metaTags}</head>`)

return new Response(text, response)
const segments = url.pathname.split("/")
const id = segments[2]
const updatedHtml = setSocialMetadata(text, id || "image id", "My Description", "https://api.fiddl.art/images/07f9e0f4-cb76-4408-9ed4-7815781bd957-lg.webp", url.toString())
return new Response(updatedHtml, response)
}
41 changes: 41 additions & 0 deletions netlify/edge-functions/lib/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export function setSocialMetadata(html: string, title: string, description: string, imageUrl: string, canonicalUrl: string): string {
// Helper to create or update meta tags in HTML content
const updateMetaTag = (html: string, property: string, content: string, attribute: string = "content"): string => {
// Matching meta tags with flexible attribute formats
const metaTagRegex = new RegExp(`<meta\\s+[^>]*?property=["']?${property}["']?\\s+[^>]*${attribute}=["']?[^"']*["']?\\s*/?>`, "gi")
const newMetaTag = `<meta property="${property}" ${attribute}="${content}" />`

if (html.search(metaTagRegex) !== -1) {
// Replace existing meta tag
return html.replace(metaTagRegex, newMetaTag)
} else {
// Append new meta tag if not found
return html.replace("</head>", ` ${newMetaTag}\n</head>`)
}
}

const updateLinkTag = (html: string, rel: string, href: string): string => {
// Matching link tags with flexible attribute formats
const linkTagRegex = new RegExp(`<link\\s+[^>]*?rel=["']?${rel}["']?\\s+[^>]*href=["']?[^"']*["']?\\s*/?>`, "gi")
const newLinkTag = `<link rel="${rel}" href="${href}" />`

if (html.search(linkTagRegex) !== -1) {
// Replace existing link tag
return html.replace(linkTagRegex, newLinkTag)
} else {
// Append new link tag if not found
return html.replace("</head>", ` ${newLinkTag}\n</head>`)
}
}

// Update meta tags
html = updateMetaTag(html, "og:title", title)
html = updateMetaTag(html, "og:description", description)
html = updateMetaTag(html, "og:image", imageUrl)
html = updateMetaTag(html, "twitter:title", title)
html = updateMetaTag(html, "twitter:description", description)
html = updateMetaTag(html, "twitter:image", imageUrl)
html = updateLinkTag(html, "canonical", canonicalUrl)

return html
}
11 changes: 11 additions & 0 deletions netlify/edge-functions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"strict": true,
"esModuleInterop": true,
"allowImportingTsExtensions": true,
"noEmit": true,
"moduleResolution": "NodeNext"
}
}
64 changes: 0 additions & 64 deletions netlify/functions/renderMetadata.ts

This file was deleted.

10 changes: 0 additions & 10 deletions netlify/functions/tsconfig.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"test": "echo \"No test specified\" && exit 0",
"dev": "quasar dev",
"build": "quasar build && yarn build:func",
"build": "quasar build",
"build:func": "tsc -p netlify/functions/tsconfig.json",
"dev:func": "tsc -p netlify/functions/tsconfig.json --watch",
"lint": "eslint --fix ./src",
Expand Down
38 changes: 23 additions & 15 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,66 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://alpha.fiddl.art/</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/stats</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/search</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/create</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/vote</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/mint</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/browse</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/account</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<loc>https://alpha.fiddl.art/a</loc>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/settings</loc>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/creations/</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/addPoints</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/login</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/r/</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/admin</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/claim/</loc>
<lastmod>2024-10-31T16:56:59.707Z</lastmod>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
<url>
<loc>https://alpha.fiddl.art/tos</loc>
<lastmod>2024-11-02T04:53:07.158Z</lastmod>
</url>
</urlset>
4 changes: 2 additions & 2 deletions src/components/dialogs/ImageGallery.vue
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ export default defineComponent({
})
} else {
this.userLikedImage = !this.userLikedImage
if (this.userLikedImage) this.$api.collections.likeImage.mutate(this.currentImageId)
else this.$api.collections.unlikeImage.mutate(this.currentImageId)
if (this.userLikedImage) void this.$api.collections.likeImage.mutate(this.currentImageId)
else void this.$api.collections.unlikeImage.mutate(this.currentImageId)
}
},
editImage() {
Expand Down
Loading

0 comments on commit 68fccc9

Please sign in to comment.