Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"@funstack/static": "^0.0.9",
"react": "^19.2.4",
"react-dom": "^19.2.4",
"shiki": "^3.22.0"
"shiki": "^3.22.0",
"urlpattern-polyfill": "^10.1.0"
},
"devDependencies": {
"@types/react": "^19.2.14",
Expand Down
6 changes: 3 additions & 3 deletions packages/docs/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const NotFoundPage = lazy(() =>
})),
);

const routes = [
export const routes = [
route({
component: (
// Note: somehow the Suspense here causes issues with hydration.
Expand Down Expand Up @@ -230,6 +230,6 @@ const routes = [
}),
];

export default function App() {
return <ClientApp routes={routes} />;
export default function App({ ssrPath }: { ssrPath?: string }) {
return <ClientApp routes={routes} ssrPath={ssrPath} />;
}
16 changes: 14 additions & 2 deletions packages/docs/src/ClientApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { useEffect } from "react";
import { Router, type RouteDefinition } from "@funstack/router";
import "./styles.css";

export function ClientApp({ routes }: { routes: RouteDefinition[] }) {
export function ClientApp({
routes,
ssrPath,
}: {
routes: RouteDefinition[];
ssrPath?: string;
}) {
// Auto scroll to top - this should be handled by the browser per spec,
// but currently Chrome and Safari do not follow the spec.
useEffect(() => {
Expand Down Expand Up @@ -32,5 +38,11 @@ export function ClientApp({ routes }: { routes: RouteDefinition[] }) {
};
}, []);

return <Router routes={routes} fallback="static" />;
return (
<Router
routes={routes}
fallback="static"
ssr={ssrPath ? { path: ssrPath } : undefined}
/>
);
}
50 changes: 50 additions & 0 deletions packages/docs/src/entries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import "urlpattern-polyfill";
import type { EntryDefinition } from "@funstack/static/entries";
import type { RouteDefinition } from "@funstack/router";
import { routes } from "./App.js";
import App from "./App.js";

function collectPaths(routeDefs: RouteDefinition[], prefix: string): string[] {
const paths: string[] = [];
for (const r of routeDefs) {
const routePath = r.path;
if (routePath === undefined) {
// Pathless route: recurse with same prefix
if (r.children) {
paths.push(...collectPaths(r.children, prefix));
}
} else if (r.children) {
// Has path and children: recurse with new prefix
paths.push(...collectPaths(r.children, prefix + routePath));
} else {
// Leaf route: collect the full path
const fullPath = routePath === "/" ? prefix || "/" : prefix + routePath;
paths.push(fullPath);
}
}
return paths;
}

function toEntry(path: string): { ssrPath: string; outputPath: string } {
if (path === "/*") {
return { ssrPath: "/__404__", outputPath: "404.html" };
}
if (path === "/") {
return { ssrPath: "/", outputPath: "index.html" };
}
// Remove leading slash for outputPath
const stripped = path.slice(1);
return { ssrPath: path, outputPath: `${stripped}/index.html` };
}

export default function getEntries(): EntryDefinition[] {
const paths = collectPaths(routes, "");
return paths.map((path) => {
const { ssrPath, outputPath } = toEntry(path);
return {
path: outputPath,
root: () => import("./Root.js"),
app: <App ssrPath={ssrPath} />,
};
});
}
3 changes: 1 addition & 2 deletions packages/docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { defineConfig } from "vite";
export default defineConfig({
plugins: [
funstackStatic({
root: "./src/Root.tsx",
app: "./src/App.tsx",
entries: "./src/entries.tsx",
ssr: true,
}),
react(),
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/wrangler-dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ compatibility_date = "2026-02-18"

[assets]
directory = "./dist/public"
not_found_handling = "single-page-application"
not_found_handling = "404-page"
2 changes: 1 addition & 1 deletion packages/docs/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ compatibility_date = "2026-02-18"

[assets]
directory = "./dist/public"
not_found_handling = "single-page-application"
not_found_handling = "404-page"
4 changes: 3 additions & 1 deletion packages/router/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ export function Router({

const routerContextValue = {
locationEntry: locationEntry,
url: locationEntry?.url ?? null,
url:
locationEntry?.url ??
(ssr ? new URL(ssr.path, "http://localhost") : null),
isPending,
navigate,
navigateAsync,
Expand Down
20 changes: 20 additions & 0 deletions packages/router/src/__tests__/fallback.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,26 @@ describe("ssr", () => {
expect(container.textContent).toBe("");
});

it("provides URL via useLocation during SSR with ssr.path", () => {
function Page() {
const location = useLocation();
return (
<div>
<span data-testid="pathname">{location.pathname}</span>
<span data-testid="search">{location.search}</span>
<span data-testid="hash">{location.hash}</span>
</div>
);
}

const routes: RouteDefinition[] = [{ path: "/about", component: Page }];

render(<Router routes={routes} ssr={{ path: "/about" }} />);
expect(screen.getByTestId("pathname").textContent).toBe("/about");
expect(screen.getByTestId("search").textContent).toBe("");
expect(screen.getByTestId("hash").textContent).toBe("");
});

it("pathless route wrapping path-based children works with ssr.path", () => {
const routes: RouteDefinition[] = [
{
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.