Browse Source

Do browser-side rendering for articles and add error handling

master
buttercat1791 2 years ago committed by limina1
parent
commit
7225f24cca
  1. 4
      src/routes/[id]/+page.svelte
  2. 20
      src/routes/[id]/+page.ts
  3. 4
      src/routes/d/[tag]/+page.svelte
  4. 23
      src/routes/d/[tag]/+page.ts

4
src/routes/[id]/+page.svelte

@ -3,8 +3,10 @@
import type { PageData } from './$types'; import type { PageData } from './$types';
export let data: PageData; export let data: PageData;
let { event } = data;
</script> </script>
<main> <main>
<Article event={data.event} /> <Article {event} />
</main> </main>

20
src/routes/[id]/+page.ts

@ -1,12 +1,26 @@
import { getNdkInstance, ndk } from '$lib/ndk'; import { getNdkInstance, ndk } from '$lib/ndk';
import type { NDKEvent } from '@nostr-dev-kit/ndk';
import { error } from '@sveltejs/kit';
// MichaelJ - 23 July 2024 - Disable server-side rendering so that the load function can use the
// browser's local storage to retrieve saved relays and the cache adapter for the NDK instance.
export const ssr = false;
export const load = async ({ params }) => { export const load = async ({ params }) => {
// TODO: Don't rely on browser cache here.
const ndk = getNdkInstance(); const ndk = getNdkInstance();
const { id } = params; const { id } = params;
// TODO: Add error handling. let event: NDKEvent | null | undefined;
const event = await ndk.fetchEvent(id);
try {
event = await ndk.fetchEvent(id);
} catch (err) {
console.error(err);
}
if (!event) {
error(404, 'No event found with the given ID.');
}
return { event }; return { event };
}; };

4
src/routes/d/[tag]/+page.svelte

@ -3,8 +3,10 @@
import type { PageData } from './$types'; import type { PageData } from './$types';
export let data: PageData; export let data: PageData;
let { event } = data;
</script> </script>
<main> <main>
<Article event={data.event} /> <Article {event} />
</main> </main>

23
src/routes/d/[tag]/+page.ts

@ -1,13 +1,28 @@
import { getNdkInstance } from "$lib/ndk"; import { getNdkInstance } from "$lib/ndk";
import type { NDKEvent } from "@nostr-dev-kit/ndk";
import { error } from "@sveltejs/kit";
// MichaelJ - 23 July 2024 - Disable server-side rendering so that the load function can use the
// browser's local storage to retrieve saved relays and the cache adapter for the NDK instance.
export const ssr = false;
export const load = async ({ params }) => { export const load = async ({ params }) => {
// TODO: Don't rely on browser cache here.
const ndk = getNdkInstance(); const ndk = getNdkInstance();
const { tag } = params; const { tag } = params;
// TODO: Add error handling. let events: Set<NDKEvent> = new Set();
const events = await ndk.fetchEvents({ '#d': [tag] }); let event: NDKEvent | null | undefined;
const event = events.values().next().value;
try {
events = await ndk.fetchEvents({ '#d': [tag] });
event = events.values().next().value;
} catch (err) {
console.error(err);
}
if (events.size === 0) {
error(404, 'No events found with the given d tag.');
}
return { event }; return { event };
}; };

Loading…
Cancel
Save