Files
vercel__workflow/docs/hooks/geistdocs/use-version.tsx
Karthik Kalyan ea16d04599 docs: split v4/v5 content trees and fix version switcher end-to-end (#1948)
* docs: split v4/v5 content, fix version switcher end-to-end

## Content restructuring
- Split `docs/content/docs/` into `docs/content/docs/v4/` and
  `docs/content/docs/v5/` so each version is a fully independent
  content tree with no shared-file coupling
- v4 excludes the four pages that are v5-only (AbortController
  cancellation docs and the serializable-abort-controller internal page)
- v5 retains all pages; `preRelease` frontmatter field removed (no
  longer needed now that each version is its own folder)
- Removed `AbortController` / `AbortSignal` from v4 serialization page
  (section moved to v5 only)

## Fumadocs source
- Added `v4docs` and `v5docs` as separate `defineDocs()` collections in
  `source.config.ts`; shared `docsSchema` (no more `preRelease` field)
- `source.ts` exports both `source` (v4, `baseUrl: /docs`) and
  `v5Source` (v5, same base URL)

## Version routing
- `version-source.ts` simplified: `filterPreReleaseFromNodes` and
  `isPreReleaseUrl` logic removed; v4 tree uses `source`, v5 tree uses
  `v5Source` + `rewriteNodeUrls`
- v4 `page.tsx`: removed `preRelease` guard (v4Source has no such pages)
- v5 `page.tsx`: uses `v5Source` for `getPage` / `generateStaticParams`
  / `generateMetadata`; `v5Link` wrapper rewrites `/docs/…` hrefs to
  `/v5/docs/…` so inline MDX links stay in the v5 context

## Versioned cookbook
- Added `app/[lang]/v5/cookbook/` layout + page (mirrors v4 but uses
  `v5Source`, `rewriteCookbookUrlForVersion`, and `V5CookbookLink`)
- `getCookbookTree` accepts a `versionPrefix` parameter; sidebar URLs
  are prefixed accordingly (`/v5/cookbook/…`)
- `cookbook-tree.ts`: added `skipVersions?: string[]` per-recipe field
  for version-specific exclusions; `distributed-abort-controller` is
  marked `skipVersions: ['v5']`

## Version switcher — state & navigation
- New `VersionProvider` context (`hooks/geistdocs/use-version.tsx`)
  backed by `localStorage`: URL is source of truth on versioned pages,
  `localStorage` carries the preference across non-versioned pages
  (cookbook overview, worlds, etc.)
- `VersionSwitcher` uses `useVersion()` context instead of URL-only
  detection; now visible on all pages including cookbook
- `DesktopMenu` and `MobileMenu` use `activeVersion` from context so
  the "Docs" and "Cookbook" navbar links resolve to the correct version
  prefix on every page
- `buildVersionUrl` expanded to handle `/cookbook/…` paths alongside
  `/docs/…`; non-versioned routes (worlds, api) return unchanged
- `switchVersion` does a `HEAD` probe before navigating; falls back to
  the versioned cookbook or docs home if the target page doesn't exist
  in that version (handles v4-only → v5 and v5-only → v4 cases)

## Cookbook content (v5)
- Rewrote `agent-cancellation` recipe using a single `AbortController`
  pattern; removed Hard Cancellation vs Stop Signal two-approach
  comparison
- Deleted `distributed-abort-controller` recipe from v5 (native
  `AbortController` serialization makes it unnecessary)
- Removed references to distributed-abort-controller from
  `cookbook/index.mdx` and `common-patterns/timeouts.mdx`

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(docs): use abortSignal (not signal) in DurableAgent.stream() options

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(docs): update prepack scripts to use versioned content paths

Content moved from docs/content/docs/ to docs/content/docs/v5/ on main
(pre-release channel). Stable branch will use v4/ after backport.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-06 05:25:15 +00:00

118 lines
3.3 KiB
TypeScript

'use client';
import { usePathname, useRouter } from 'next/navigation';
import {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from 'react';
import {
buildVersionUrl,
type DocsVersion,
getVersionFromPathname,
LATEST_VERSION,
VERSIONS,
} from '@/lib/geistdocs/versions';
const STORAGE_KEY = 'workflow-docs-version';
function getStoredVersion(): DocsVersion {
if (typeof window === 'undefined') return LATEST_VERSION;
const stored = localStorage.getItem(STORAGE_KEY);
return VERSIONS.find((v) => v.id === stored) ?? LATEST_VERSION;
}
function persistVersion(version: DocsVersion) {
if (typeof window !== 'undefined') {
localStorage.setItem(STORAGE_KEY, version.id);
}
}
interface VersionContextValue {
activeVersion: DocsVersion;
switchVersion: (target: DocsVersion) => Promise<void>;
}
const VersionContext = createContext<VersionContextValue | null>(null);
export const VersionProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const pathname = usePathname();
const router = useRouter();
// Both /docs and /cookbook routes carry version in the URL.
const isVersionedPage =
pathname.includes('/docs') || pathname.includes('/cookbook');
const urlVersion = isVersionedPage ? getVersionFromPathname(pathname) : null;
// Initialize from localStorage; docs pages override this via urlVersion.
const [storedVersion, setStoredVersion] = useState<DocsVersion>(() =>
getStoredVersion()
);
// On docs pages the URL is the source of truth — sync it to localStorage
// so non-docs pages can pick it up after navigation.
useEffect(() => {
if (urlVersion && urlVersion.id !== storedVersion.id) {
setStoredVersion(urlVersion);
persistVersion(urlVersion);
}
}, [urlVersion?.id]);
const activeVersion = urlVersion ?? storedVersion;
const switchVersion = useCallback(
async (target: DocsVersion) => {
setStoredVersion(target);
persistVersion(target);
if (!isVersionedPage) {
// On worlds and other unversioned pages just update the stored preference.
return;
}
const targetUrl = buildVersionUrl(pathname, target);
// Some pages exist in one version but not the other (e.g. v4-only
// recipes, v5-only docs). Probe the target URL with a HEAD request
// before navigating; if it 404s fall back to the versioned home for
// that section rather than landing on a 404.
try {
const res = await fetch(targetUrl, { method: 'HEAD' });
if (!res.ok) {
const fallback = pathname.includes('/cookbook')
? `${target.prefix}/cookbook`
: `${target.prefix}/docs/getting-started`;
router.push(fallback);
return;
}
} catch {
const fallback = pathname.includes('/cookbook')
? `${target.prefix}/cookbook`
: `${target.prefix}/docs/getting-started`;
router.push(fallback);
return;
}
router.push(targetUrl);
},
[isVersionedPage, pathname, router]
);
return (
<VersionContext.Provider value={{ activeVersion, switchVersion }}>
{children}
</VersionContext.Provider>
);
};
export function useVersion(): VersionContextValue {
const ctx = useContext(VersionContext);
if (!ctx) throw new Error('useVersion must be used inside VersionProvider');
return ctx;
}