|
13 | 13 | import TrainingPage from '../pages/training/TrainingPage.svelte'; |
14 | 14 | import PlaygroundPage from '../pages/PlaygroundPage.svelte'; |
15 | 15 | import { currentPageComponent } from '../views/currentComponentStore'; |
16 | | - import { currentPath, navigate, Paths, PathType } from './paths'; |
| 16 | + import { currentPath, isValidPath, navigate, Paths, PathType } from './paths'; |
| 17 | +
|
| 18 | + const stripLeadingSlash = (s: string): string => (s.startsWith('/') ? s.slice(1) : s); |
17 | 19 |
|
18 | 20 | function getRoutedComponent(path: PathType) { |
19 | 21 | switch (path) { |
|
44 | 46 |
|
45 | 47 | if (shouldPushState) { |
46 | 48 | const url = |
47 | | - window.location.origin + |
48 | | - import.meta.env.BASE_URL + |
49 | | - (path.startsWith('/') ? path.slice(1) : path); |
| 49 | + window.location.origin + import.meta.env.BASE_URL + stripLeadingSlash(path); |
50 | 50 | history.pushState({ path: path }, '', url); |
51 | 51 | } |
52 | 52 | }; |
|
68 | 68 | }; |
69 | 69 |
|
70 | 70 | const navigateFromUrl = () => { |
71 | | - let urlPath = window.location.pathname; |
72 | | - if (urlPath.startsWith('/')) { |
73 | | - urlPath = urlPath.substring(1, urlPath.length); |
74 | | - } |
75 | | - let path: PathType = Paths.HOME; |
76 | | - if (Object.values(Paths).includes(urlPath as PathType)) { |
77 | | - path = urlPath as PathType; |
| 71 | + const path = stripLeadingSlash(window.location.pathname).slice( |
| 72 | + stripLeadingSlash(import.meta.env.BASE_URL).length, |
| 73 | + ); |
| 74 | + if (isValidPath(path)) { |
78 | 75 | navigate(path); |
79 | 76 | } else { |
80 | 77 | history.replaceState({}, '', Paths.HOME); |
|
0 commit comments