reworked to only load pages if needed

This commit is contained in:
CodingPhoenixx
2026-02-13 10:53:09 +01:00
parent e77894cd48
commit 7f229266c4
2 changed files with 44 additions and 20 deletions
+35 -15
View File
@@ -1,27 +1,22 @@
import { LitElement, html, css } from 'lit'; import { LitElement, html, css } from 'lit';
import { customElement, state } from 'lit/decorators.js'; import { customElement, state } from 'lit/decorators.js';
import { Router } from './router/router';
import './pages/not-found-page'; import './pages/not-found-page';
import './components/nav-bar'; import './components/nav-bar';
import './components/cc/cc-nav-bar'; import './components/cc/cc-nav-bar';
import './components/footer-bar'; import './components/footer-bar';
import { Router } from './router/router';
import './pages/home-page';
import './pages/competition-page';
import './pages/auth/login-page';
import './pages/auth/register-page';
import './pages/cc/cc-home-page';
@customElement('app-root') @customElement('app-root')
export class AppRoot extends LitElement { export class AppRoot extends LitElement {
private router!: Router; private router!: Router;
@state() private isCompetitionCenter = false; @state() private isCompetitionCenter = false;
static styles = css` static styles = css`
:host { :host {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
min-height: 100vh; min-height: 100vh;
} }
nav-bar { nav-bar {
flex: 0 0 auto; flex: 0 0 auto;
@@ -41,11 +36,36 @@ export class AppRoot extends LitElement {
firstUpdated() { firstUpdated() {
const outlet = this.shadowRoot?.getElementById('outlet') as HTMLElement; const outlet = this.shadowRoot?.getElementById('outlet') as HTMLElement;
this.router = new Router(outlet, [ this.router = new Router(outlet, [
{ path: '/', view: () => document.createElement('home-page') }, {
{ path: '/competitions', view: () => document.createElement('competition-page') }, path: '/', view: async () => {
{ path: '/login', view: () => document.createElement('login-page') }, await import('./pages/home-page.js');
{ path: '/register', view: () => document.createElement('register-page') }, return document.createElement('home-page');
{ path: '/cc/', view: () => document.createElement('cc-home-page') }, }
},
{
path: '/competitions', view: async () => {
await import('./pages/competition-page.js');
return document.createElement('competition-page');
}
},
{
path: '/login', view: async () => {
await import('./pages/auth/login-page.js');
return document.createElement('login-page')
}
},
{
path: '/register', view: async () => {
await import('./pages/auth/register-page.js');
return document.createElement('register-page');
}
},
{
path: '/cc/', view: async () => {
await import('./pages/cc/cc-home-page.js');
return document.createElement('cc-home-page');
}
},
]); ]);
this.router.onRouteChange = (path: string) => { this.router.onRouteChange = (path: string) => {
+9 -5
View File
@@ -1,7 +1,7 @@
export type Route = { export type Route = {
path: string; path: string;
view: () => HTMLElement; view: () => Promise<HTMLElement> | HTMLElement;
}; };
export class Router { export class Router {
@@ -20,13 +20,17 @@ export class Router {
this.resolve(); this.resolve();
} }
resolve() { async resolve() {
const path = window.location.pathname; const path = window.location.pathname;
const match = this.routes.find((r) => r.path === path); const match = this.routes.find((r) => r.path === path);
this.outlet.innerHTML = ''; this.outlet.innerHTML = '';
this.outlet.append(
match ? match.view() : document.createElement('not-found-page') if (match) {
); const view = await match.view();
this.outlet.append(view);
} else {
this.outlet.append(document.createElement('not-found-page'));
}
if (this.onRouteChange) { if (this.onRouteChange) {
this.onRouteChange(path); this.onRouteChange(path);