Skip to content

Commit 1309882

Browse files
committed
Ensure all page JS is in functions rather than running at the top level
Improve init code
1 parent 164c8bc commit 1309882

3 files changed

Lines changed: 46 additions & 23 deletions

File tree

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Plausible from 'plausible-tracker';
22

3-
const {trackPageview, enableAutoOutboundTracking} = Plausible({
4-
apiHost: 'https://d.sb',
5-
trackLocalhost: false,
6-
});
3+
export function trackPageview(): void {
4+
const {trackPageview, enableAutoOutboundTracking} = Plausible({
5+
apiHost: 'https://d.sb',
6+
trackLocalhost: false,
7+
});
78

8-
trackPageview();
9-
// TODO: This doesn't provide a way to ignore links, which breaks the
10-
// JS-powered links on the projects page.
11-
// https://github.com/plausible/plausible-tracker/blob/c0b87d997d839938c23023d35bac0d6683635bbc/src/lib/tracker.ts#L292
12-
//enableAutoOutboundTracking();
9+
trackPageview();
10+
// TODO: This doesn't provide a way to ignore links, which breaks the
11+
// JS-powered links on the projects page.
12+
// https://github.com/plausible/plausible-tracker/blob/c0b87d997d839938c23023d35bac0d6683635bbc/src/lib/tracker.ts#L292
13+
//enableAutoOutboundTracking();
14+
}
Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
import '../css/main.less';
22

3+
import {initBlog} from './pages/blog';
34
import {initIndex} from './pages/home';
45
import {initProjects} from './pages/projects';
56
import {initSocialFeed} from './pages/socialfeed';
67

7-
import './analytics';
8-
import '../not_an_easter_egg/secret';
8+
import {trackPageview} from './analytics';
9+
import {attachKonamiCodeListener} from '../not_an_easter_egg/secret';
910

10-
switch (document.body.id) {
11-
case 'site-index':
12-
initIndex();
13-
break;
11+
function initPage(): void {
12+
const controller = document.body.dataset.controller;
13+
const action = document.body.dataset.action;
14+
switch (controller) {
15+
case 'blog':
16+
initBlog();
17+
break;
1418

15-
case 'site-socialfeed':
16-
initSocialFeed();
17-
break;
19+
case 'projects':
20+
initProjects();
21+
break;
1822

19-
case 'project-index':
20-
initProjects();
21-
break;
23+
case 'site':
24+
switch (action) {
25+
case 'index':
26+
initIndex();
27+
break;
28+
29+
case 'socialfeed':
30+
initSocialFeed();
31+
break;
32+
}
33+
break;
34+
}
2235
}
36+
37+
trackPageview();
38+
initPage();
39+
attachKonamiCodeListener();

Daniel15.Web/wwwroot/Content/not_an_easter_egg/secret.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const KEY_SEQUENCE = [
1515
];
1616
let step = 0;
1717

18-
window.addEventListener('keydown', evt => {
18+
function checkKonamiCode(evt: KeyboardEvent): void {
1919
// If incorrect key is pressed, start again!
2020
if (evt.key.toLowerCase() !== KEY_SEQUENCE[step]) {
2121
step = 0;
@@ -34,4 +34,8 @@ window.addEventListener('keydown', evt => {
3434
newCSS.setAttribute('type', 'text/css');
3535
head.appendChild(newCSS);
3636
}
37-
});
37+
}
38+
39+
export function attachKonamiCodeListener(): void {
40+
window.addEventListener('keydown', checkKonamiCode);
41+
}

0 commit comments

Comments
 (0)