diff --git a/src/routes/[slug]/[...tab].tsx b/src/routes/[slug]/[...tab].tsx index 5806a6b9..ef9fc842 100644 --- a/src/routes/[slug]/[...tab].tsx +++ b/src/routes/[slug]/[...tab].tsx @@ -1,6 +1,6 @@ -import { RouteDefinition, RouteSectionProps, useLocation } from '@solidjs/router' +import { RouteDefinition, RouteSectionProps, createAsync, useLocation } from '@solidjs/router' import { HttpStatusCode } from '@solidjs/start' -import { ErrorBoundary, Show, Suspense, createEffect, on, createSignal, onMount } from 'solid-js' +import { ErrorBoundary, Show, Suspense, createEffect, on, onMount } from 'solid-js' import { FourOuFourView } from '~/components/Views/FourOuFour' import { Loading } from '~/components/_shared/Loading' import { gaIdentity } from '~/config' @@ -15,21 +15,17 @@ import { ReactionsProvider } from '../../context/reactions' import AuthorPage, { AuthorPageProps } from '../author/[slug]/[...tab]' import TopicPage, { TopicPageProps } from '../topic/[slug]/[...tab]' -// Simplified fetch function for the shout const fetchShout = async (slug: string): Promise => { - console.debug('fetchShout called with slug:', slug) - if (slug.startsWith('@')) return // Skip fetching article for slugs starting with @ (author pages) - const result = await getShout({ slug }) - console.debug('fetchShout result:', result) + if (slug.startsWith('@')) return + const shoutLoader = getShout({ slug }) + const result = await shoutLoader() return result } export const route: RouteDefinition = { - load: async ({ params }) => { - const article = await fetchShout(params.slug) - console.debug('route.load fetched article:', article) - return { article } - } + load: async ({ params }) => ({ + article: await fetchShout(params.slug) + }) } export type ArticlePageProps = { @@ -48,7 +44,6 @@ export type SlugPageProps = { } export default function ArticlePage(props: RouteSectionProps) { - // If the slug starts with '@', render as author page if (props.params.slug.startsWith('@')) { console.debug('[routes] [slug]/[...tab] starts with @, render as author page') const patchedProps = { @@ -61,7 +56,6 @@ export default function ArticlePage(props: RouteSectionProps) { return } - // If the slug starts with '!', render as topic page if (props.params.slug.startsWith('!')) { console.debug('[routes] [slug]/[...tab] starts with !, render as topic page') const patchedProps = { @@ -74,40 +68,14 @@ export default function ArticlePage(props: RouteSectionProps) { return } - // Handle regular article slugs function ArticlePage(props: RouteSectionProps) { const loc = useLocation() const { t } = useLocalize() + const data = createAsync(async () => props.data?.article || (await fetchShout(props.params.slug))) - // Set up a signal for article data and loading state - const [article, setArticle] = createSignal(props.data?.article) - const [isLoading, setIsLoading] = createSignal(false) - - // Fetch article data manually based on slug changes - createEffect(() => { - const slug = props.params.slug - if (!slug) return - - setIsLoading(true) - fetchShout(slug) - .then((result) => { - setArticle(result) - if (!result) { - console.warn('No article data found for slug:', slug) - } - }) - .catch((error) => { - console.error('Error fetching shout:', error) - }) - .finally(() => setIsLoading(false)) - }) - - // onMount to load GA script onMount(async () => { - console.debug('onMount triggered') - if (gaIdentity && article()?.id) { + if (gaIdentity && data()?.id) { try { - console.debug('Loading GA script') await loadGAScript(gaIdentity) initGA(gaIdentity) } catch (error) { @@ -116,10 +84,9 @@ export default function ArticlePage(props: RouteSectionProps) { } }) - // Google Analytics effect createEffect( on( - article, + data, (a?: Shout) => { if (!a?.id) return window?.gtag?.('event', 'page_view', { @@ -136,25 +103,24 @@ export default function ArticlePage(props: RouteSectionProps) { }> }> - {console.warn('Rendering 404 error page - no article data found')} } > - + @@ -162,6 +128,5 @@ export default function ArticlePage(props: RouteSectionProps) { ) } - return }