Skip to content

Commit

Permalink
feat: notes fallback languages and 404
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai committed Sep 19, 2023
1 parent 812397a commit 468a94d
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/routes/[lang=lang]/notes/[slug]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import { error, redirect } from '@sveltejs/kit';

// Based on visitors
const LANGUAGES = ['en', 'es', 'hu'];

async function findAvailableLang(
slug: string,
lang: string,
): Promise<string | null> {
const fallbackLanguages = LANGUAGES.filter((l) => l !== lang);

for (const lang of fallbackLanguages) {
try {
await import(`../../../../mdsvex/${slug}/${lang}.svx`);
return lang;
} catch (_) {
continue;
}
}

return null;
}

export async function load({ params }) {
// FIXME: I dont like this at all, needs refactor but its 1:37 AM
try {
const post = await import(
`../../../../mdsvex/${params.slug}/${params.lang}.svx`
Expand All @@ -9,11 +33,21 @@ export async function load({ params }) {
content,
};
} catch (error) {
const post = await import(`../../../../mdsvex/${params.slug}/en.svx`);
// Failed to find the requested language, try to find a fallback language
}

const fallbackLang = await findAvailableLang(params.slug, params.lang);

if (fallbackLang) {
const post = await import(
`../../../../mdsvex/${params.slug}/${fallbackLang}.svx`
);
const content = post.default;

return {
content,
};
}

throw error(404, 'Not found');
}

0 comments on commit 468a94d

Please sign in to comment.