Skip to content

Commit 6fdbfc8

Browse files
committed
Fix cookie control
1 parent 47de9b7 commit 6fdbfc8

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

api/country.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function handler (req, res) {
2+
const country = (req.headers['cf-ipcountry'] || '').toUpperCase()
3+
res.status(200).json({ country })
4+
}

bin/vite/dev-server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ async function createServer () {
129129

130130
app.use(vite.middlewares)
131131

132+
app.get('/api/country', (req, res) => {
133+
const country = (req.headers['cf-ipcountry'] || 'BG').toUpperCase()
134+
res.json({ country })
135+
})
136+
132137
app.get('/', handleIndex)
133138
app.get('/videos', handleVideosIndex)
134139
app.get('/videos/:videoSlug', handleVideo)

src/css/modern.styl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,11 @@ a:hover
13511351

13521352
/* ===== Cookie Consent ===== */
13531353
#cookie-consent
1354+
position fixed
1355+
bottom 0
1356+
left 0
1357+
right 0
1358+
z-index 9999
13541359
background #fff
13551360
border-top 1px solid var(--color-border)
13561361
box-shadow 0 -2px 10px rgba(0, 0, 0, .06)

src/views/parts/react-footer.pug

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,34 @@ script(type='module').
2222

2323
script.
2424
(function(){
25+
var REGULATED = ['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','CH','GB'];
2526
var consent = localStorage.getItem('cookie-consent');
26-
if (!consent) {
27-
document.getElementById('cookie-consent').style.display = 'block';
28-
} else if (consent === 'accepted') {
27+
if (consent === 'accepted') {
2928
loadGoogleScripts();
29+
return;
30+
}
31+
if (consent === 'rejected') {
32+
return;
3033
}
34+
fetch('/api/country').then(function(r){ return r.json(); }).then(function(d){
35+
var cc = (d.country || '').toUpperCase();
36+
if (cc && REGULATED.indexOf(cc) === -1) {
37+
localStorage.setItem('cookie-consent', 'accepted');
38+
loadGoogleScripts();
39+
} else {
40+
document.getElementById('cookie-consent').style.display = 'block';
41+
}
42+
}).catch(function(){
43+
document.getElementById('cookie-consent').style.display = 'block';
44+
});
3145
document.getElementById('cookie-accept').addEventListener('click', function() {
3246
localStorage.setItem('cookie-consent', 'accepted');
3347
document.getElementById('cookie-consent').style.display = 'none';
34-
document.body.style.paddingBottom = '';
3548
loadGoogleScripts();
3649
});
3750
document.getElementById('cookie-reject').addEventListener('click', function() {
3851
localStorage.setItem('cookie-consent', 'rejected');
3952
document.getElementById('cookie-consent').style.display = 'none';
40-
document.body.style.paddingBottom = '';
4153
});
4254
function loadGoogleScripts() {
4355
var s1 = document.createElement('script');

0 commit comments

Comments
 (0)