Skip to content

Instantly share code, notes, and snippets.

@sanscheese
Created June 24, 2022 16:00
Show Gist options
  • Save sanscheese/eac33ea8aeda55c01748e5c2c2e6b58b to your computer and use it in GitHub Desktop.
Save sanscheese/eac33ea8aeda55c01748e5c2c2e6b58b to your computer and use it in GitHub Desktop.
Edge function - Dynamic pricing
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