404 redirect if wrong locale or page

This commit is contained in:
vcoppe
2024-07-26 00:03:29 +02:00
parent ca98c48a5c
commit cbb6920ebf
3 changed files with 46 additions and 18 deletions

View File

@@ -1,4 +1,6 @@
<script lang="ts">
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
import { _, locale } from 'svelte-i18n';
export let path: string;
@@ -17,7 +19,11 @@
}
$: if ($locale) {
loadModule(`/src/lib/docs/${$locale}/${path}`);
if (modules.hasOwnProperty(`/src/lib/docs/${$locale}/${path}`)) {
loadModule(`/src/lib/docs/${$locale}/${path}`);
} else if (browser) {
goto(`/404`);
}
}
</script>

View File

@@ -51,11 +51,15 @@ export function getPreviousGuide(currentGuide: string): string | undefined {
return `${previousGuide}/${guides[previousGuide][guides[previousGuide].length - 1]}`;
}
} else {
let subguideIndex = guides[subguides[0]].indexOf(subguides[1]);
if (subguideIndex > 0) {
return `${subguides[0]}/${guides[subguides[0]][subguideIndex - 1]}`;
if (guides.hasOwnProperty(subguides[0])) {
let subguideIndex = guides[subguides[0]].indexOf(subguides[1]);
if (subguideIndex > 0) {
return `${subguides[0]}/${guides[subguides[0]][subguideIndex - 1]}`;
} else {
return subguides[0];
}
} else {
return subguides[0];
return undefined;
}
}
}
@@ -64,21 +68,29 @@ export function getNextGuide(currentGuide: string): string | undefined {
let subguides = currentGuide.split('/');
if (subguides.length === 1) {
if (guides[currentGuide].length === 0) {
let keys = Object.keys(guides);
let index = keys.indexOf(currentGuide);
return keys[index + 1];
if (guides.hasOwnProperty(currentGuide)) {
if (guides[currentGuide].length === 0) {
let keys = Object.keys(guides);
let index = keys.indexOf(currentGuide);
return keys[index + 1];
} else {
return `${currentGuide}/${guides[currentGuide][0]}`;
}
} else {
return `${currentGuide}/${guides[currentGuide][0]}`;
return undefined;
}
} else {
let subguideIndex = guides[subguides[0]].indexOf(subguides[1]);
if (subguideIndex < guides[subguides[0]].length - 1) {
return `${subguides[0]}/${guides[subguides[0]][subguideIndex + 1]}`;
if (guides.hasOwnProperty(subguides[0])) {
let subguideIndex = guides[subguides[0]].indexOf(subguides[1]);
if (subguideIndex < guides[subguides[0]].length - 1) {
return `${subguides[0]}/${guides[subguides[0]][subguideIndex + 1]}`;
} else {
let keys = Object.keys(guides);
let index = keys.indexOf(subguides[0]);
return keys[index + 1];
}
} else {
let keys = Object.keys(guides);
let index = keys.indexOf(subguides[0]);
return keys[index + 1];
return undefined;
}
}
}

View File

@@ -6,11 +6,21 @@
import Head from '$lib/components/Head.svelte';
import Nav from '$lib/components/Nav.svelte';
import Footer from '$lib/components/Footer.svelte';
import { languages } from '$lib/languages';
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
$: if ($page.params.language === '' && $locale !== 'en') {
locale.set('en');
} else if ($page.params.language && $locale !== $page.params.language) {
locale.set($page.params.language.replace('/', ''));
} else if ($page.params.language) {
let lang = $page.params.language.replace('/', '');
if ($locale !== lang) {
if (languages.hasOwnProperty(lang)) {
locale.set(lang);
} else if (browser) {
goto('/404');
}
}
}
const appRoutes = ['/[...language]/app', '/[...language]/embed'];