Created
May 3, 2024 20:55
-
-
Save thetafferboy/eb48404786ba3c002556573471007624 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
async function handleRequest(request) { | |
const originalResponse = await fetch(request) | |
const originalHtml = await originalResponse.text() | |
const url = new URL(request.url) | |
const nftMetadata = await fetchNFTMetadata(url.pathname) | |
const countryCode = nftMetadata.countryCode | |
const modifiedHtml = injectHrefLang(originalHtml, countryCode) | |
return new Response(modifiedHtml, { | |
headers: { 'Content-Type': 'text/html' } | |
}) | |
} | |
async function fetchNFTMetadata(path) { | |
return { | |
countryCode: 'US' | |
} | |
} | |
function injectHrefLang(html, countryCode) { | |
const hrefLangTag = `<link rel="alternate" hreflang="${countryCode}" href="${request.url}">` | |
const pos = html.indexOf('</head>') | |
if (pos === -1) return html | |
return html.slice(0, pos) + hrefLangTag + html.slice(pos) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment