velxio/frontend/src/entry-server.tsx

96 lines
3.2 KiB
TypeScript
Raw Normal View History

/**
* SSR entry point for prerendering SEO pages at build time.
*
* Used by scripts/prerender-seo.mjs via Vite's ssrLoadModule.
* Renders each page component to an HTML string so the prerender script
* can inject it into the static dist/index.html per route.
*/
import React from 'react';
import { renderToString } from 'react-dom/server';
import { MemoryRouter } from 'react-router-dom';
import { SEO_ROUTES } from './seoRoutes';
// ── SEO page components ─────────────────────────────────────────────────────
import { exampleProjects } from './data/examples';
import { ExamplesPage } from './pages/ExamplesPage';
import { ExampleDetailPage } from './pages/ExampleDetailPage';
refactor(routes): OSS is the editor; marketing moves to the overlay The open-source build now ships exactly its product surface: /editor, the examples gallery (/examples, /examples/:id) and the per-example editor (/example/:id). Landing, about, pricing, docs, the 14 keyword-targeted simulator landings and the v2/v2.5/v3 showcases move to the private overlay, registered through the same registerProRoutes seam that already carries login/admin/classroom. Root behaves per build: an overlay that registers an index route claims '/' (velxio.dev keeps its landing); otherwise '/' redirects to /editor. The redirect waits for the overlay import to settle — same contract as markProExamplesSettled — so a velxio.dev visitor is never bounced into the editor because the landing was 300ms away from registering. Prerender and sitemap follow the split: entry-server pulls the marketing page map from '@pro/pages/marketing' behind a VITE_PRO_BUILD-gated dynamic import (the proven main.tsx pattern, with an OSS stub for tsc), and generate-sitemap lists only served routes in OSS builds (2 URLs) while pro builds stay byte-identical — verified against a pre-migration baseline: sitemap (37 URLs) and prerendered /, /about, /docs, /arduino-simulator, /esp32-simulator, /v3 all identical modulo hashed asset names. OSS prerender drops exactly the 32 marketing pages (348→316). The OSS header slims down to match: Editor, Examples, GitHub, Discord — the marketing links only render in pro builds, where their routes exist. The editor Help menu links those pages absolutely (velxio.dev) in OSS, exactly like the desktop app's Help menu.
2026-07-30 22:50:14 +07:00
// Map route paths to their React component. The OSS build prerenders only
// what it ships (the examples gallery); the marketing surface lives in the
// pro overlay and is merged in by loadRouteComponents() below.
const OSS_ROUTE_COMPONENTS: Record<string, React.FC> = {
'/examples': ExamplesPage,
};
refactor(routes): OSS is the editor; marketing moves to the overlay The open-source build now ships exactly its product surface: /editor, the examples gallery (/examples, /examples/:id) and the per-example editor (/example/:id). Landing, about, pricing, docs, the 14 keyword-targeted simulator landings and the v2/v2.5/v3 showcases move to the private overlay, registered through the same registerProRoutes seam that already carries login/admin/classroom. Root behaves per build: an overlay that registers an index route claims '/' (velxio.dev keeps its landing); otherwise '/' redirects to /editor. The redirect waits for the overlay import to settle — same contract as markProExamplesSettled — so a velxio.dev visitor is never bounced into the editor because the landing was 300ms away from registering. Prerender and sitemap follow the split: entry-server pulls the marketing page map from '@pro/pages/marketing' behind a VITE_PRO_BUILD-gated dynamic import (the proven main.tsx pattern, with an OSS stub for tsc), and generate-sitemap lists only served routes in OSS builds (2 URLs) while pro builds stay byte-identical — verified against a pre-migration baseline: sitemap (37 URLs) and prerendered /, /about, /docs, /arduino-simulator, /esp32-simulator, /v3 all identical modulo hashed asset names. OSS prerender drops exactly the 32 marketing pages (348→316). The OSS header slims down to match: Editor, Examples, GitHub, Discord — the marketing links only render in pro builds, where their routes exist. The editor Help menu links those pages absolutely (velxio.dev) in OSS, exactly like the desktop app's Help menu.
2026-07-30 22:50:14 +07:00
let routeComponents: Record<string, React.FC> = OSS_ROUTE_COMPONENTS;
/**
* Merge in the overlay's marketing pages (landing, about, docs, the SEO
* simulator landings). Pro-gated dynamic import — the never-taken branch
* keeps the OSS build from ever referencing files it does not have, the
* same pattern main.tsx uses for mountPro. The prerender script awaits
* this before asking for routes.
*/
export async function loadRouteComponents(): Promise<Record<string, React.FC>> {
if (import.meta.env.VITE_PRO_BUILD) {
const m = await import('@pro/pages/marketing');
routeComponents = { ...OSS_ROUTE_COMPONENTS, ...m.MARKETING_ROUTE_COMPONENTS };
}
return routeComponents;
}
/**
* Returns all routes that have both seoMeta and a renderable component.
*/
export function getPrerenderedRoutes() {
refactor(routes): OSS is the editor; marketing moves to the overlay The open-source build now ships exactly its product surface: /editor, the examples gallery (/examples, /examples/:id) and the per-example editor (/example/:id). Landing, about, pricing, docs, the 14 keyword-targeted simulator landings and the v2/v2.5/v3 showcases move to the private overlay, registered through the same registerProRoutes seam that already carries login/admin/classroom. Root behaves per build: an overlay that registers an index route claims '/' (velxio.dev keeps its landing); otherwise '/' redirects to /editor. The redirect waits for the overlay import to settle — same contract as markProExamplesSettled — so a velxio.dev visitor is never bounced into the editor because the landing was 300ms away from registering. Prerender and sitemap follow the split: entry-server pulls the marketing page map from '@pro/pages/marketing' behind a VITE_PRO_BUILD-gated dynamic import (the proven main.tsx pattern, with an OSS stub for tsc), and generate-sitemap lists only served routes in OSS builds (2 URLs) while pro builds stay byte-identical — verified against a pre-migration baseline: sitemap (37 URLs) and prerendered /, /about, /docs, /arduino-simulator, /esp32-simulator, /v3 all identical modulo hashed asset names. OSS prerender drops exactly the 32 marketing pages (348→316). The OSS header slims down to match: Editor, Examples, GitHub, Discord — the marketing links only render in pro builds, where their routes exist. The editor Help menu links those pages absolutely (velxio.dev) in OSS, exactly like the desktop app's Help menu.
2026-07-30 22:50:14 +07:00
return SEO_ROUTES.filter((r) => r.seoMeta && routeComponents[r.path]);
}
/**
* Render a route's page component to an HTML string.
*/
export function render(path: string): string {
refactor(routes): OSS is the editor; marketing moves to the overlay The open-source build now ships exactly its product surface: /editor, the examples gallery (/examples, /examples/:id) and the per-example editor (/example/:id). Landing, about, pricing, docs, the 14 keyword-targeted simulator landings and the v2/v2.5/v3 showcases move to the private overlay, registered through the same registerProRoutes seam that already carries login/admin/classroom. Root behaves per build: an overlay that registers an index route claims '/' (velxio.dev keeps its landing); otherwise '/' redirects to /editor. The redirect waits for the overlay import to settle — same contract as markProExamplesSettled — so a velxio.dev visitor is never bounced into the editor because the landing was 300ms away from registering. Prerender and sitemap follow the split: entry-server pulls the marketing page map from '@pro/pages/marketing' behind a VITE_PRO_BUILD-gated dynamic import (the proven main.tsx pattern, with an OSS stub for tsc), and generate-sitemap lists only served routes in OSS builds (2 URLs) while pro builds stay byte-identical — verified against a pre-migration baseline: sitemap (37 URLs) and prerendered /, /about, /docs, /arduino-simulator, /esp32-simulator, /v3 all identical modulo hashed asset names. OSS prerender drops exactly the 32 marketing pages (348→316). The OSS header slims down to match: Editor, Examples, GitHub, Discord — the marketing links only render in pro builds, where their routes exist. The editor Help menu links those pages absolutely (velxio.dev) in OSS, exactly like the desktop app's Help menu.
2026-07-30 22:50:14 +07:00
const Component = routeComponents[path];
if (!Component) return '';
try {
return renderToString(
<MemoryRouter initialEntries={[path]}>
<Component />
</MemoryRouter>,
);
} catch (err) {
console.warn(` ⚠ SSR render failed for ${path}:`, (err as Error).message);
return '';
}
}
/**
* Returns all example routes to prerender, one per example project.
*/
export function getPrerenderedExampleRoutes() {
return exampleProjects.map((e) => ({
path: `/examples/${e.id}`,
title: `${e.title} — Free Arduino Simulator Example | Velxio`,
description: `${e.description}. Run this example free in your browser — no install, no account required.`,
url: `https://velxio.dev/examples/${e.id}`,
}));
}
/**
* Render an example detail page to an HTML string.
*/
export function renderExample(exampleId: string): string {
try {
return renderToString(
<MemoryRouter initialEntries={[`/examples/${exampleId}`]}>
<ExampleDetailPage />
</MemoryRouter>,
);
} catch (err) {
console.warn(` ⚠ SSR render failed for /examples/${exampleId}:`, (err as Error).message);
return '';
}
}