Komplet guide til at bygge en Next.js App Router-site med @webhouse/cms — fra indholdsindlæsning til deployment.
Oversigt
Denne guide gennemgår opbygningen af et komplet Next.js-site med @webhouse/cms. CMS'et håndterer indholdslagring og redigering. Next.js håndterer rendering og routing.
Trin 1: Projektopsætning
# Scaffold a new project
npm create @webhouse/cms my-site
# Or with npx
npx create-@webhouse/cms my-site
# With a specific template
npm create @webhouse/cms my-site -- --template nextjsEller start fra Next.js-boilerplaten:
npm create @webhouse/cms my-site -- --template nextjsTrin 2: Indholdslag
Indholdslaget læser JSON-filer ved build/request-tid:
import { readFileSync, readdirSync, existsSync } from 'fs';
import { join } from 'path';
const CONTENT = join(process.cwd(), 'content');
export function getCollection(name: string) {
const dir = join(CONTENT, name);
if (!existsSync(dir)) return [];
return readdirSync(dir)
.filter(f => f.endsWith('.json'))
.map(f => JSON.parse(readFileSync(join(dir, f), 'utf-8')))
.filter(d => d.status === 'published');
}
export function getDocument(collection: string, slug: string) {
const file = join(CONTENT, collection, `${slug}.json`);
if (!existsSync(file)) return null;
return JSON.parse(readFileSync(file, 'utf-8'));
}Trin 3: Root layout
Root layout læser globale indstillinger og renderer navbar + footer.
Trin 4: Forside med blokke
Forsiden læser content/pages/home.json og renderer blokke.
Trin 5: Blog med statisk generering
// app/blog/page.tsx
import { getCollection } from '@/lib/content';
export default function BlogPage() {
const posts = getCollection('posts')
.sort((a, b) => (b.data.date ?? '').localeCompare(a.data.date ?? ''));
return (
<div>
<h1>Blog</h1>
{posts.map(post => (
<a key={post.slug} href={`/blog/${post.slug}`}>
<h2>{post.data.title}</h2>
<p>{post.data.excerpt}</p>
</a>
))}
</div>
);
}Individuelle indlæg med SEO:
// app/blog/[slug]/page.tsx
import { getCollection, getDocument } from '@/lib/content';
import { notFound } from 'next/navigation';
export function generateStaticParams() {
return getCollection('posts').map(d => ({ slug: d.slug }));
}
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
const doc = getDocument('posts', slug);
if (!doc) return {};
return {
title: doc.data._seo?.metaTitle ?? doc.data.title,
description: doc.data._seo?.metaDescription ?? doc.data.excerpt,
};
}
export default async function PostPage({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
const doc = getDocument('posts', slug);
if (!doc) notFound();
return (
<article>
<h1>{doc.data.title}</h1>
{/* Render doc.data.content with react-markdown */}
</article>
);
}Trin 6: Richtext-rendering
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
function ArticleBody({ content }: { content: string }) {
return (
<ReactMarkdown remarkPlugins={[remarkGfm]}>
{content}
</ReactMarkdown>
);
}Brug aldrig
dangerouslySetInnerHTMLmed regex-baserede parsere.
Trin 7: Blok-rendering
function BlockRenderer({ block }: { block: any }) {
switch (block._block) {
case 'hero':
return (
<section>
<h1>{block.tagline}</h1>
<p>{block.description}</p>
</section>
);
case 'features':
return (
<section>
<h2>{block.title}</h2>
<div className="grid grid-cols-3 gap-4">
{block.items?.map((item: any, i: number) => (
<div key={i}>
<h3>{item.title}</h3>
<p>{item.description}</p>
</div>
))}
</div>
</section>
);
default:
return null;
}
}Trin 8: SEO-metadata
export async function generateMetadata({ params }) {
const doc = getDocument('posts', (await params).slug);
const seo = doc?.data._seo ?? {};
return {
title: seo.metaTitle ?? doc?.data.title,
description: seo.metaDescription ?? doc?.data.excerpt,
openGraph: {
title: seo.metaTitle ?? doc?.data.title,
description: seo.metaDescription,
images: seo.ogImage ? [seo.ogImage] : [],
},
};
}Trin 9: i18n (valgfrit)
export default defineConfig({
defaultLocale: 'en',
locales: ['en', 'da'],
collections: [
defineCollection({
name: 'posts',
sourceLocale: 'en',
locales: ['en', 'da'],
translatable: true,
fields: [
{ name: 'title', type: 'text', required: true },
{ name: 'content', type: 'richtext' },
],
}),
],
});Trin 10: Deployment
FROM node:22-alpine AS builder
WORKDIR /app
COPY . .
RUN npm ci && npm run build
FROM node:22-alpine
WORKDIR /app
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
COPY --from=builder /app/content ./content
CMD ["node", "server.js"]Vigtige mønstre
- Server Components som standard — al indholdsindlæsning sker server-side
"use client"kun hvor nødvendigt — tema-toggle, søgning, markdowngenerateStaticParams— prægenerér alle sider ved build-tidgenerateMetadata— SEO fra CMS_seo-felter med fallbacks- Hardkod aldrig indhold — alt fra CMS JSON-filer
- Filtrér på published — tjek altid
status === "published"