Created
June 24, 2022 16:00
-
-
Save sanscheese/eac33ea8aeda55c01748e5c2c2e6b58b to your computer and use it in GitHub Desktop.
Edge function - Dynamic pricing
This file contains 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
import { Context } from "netlify:edge" | |
export default async (request: Request, context: Context) => { | |
const url = new URL(request.url) | |
context.log(`Updating price ${url}`) | |
// Get the page content | |
const response = await context.next() | |
const page = await response.text() | |
// Pricing | |
const regex_hobby = /{{PRICE_HOBBY}}/ig | |
const regex_freelance = /{{PRICE_FREELANCE}}/ig | |
const prices: any = { | |
'GB': { | |
hobby: '£5', | |
freelance: '£20' | |
}, | |
'AU': { | |
hobby: '$50', | |
freelance: '$300' | |
}, | |
'US': { | |
hobby: '$2', | |
freelance: '$5' | |
}, | |
} | |
const countryCode = context.geo.country?.code || 'GB' | |
// Replace the content | |
const updatedPage = page | |
.replace(regex_hobby, prices[countryCode].hobby) | |
.replace(regex_freelance, prices[countryCode].freelance) | |
return new Response(updatedPage, response); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment