Browse Source

Skip prerendering page layout children on server

master
buttercat1791 7 months ago
parent
commit
884d04a45e
  1. 35
      src/routes/publication/[type]/[identifier]/+layout.server.ts
  2. 5
      src/routes/publication/[type]/[identifier]/+layout.svelte
  3. 66
      src/routes/publication/[type]/[identifier]/+layout.ts
  4. 18
      src/routes/publication/[type]/[identifier]/+page.server.ts
  5. 69
      src/routes/publication/[type]/[identifier]/+page.ts

35
src/routes/publication/[type]/[identifier]/+layout.server.ts

@ -1,23 +1,40 @@
import { error } from "@sveltejs/kit"; import { error } from "@sveltejs/kit";
import type { LayoutServerLoad } from "./$types"; 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 }) => { export const load: LayoutServerLoad = async ({ params, url }) => {
const { type, identifier } = params; const { type, identifier } = params;
// Validate the identifier type for SSR let indexEvent: NostrEvent;
const validTypes = ['id', 'd', 'naddr', 'nevent'];
if (!validTypes.includes(type)) { // Handle different identifier types
error(400, `Unsupported identifier type: ${type}`); 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:
error(400, `Unsupported identifier type: ${type}`);
} }
// Provide basic metadata for SSR - actual fetching will happen on client // Extract metadata for meta tags
const title = "Alexandria Publication"; const title = indexEvent.tags.find((tag) => tag[0] === "title")?.[1] || "Alexandria Publication";
const summary = "Alexandria is a digital library, utilizing Nostr events for curated publications and wiki pages."; const summary = indexEvent.tags.find((tag) => tag[0] === "summary")?.[1] ||
const image = "/screenshots/old_books.jpg"; "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}`; const currentUrl = `${url.origin}${url.pathname}`;
return { return {
indexEvent: null, // Will be fetched on client side indexEvent,
metadata: { metadata: {
title, title,
summary, summary,

5
src/routes/publication/[type]/[identifier]/+layout.svelte

@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import { browser } from "$app/environment";
import type { LayoutProps } from "./$types"; import type { LayoutProps } from "./$types";
let { data, children }: LayoutProps = $props(); let { data, children }: LayoutProps = $props();
@ -26,4 +27,6 @@
<meta name="twitter:image" content={metadata.image} /> <meta name="twitter:image" content={metadata.image} />
</svelte:head> </svelte:head>
{@render children()} {#if browser}
{@render children()}
{/if}

66
src/routes/publication/[type]/[identifier]/+layout.ts

@ -1,67 +1 @@
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 ssr = true; export const ssr = true;
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:
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);
error(404, `Failed to load publication: ${err}`);
}
};

18
src/routes/publication/[type]/[identifier]/+page.server.ts

@ -1,18 +0,0 @@
import { error } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async ({ params }) => {
const { type, identifier } = params;
// Validate the identifier type for SSR
const validTypes = ['id', 'd', 'naddr', 'nevent'];
if (!validTypes.includes(type)) {
error(400, `Unsupported identifier type: ${type}`);
}
// Provide basic data for SSR - actual fetching will happen on client
return {
publicationType: "", // Will be determined on client side
indexEvent: null, // Will be fetched on client side
};
};

69
src/routes/publication/[type]/[identifier]/+page.ts

@ -2,53 +2,38 @@ import { error } from "@sveltejs/kit";
import type { PageLoad } from "./$types"; import type { PageLoad } from "./$types";
import { fetchEventByDTag, fetchEventById, fetchEventByNaddr, fetchEventByNevent } from "../../../../lib/utils/websocket_utils.ts"; import { fetchEventByDTag, fetchEventById, fetchEventByNaddr, fetchEventByNevent } from "../../../../lib/utils/websocket_utils.ts";
import type { NostrEvent } 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 }) => { export const load: PageLoad = async ({ params }) => {
const { type, identifier } = params; const { type, identifier } = params;
// Only fetch on the client side where WebSocket is available let indexEvent: NostrEvent | null;
if (!browser) {
// Return basic data for SSR
return {
publicationType: "",
indexEvent: null,
};
}
let indexEvent: NostrEvent;
try { // Handle different identifier types
// Handle different identifier types switch (type) {
switch (type) { case 'id':
case 'id': indexEvent = await fetchEventById(identifier);
indexEvent = await fetchEventById(identifier); break;
break; case 'd':
case 'd': indexEvent = await fetchEventByDTag(identifier);
indexEvent = await fetchEventByDTag(identifier); break;
break; case 'naddr':
case 'naddr': indexEvent = await fetchEventByNaddr(identifier);
indexEvent = await fetchEventByNaddr(identifier); break;
break; case 'nevent':
case 'nevent': indexEvent = await fetchEventByNevent(identifier);
indexEvent = await fetchEventByNevent(identifier); break;
break; default:
default: error(400, `Unsupported identifier type: ${type}`);
error(400, `Unsupported identifier type: ${type}`); }
}
if (!indexEvent) { if (!indexEvent) {
error(404, `Event not found for ${type}: ${identifier}`); error(404, `Event not found for ${type}: ${identifier}`);
} }
const publicationType = indexEvent.tags.find((tag) => tag[0] === "type")?.[1] ?? ""; const publicationType = indexEvent.tags.find((tag) => tag[0] === "type")?.[1] ?? "";
return { return {
publicationType, publicationType,
indexEvent, indexEvent,
}; };
} catch (err) { };
console.error('Failed to fetch publication:', err);
error(404, `Failed to load publication: ${err}`);
}
};

Loading…
Cancel
Save