Skip to content

Commit

Permalink
Ensure all page JS is in functions rather than running at the top level
Browse files Browse the repository at this point in the history
Improve init code
  • Loading branch information
Daniel15 committed Apr 18, 2024
1 parent 164c8bc commit 1309882
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
20 changes: 11 additions & 9 deletions Daniel15.Web/wwwroot/Content/js/analytics.ts
@@ -1,12 +1,14 @@
import Plausible from 'plausible-tracker';

const {trackPageview, enableAutoOutboundTracking} = Plausible({
apiHost: 'https://d.sb',
trackLocalhost: false,
});
export function trackPageview(): void {
const {trackPageview, enableAutoOutboundTracking} = Plausible({
apiHost: 'https://d.sb',
trackLocalhost: false,
});

trackPageview();
// TODO: This doesn't provide a way to ignore links, which breaks the
// JS-powered links on the projects page.
// https://github.com/plausible/plausible-tracker/blob/c0b87d997d839938c23023d35bac0d6683635bbc/src/lib/tracker.ts#L292
//enableAutoOutboundTracking();
trackPageview();
// TODO: This doesn't provide a way to ignore links, which breaks the
// JS-powered links on the projects page.
// https://github.com/plausible/plausible-tracker/blob/c0b87d997d839938c23023d35bac0d6683635bbc/src/lib/tracker.ts#L292
//enableAutoOutboundTracking();
}
41 changes: 29 additions & 12 deletions Daniel15.Web/wwwroot/Content/js/main.ts
@@ -1,22 +1,39 @@
import '../css/main.less';

import {initBlog} from './pages/blog';
import {initIndex} from './pages/home';
import {initProjects} from './pages/projects';
import {initSocialFeed} from './pages/socialfeed';

import './analytics';
import '../not_an_easter_egg/secret';
import {trackPageview} from './analytics';
import {attachKonamiCodeListener} from '../not_an_easter_egg/secret';

switch (document.body.id) {
case 'site-index':
initIndex();
break;
function initPage(): void {
const controller = document.body.dataset.controller;
const action = document.body.dataset.action;
switch (controller) {
case 'blog':
initBlog();
break;

case 'site-socialfeed':
initSocialFeed();
break;
case 'projects':
initProjects();
break;

case 'project-index':
initProjects();
break;
case 'site':
switch (action) {
case 'index':
initIndex();
break;

case 'socialfeed':
initSocialFeed();
break;
}
break;
}
}

trackPageview();
initPage();
attachKonamiCodeListener();
8 changes: 6 additions & 2 deletions Daniel15.Web/wwwroot/Content/not_an_easter_egg/secret.ts
Expand Up @@ -15,7 +15,7 @@ const KEY_SEQUENCE = [
];
let step = 0;

window.addEventListener('keydown', evt => {
function checkKonamiCode(evt: KeyboardEvent): void {
// If incorrect key is pressed, start again!
if (evt.key.toLowerCase() !== KEY_SEQUENCE[step]) {
step = 0;
Expand All @@ -34,4 +34,8 @@ window.addEventListener('keydown', evt => {
newCSS.setAttribute('type', 'text/css');
head.appendChild(newCSS);
}
});
}

export function attachKonamiCodeListener(): void {
window.addEventListener('keydown', checkKonamiCode);
}

0 comments on commit 1309882

Please sign in to comment.