Skip to content

Instantly share code, notes, and snippets.

@thetafferboy
Created May 3, 2024 20:55
Show Gist options
  • Save thetafferboy/eb48404786ba3c002556573471007624 to your computer and use it in GitHub Desktop.
Save thetafferboy/eb48404786ba3c002556573471007624 to your computer and use it in GitHub Desktop.
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