From 08d146b8f7aacb51be1c87b4557ace6b41dff39b Mon Sep 17 00:00:00 2001 From: silberengel Date: Sat, 2 Aug 2025 00:31:35 +0200 Subject: [PATCH] fix problem with trying to call websockets from the browser --- .../[type]/[identifier]/+layout.server.ts | 35 +++------- .../[type]/[identifier]/+layout.ts | 66 ++++++++++++++++++- .../[type]/[identifier]/+page.server.ts | 35 ++-------- .../publication/[type]/[identifier]/+page.ts | 54 +++++++++++++++ 4 files changed, 135 insertions(+), 55 deletions(-) create mode 100644 src/routes/publication/[type]/[identifier]/+page.ts diff --git a/src/routes/publication/[type]/[identifier]/+layout.server.ts b/src/routes/publication/[type]/[identifier]/+layout.server.ts index f2c64dc..f97639a 100644 --- a/src/routes/publication/[type]/[identifier]/+layout.server.ts +++ b/src/routes/publication/[type]/[identifier]/+layout.server.ts @@ -1,40 +1,23 @@ import { error } from "@sveltejs/kit"; import type { LayoutServerLoad } from "./$types"; -import { fetchEventByDTag, fetchEventById, fetchEventByNaddr, fetchEventByNevent } from "../../../../lib/utils/websocket_utils.ts"; -import type { NostrEvent } from "../../../../lib/utils/websocket_utils.ts"; export const load: LayoutServerLoad = async ({ params, url }) => { const { type, identifier } = params; - let indexEvent: NostrEvent; - - // Handle different identifier types - switch (type) { - case 'id': - indexEvent = await fetchEventById(identifier); - break; - case 'd': - indexEvent = await fetchEventByDTag(identifier); - break; - case 'naddr': - indexEvent = await fetchEventByNaddr(identifier); - break; - case 'nevent': - indexEvent = await fetchEventByNevent(identifier); - break; - default: - throw error(400, `Unsupported identifier type: ${type}`); + // Validate the identifier type for SSR + const validTypes = ['id', 'd', 'naddr', 'nevent']; + if (!validTypes.includes(type)) { + throw error(400, `Unsupported identifier type: ${type}`); } - // Extract metadata for meta tags - const title = indexEvent.tags.find((tag) => tag[0] === "title")?.[1] || "Alexandria Publication"; - const summary = indexEvent.tags.find((tag) => tag[0] === "summary")?.[1] || - "Alexandria is a digital library, utilizing Nostr events for curated publications and wiki pages."; - const image = indexEvent.tags.find((tag) => tag[0] === "image")?.[1] || "/screenshots/old_books.jpg"; + // Provide basic metadata for SSR - actual fetching will happen on client + const title = "Alexandria Publication"; + const summary = "Alexandria is a digital library, utilizing Nostr events for curated publications and wiki pages."; + const image = "/screenshots/old_books.jpg"; const currentUrl = `${url.origin}${url.pathname}`; return { - indexEvent, + indexEvent: null, // Will be fetched on client side metadata: { title, summary, diff --git a/src/routes/publication/[type]/[identifier]/+layout.ts b/src/routes/publication/[type]/[identifier]/+layout.ts index 77ab0a0..0830e1a 100644 --- a/src/routes/publication/[type]/[identifier]/+layout.ts +++ b/src/routes/publication/[type]/[identifier]/+layout.ts @@ -1 +1,65 @@ -export const ssr = true; +import { error } from "@sveltejs/kit"; +import type { LayoutLoad } from "./$types"; +import { fetchEventByDTag, fetchEventById, fetchEventByNaddr, fetchEventByNevent } from "../../../../lib/utils/websocket_utils.ts"; +import type { NostrEvent } from "../../../../lib/utils/websocket_utils.ts"; +import { browser } from "$app/environment"; + +export const load: LayoutLoad = async ({ params, url }) => { + const { type, identifier } = params; + + // Only fetch on the client side where WebSocket is available + if (!browser) { + // Return basic metadata for SSR + return { + indexEvent: null, + metadata: { + title: "Alexandria Publication", + summary: "Alexandria is a digital library, utilizing Nostr events for curated publications and wiki pages.", + image: "/screenshots/old_books.jpg", + currentUrl: `${url.origin}${url.pathname}`, + }, + }; + } + + let indexEvent: NostrEvent; + + try { + // Handle different identifier types + switch (type) { + case 'id': + indexEvent = await fetchEventById(identifier); + break; + case 'd': + indexEvent = await fetchEventByDTag(identifier); + break; + case 'naddr': + indexEvent = await fetchEventByNaddr(identifier); + break; + case 'nevent': + indexEvent = await fetchEventByNevent(identifier); + break; + default: + throw error(400, `Unsupported identifier type: ${type}`); + } + + // Extract metadata for meta tags + const title = indexEvent.tags.find((tag) => tag[0] === "title")?.[1] || "Alexandria Publication"; + const summary = indexEvent.tags.find((tag) => tag[0] === "summary")?.[1] || + "Alexandria is a digital library, utilizing Nostr events for curated publications and wiki pages."; + const image = indexEvent.tags.find((tag) => tag[0] === "image")?.[1] || "/screenshots/old_books.jpg"; + const currentUrl = `${url.origin}${url.pathname}`; + + return { + indexEvent, + metadata: { + title, + summary, + image, + currentUrl, + }, + }; + } catch (err) { + console.error('Failed to fetch publication:', err); + throw error(404, `Failed to load publication: ${err}`); + } +}; diff --git a/src/routes/publication/[type]/[identifier]/+page.server.ts b/src/routes/publication/[type]/[identifier]/+page.server.ts index b23adcf..5695e77 100644 --- a/src/routes/publication/[type]/[identifier]/+page.server.ts +++ b/src/routes/publication/[type]/[identifier]/+page.server.ts @@ -1,39 +1,18 @@ import { error } from "@sveltejs/kit"; import type { PageServerLoad } from "./$types"; -import { fetchEventByDTag, fetchEventById, fetchEventByNaddr, fetchEventByNevent } from "../../../../lib/utils/websocket_utils.ts"; -import type { NostrEvent } from "../../../../lib/utils/websocket_utils.ts"; export const load: PageServerLoad = async ({ params }) => { const { type, identifier } = params; - let indexEvent: NostrEvent | null; - - // Handle different identifier types - switch (type) { - case 'id': - indexEvent = await fetchEventById(identifier); - break; - case 'd': - indexEvent = await fetchEventByDTag(identifier); - break; - case 'naddr': - indexEvent = await fetchEventByNaddr(identifier); - break; - case 'nevent': - indexEvent = await fetchEventByNevent(identifier); - break; - default: - throw error(400, `Unsupported identifier type: ${type}`); - } - - if (!indexEvent) { - throw error(404, `Event not found for ${type}: ${identifier}`); + // Validate the identifier type for SSR + const validTypes = ['id', 'd', 'naddr', 'nevent']; + if (!validTypes.includes(type)) { + throw error(400, `Unsupported identifier type: ${type}`); } - const publicationType = indexEvent.tags.find((tag) => tag[0] === "type")?.[1] ?? ""; - + // Provide basic data for SSR - actual fetching will happen on client return { - publicationType, - indexEvent, + publicationType: "", // Will be determined on client side + indexEvent: null, // Will be fetched on client side }; }; \ No newline at end of file diff --git a/src/routes/publication/[type]/[identifier]/+page.ts b/src/routes/publication/[type]/[identifier]/+page.ts new file mode 100644 index 0000000..6de9d27 --- /dev/null +++ b/src/routes/publication/[type]/[identifier]/+page.ts @@ -0,0 +1,54 @@ +import { error } from "@sveltejs/kit"; +import type { PageLoad } from "./$types"; +import { fetchEventByDTag, fetchEventById, fetchEventByNaddr, fetchEventByNevent } from "../../../../lib/utils/websocket_utils.ts"; +import type { NostrEvent } from "../../../../lib/utils/websocket_utils.ts"; +import { browser } from "$app/environment"; + +export const load: PageLoad = async ({ params }) => { + const { type, identifier } = params; + + // Only fetch on the client side where WebSocket is available + if (!browser) { + // Return basic data for SSR + return { + publicationType: "", + indexEvent: null, + }; + } + + let indexEvent: NostrEvent; + + try { + // Handle different identifier types + switch (type) { + case 'id': + indexEvent = await fetchEventById(identifier); + break; + case 'd': + indexEvent = await fetchEventByDTag(identifier); + break; + case 'naddr': + indexEvent = await fetchEventByNaddr(identifier); + break; + case 'nevent': + indexEvent = await fetchEventByNevent(identifier); + break; + default: + throw error(400, `Unsupported identifier type: ${type}`); + } + + if (!indexEvent) { + throw error(404, `Event not found for ${type}: ${identifier}`); + } + + const publicationType = indexEvent.tags.find((tag) => tag[0] === "type")?.[1] ?? ""; + + return { + publicationType, + indexEvent, + }; + } catch (err) { + console.error('Failed to fetch publication:', err); + throw error(404, `Failed to load publication: ${err}`); + } +}; \ No newline at end of file