From aa9107dc02d0c4f603edfdb06f1156a111b3e937 Mon Sep 17 00:00:00 2001 From: WardPearce Date: Sun, 15 Feb 2026 06:28:46 +1300 Subject: [PATCH] Use throw redirect --- materialious/src/routes/(app)/+page.ts | 8 +++- .../src/routes/(app)/playlist/+page.ts | 5 +-- .../src/routes/(app)/shorts/[slug]/+page.ts | 4 +- materialious/src/routes/(app)/watch/+page.ts | 6 +-- materialious/src/routes/+layout.ts | 38 ++++++++++++------- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/materialious/src/routes/(app)/+page.ts b/materialious/src/routes/(app)/+page.ts index 1a74d6b2..53ac18ee 100644 --- a/materialious/src/routes/(app)/+page.ts +++ b/materialious/src/routes/(app)/+page.ts @@ -2,7 +2,7 @@ import { goto } from '$app/navigation'; import { resolve } from '$app/paths'; import { getPopular } from '$lib/api/index'; import { isYTBackend } from '$lib/misc'; -import { feedCacheStore } from '$lib/store'; +import { feedCacheStore, invidiousInstanceStore } from '$lib/store'; import { error } from '@sveltejs/kit'; import { get } from 'svelte/store'; @@ -12,6 +12,12 @@ export async function load() { return; } + if (!get(invidiousInstanceStore)) { + return { + popularDisabled: false + }; + } + let popular = get(feedCacheStore).popular; let popularDisabled: boolean = false; diff --git a/materialious/src/routes/(app)/playlist/+page.ts b/materialious/src/routes/(app)/playlist/+page.ts index 7cd0b96b..81c0cc42 100644 --- a/materialious/src/routes/(app)/playlist/+page.ts +++ b/materialious/src/routes/(app)/playlist/+page.ts @@ -1,11 +1,10 @@ -import { goto } from '$app/navigation'; import { resolve } from '$app/paths'; -import { error } from '@sveltejs/kit'; +import { error, redirect } from '@sveltejs/kit'; export async function load({ url }) { const playlistId = url.searchParams.get('list'); if (playlistId) { - goto(resolve(`/playlist/[playlistId]`, { playlistId })); + throw redirect(302, resolve(`/playlist/[playlistId]`, { playlistId })); } else { error(404); } diff --git a/materialious/src/routes/(app)/shorts/[slug]/+page.ts b/materialious/src/routes/(app)/shorts/[slug]/+page.ts index 32c921dc..c6d9566e 100644 --- a/materialious/src/routes/(app)/shorts/[slug]/+page.ts +++ b/materialious/src/routes/(app)/shorts/[slug]/+page.ts @@ -1,6 +1,6 @@ -import { goto } from '$app/navigation'; import { resolve } from '$app/paths'; +import { redirect } from '@sveltejs/kit'; export async function load({ params }) { - goto(resolve('/watch/[videoId]', { videoId: params.slug })); + throw redirect(302, resolve('/watch/[videoId]', { videoId: params.slug })); } diff --git a/materialious/src/routes/(app)/watch/+page.ts b/materialious/src/routes/(app)/watch/+page.ts index eb02ba15..b041e5c1 100644 --- a/materialious/src/routes/(app)/watch/+page.ts +++ b/materialious/src/routes/(app)/watch/+page.ts @@ -1,7 +1,8 @@ -import { goto } from '$app/navigation'; import { resolve } from '$app/paths'; import { error } from '@sveltejs/kit'; +import { redirect } from '@sveltejs/kit'; + export async function load({ url }) { const videoId = url.searchParams.get('v'); const playlistId = url.searchParams.get('list'); @@ -18,8 +19,7 @@ export async function load({ url }) { goToUrl.searchParams.set('time', timestamp); } - // eslint-disable-next-line svelte/no-navigation-without-resolve - goto(goToUrl); + throw redirect(302, goToUrl); } else { error(404); } diff --git a/materialious/src/routes/+layout.ts b/materialious/src/routes/+layout.ts index 4fd98fb5..a5d6aad9 100644 --- a/materialious/src/routes/+layout.ts +++ b/materialious/src/routes/+layout.ts @@ -1,9 +1,9 @@ import { browser } from '$app/environment'; -import { goto } from '$app/navigation'; import { resolve } from '$app/paths'; +import { redirect } from '@sveltejs/kit'; import androidTv from '$lib/android/plugins/androidTv'; import { getResolveUrl } from '$lib/api'; -import '$lib/i18n'; // Import to initialize. Important :) +import '$lib/i18n'; import { initI18n } from '$lib/i18n'; import { getPages } from '$lib/navPages'; import { @@ -32,8 +32,6 @@ export async function load({ url }) { isAndroidTvStore.set((await androidTv.isAndroidTv()).value); if (Capacitor.getPlatform() === 'android') { - // Due to race condition with how we set & save persistent store values - // we manually set stores like auth & instance before load. const preferenceKey: Record> = { invidiousInstance: invidiousInstanceStore, authToken: invidiousAuthStore, @@ -42,22 +40,30 @@ export async function load({ url }) { }; for (const [key, store] of Object.entries(preferenceKey)) { - const result = await Preferences.get({ key: key }); - if (result.value !== null) store.set(deserialize(result.value)); + const result = await Preferences.get({ key }); + if (result.value !== null) { + store.set(deserialize(result.value)); + } } } const resolvedRoot = resolve('/', {}); + if (url.pathname.startsWith(resolvedRoot + '@')) { const username = url.pathname.substring(resolvedRoot.length).split('/')[0]; try { const resolvedUrl = await getResolveUrl(`www.youtube.com/${username}`); if (resolvedUrl.pageType === 'WEB_PAGE_TYPE_CHANNEL') { - goto(resolve(`/channel/[authorId]`, { authorId: resolvedUrl.ucid })); + throw redirect( + 302, + resolve(`/channel/[authorId]`, { + authorId: resolvedUrl.ucid + }) + ); } } catch { - // continue regardless of error + // ignore errors } } @@ -70,11 +76,11 @@ export async function load({ url }) { url.pathname === resolvedRoot && window.history.length < 3 ) { - getPages().forEach((page) => { + for (const page of getPages()) { if (page.href === defaultPage && (!page.requiresAuth || get(invidiousAuthStore))) { - goto(resolve(defaultPage, {})); + throw redirect(302, resolve(defaultPage, {})); } - }); + } } const isLoginPage = url.pathname.endsWith('/internal/login'); @@ -82,9 +88,13 @@ export async function load({ url }) { if (!isLoginPage) { if (isOwnBackend()?.requireAuth && !get(rawMasterKeyStore)) { - goto(resolve('/internal/login', {}), { replaceState: true }); - } else if (!get(invidiousInstanceStore) && !isYTBackend() && !isSetupPage) { - goto(resolve('/setup', {}), { replaceState: true }); + throw redirect(302, resolve('/internal/login', {})); + } + + if (!get(invidiousInstanceStore) && !isYTBackend() && !isSetupPage) { + throw redirect(302, resolve('/setup', {})); } } + + return {}; }