The code is the post id through a reversible 32-bit mix in base36: unique, short, and not the raw sequential id. Only the code resolves the post (via the reroute hook), so the slug can change with the title. Feed links, the share button, canonical and og:url use the pretty form, and old /post/<instance>/<id> links are rewritten in the address bar. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011c5BZVwWdVLrdQi9htmuth
21 lines
904 B
TypeScript
21 lines
904 B
TypeScript
import type { Reroute } from '@sveltejs/kit'
|
|
import { SINGLE_COMMUNITY } from '$lib/app/single'
|
|
import { resolvePrettyPath } from '$lib/app/slug'
|
|
|
|
/**
|
|
* Single-community mode: serve the community page for "/" and for every route that would
|
|
* otherwise list or browse communities. The address bar keeps the original path, so the home
|
|
* page stays "/". The community's own sub-pages (e.g. /c/<name>/settings) are left alone.
|
|
*/
|
|
export const reroute: Reroute = ({ url }) => {
|
|
if (!SINGLE_COMMUNITY) return
|
|
const path = url.pathname
|
|
// Pretty post URLs: /p/<code>/<slug> -> /post/<instance>/<id>.
|
|
const post = resolvePrettyPath(path)
|
|
if (post) return post
|
|
const home = `/c/${SINGLE_COMMUNITY}`
|
|
if (path === '/' || path === '/communities' || path.startsWith('/explore')) return home
|
|
const other = path.match(/^\/c\/([^/]+)\/?$/)
|
|
if (other && other[1] !== SINGLE_COMMUNITY) return home
|
|
}
|