Skip to content

Commit

Permalink
chore: Choose osano variant based on country
Browse files Browse the repository at this point in the history
  • Loading branch information
LizBaker committed Mar 20, 2024
1 parent 3b41092 commit ac1ca11
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ yarn-error.log
.devcontainer

src/images/infrastructure_screenshot_full_sonarqube-dashboard.webp

# Local Netlify folder
.netlify
4 changes: 4 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ package = "@netlify/plugin-gatsby"
[functions]
included_files = ["!.cache/data/datastore/data.mdb","!.cache/query-engine"]

[[edge_functions]]
path = "/*"
function = "osano-country"

[[headers]]
for = "/*"

Expand Down
67 changes: 67 additions & 0 deletions netlify/edge-functions/osano-country.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { HTMLRewriter } from 'https://ghuc.cc/worker-tools/html-rewriter/index.ts';

export default async (request, context) => {
const response = await context.next();
const hasGdpr = [
'AT',
'BE',
'BG',
'HR',
'CY',
'CZ',
'DK',
'EE',
'FI',
'FR',
'DE',
'GR',
'HU',
'IE',
'IT',
'LV',
'LT',
'LU',
'MT',
'NL',
'PL',
'PT',
'RO',
'SK',
'SI',
'ES',
'SE',
].includes(context.geo.country.code);

if (hasGdpr) {
return (
new HTMLRewriter()
// .on('script', {
// text(text) {
// buffer += text.text;

// if (text.lastInTextNode) {
// text.replace(buffer.replace(/variant=one/gi, 'variant=two'));
// buffer = '';
// } else {
// text.remove();
// }
// },
// })
.on('script', {
element(element) {
const scriptSrc = element.getAttribute('src');
if (
typeof scriptSrc === 'string' &&
scriptSrc.includes('cmp.osano.com/')
) {
element.setAttribute(
'src',
scriptSrc.replace(/variant=one/gi, 'variant=two')
);
}
},
})
.transform(response)
);
}
};

0 comments on commit ac1ca11

Please sign in to comment.