agentleFS
Jeff-TianJeff-Tian/agent-skills/cohosted-frontend-backend/SKILL.md

cohosted-frontend-backend

Skill1 starsChanged 7 months agoMITView on GitHub

Co-host a frontend SPA and backend API in the same server process or container — separate codebases in development, single entry point in production. Use when integrating React/Next.js/Umi frontends with Node.js (Fastify/Egg.js) or ASP.NET Core backends. Trigger on keywords like "co-hosted frontend backend", "single container deployment", "SPA + API same server", "add frontend to backend project", "integrate Next.js with .NET", "static export", "SSR takeover", etc.

  • Reads credentials
  • Installs packages

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 3 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before implementation, choose the right approach for your project:

    line 38
  2. Distinctive:

    Create a frontend subdirectory inside the backend project:

    line 90
  3. Very specific:

    Use the project's unified package manager (pnpm/yarn/npm)

    line 109
  4. Very specific:

    If using static export, set output: 'export' and trailingSlash: true

    line 113
  5. Very specific:

    Only put public information in NEXT_PUBLIC_ prefixed environment variables

    line 213
  6. Distinctive:

    If the backend has CSRF protection, ensure the frontend requests carry the correct token

    line 215
  7. Very specific:

    When co-hosting a Next.js frontend with Duende IdentityServer, there are additional constraints beyond the generic ASP.NET Core case:

    line 241

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Separate Development, Co-Hosted Deployment

Overview

The frontend and backend run in the same process (or the same container), with requests routed by URL prefix or route priority. Each side iterates independently during development and ships as a single artifact for deployment.

When to Use

  • The project needs a frontend UI but you don't want to maintain two deployment pipelines
  • Solo or small-team project where you want to simplify operations
  • You already have a backend API and need to add frontend pages under the same domain (avoiding CORS)
  • You need SSR for the first paint or a skeleton screen, then let the client-side SPA take over

Core Architecture Pattern

Browser Request
    │
    ▼
Single Server Process (Node.js or ASP.NET Core)
    │
    ├─ /api/*              → Backend API routes (matched first)
    ├─ /graphql             → GraphQL endpoint (if any)
    ├─ /_next/* or static   → Frontend static assets (CSS/JS/images)
    ├─ /portal/* or /next/* → Frontend page routes
    └─ fallback             → Serve frontend index.html (SPA route catch-all)

Key principle: backend routes always take priority over the frontend fallback.

Decision Tree: Choosing an Integration Approach

Before implementation, choose the right approach for your project:

1. What is the backend framework?

Node.js (Fastify/Express/Egg.js/Koa)
  → Option A: Plugin integration (recommended)
  → Option B: Serve build output + 404 Fallback

ASP.NET Core
  → Option C: Static export + MapFallback (recommended)
  → Option D: Reverse proxy to a standalone Next.js process (development convenience)

Other (Go/Java/Python)
  → The approach from Option C is universally applicable

2. Does the frontend need SSR?

No (most scenarios)
  → Use Next.js output: 'export' for static export
  → Zero runtime dependencies; the backend only needs to serve static files

Yes — SSR for the first paint + client-side takeover
  → Node.js: Use @fastify/nextjs or Egg.js view templates
  → .NET: Standalone Next.js process + YARP reverse proxy

Server-rendered skeleton + SPA replacement
  → Use Pug/EJS/Razor to render an HTML skeleton
  → Inject the frontend build output's <script> and <link> tags into the HTML
  → After frontend loads, mount to #root and replace the skeleton content

3. Frontend routing strategy

basePath isolation (recommended starting point)
  → Frontend lives under /next, /portal, /app, etc.
  → No conflict with existing backend routes
  → Allows gradual migration; remove the prefix later

Root path takeover (end goal)
  → Frontend handles all unmatched routes
  → Backend API distinguished by /api/* prefix
  → Must ensure the fallback has the lowest priority

Implementation Steps (General Flow)

Step 1: Initialize the Frontend Project

Create a frontend subdirectory inside the backend project:

# Node.js project
project-root/
  ├── app.js (or server.js)
  ├── pages/          ← Next.js pages (if using plugin integration)
  └── client-app/     ← Standalone frontend project (if static export)

# ASP.NET Core project
src/Web/
  ├── Program.cs
  ├── Pages/          ← Razor Pages (keep as-is)
  ├── wwwroot/        ← Static assets root
  │   └── portal/     ← Frontend build output directory
  └── ClientApp/      ← Next.js source code

Frontend project initialization checklist:

  • Use the project's unified package manager (pnpm/yarn/npm)
  • Configure TypeScript
  • Configure Tailwind CSS or the project's existing styling solution
  • Set basePath to match the URL prefix agreed upon with the backend
  • If using static export, set output: 'export' and trailingSlash: true

Step 2: Configure Backend Routing

Principle: when adding frontend support, only add a fallback at the end of the routing pipeline — do not modify existing routes.

Node.js serving static files + fallback:

// Register static file middleware (pointing to the build output directory)
fastify.register(require('@fastify/static'), {
  root: path.join(__dirname, 'dist'),
  prefix: '/portal/',
})

// Fallback unmatched routes to index.html
fastify.setNotFoundHandler((req, reply) => {
  if (req.url.startsWith('/portal')) {
    reply.sendFile('index.html', path.join(__dirname, 'dist'))
  }
})

ASP.NET Core serving static files + fallback:

app.UseStaticFiles(); // Already present, no changes needed

// Add fallback after all routes
app.MapFallback("/portal/{**slug}", async context => {
    context.Response.ContentType = "text/html";
    await context.Response.SendFileAsync(
        Path.Combine(app.Environment.WebRootPath, "portal", "index.html"));
});

Step 3: Update Build & Docker

Multi-stage Docker build template:

# ---- Frontend Build ----
FROM node:22-alpine AS frontend
WORKDIR /app
COPY path/to/ClientApp/ ./
RUN corepack enable && corepack prepare pnpm@latest --activate
RUN pnpm install --frozen-lockfile && pnpm build

# ---- Backend Build ----
FROM <backend-base-image> AS backend
# ... existing backend build steps ...
# Copy frontend output to the static assets directory before publishing
COPY --from=frontend /app/out/ <static-assets-dir>/

# ---- Runtime ----
FROM <runtime-base-image>
# Final image has no Node.js — only the backend runtime + static files

Step 4: Configure the Development Experience

  • Frontend pnpm dev runs independently on port 3000
  • Backend dotnet run or node app.js runs on its own port
  • During frontend development, point to the backend API via environment variables (or configure a dev proxy)
  • In CI/CD, build the frontend first, then the backend

Step 5: Update .gitignore

# Frontend build output (generated during CI/Docker builds)
ClientApp/node_modules/
ClientApp/.next/
ClientApp/out/
wwwroot/portal/    # or the corresponding output directory

SSR Skeleton → SPA Takeover Pattern (Detailed)

This is a special pattern where the server first renders an HTML skeleton so users see content immediately, then the frontend JS takes over the entire page once loaded.

How it works:

1. Server renders HTML using a template engine (Pug/EJS/Razor)
2. HTML contains <div id="root">skeleton content</div>
3. Frontend build output's <script src="app.js"> is injected at the bottom
4. Browser:
   a. Immediately renders skeleton HTML (user sees the first paint; very fast FCP)
   b. Downloads and executes app.js
   c. React/Vue mounts to #root, replacing the skeleton content
   d. Client-side routing takes over; subsequent navigations don't hit the server

Difference from Next.js SSR hydration:

  • Hydration: the server renders the exact same React component tree as the client; the client "activates" event listeners without replacing the DOM
  • Skeleton replacement: the server renders simple placeholder HTML (may not contain React components); the client replaces it entirely

Skeleton replacement is simpler but can cause a flash; suitable for admin dashboards with low SEO requirements. Hydration has no flash but is more complex to implement.

Security Considerations

  • Frontend static assets must not contain sensitive information (API keys, database connection strings)
  • Only put public information in NEXT_PUBLIC_ prefixed environment variables
  • API authentication is handled by backend route middleware — do not rely on the frontend
  • If the backend has CSRF protection, ensure the frontend requests carry the correct token
  • Frontend basePath routes must not bypass the backend's authentication middleware
  • The static file directory must not expose .env, node_modules, or other sensitive content

Reference Implementations

The following are verified real-world implementations covering different tech stacks. Read the corresponding reference files when you need specific code examples:

  • references/node-fastify-nextjs.md — Fastify + Next.js plugin integration (Jeff-Tian/mp)
  • references/node-eggjs-umi.md — Egg.js + Umi/React skeleton replacement pattern (Jeff-Tian/alpha)
  • references/dotnet-nextjs-static-export.md — ASP.NET Core + Next.js static export (Jeff-Tian/Leg-Godt)
  • references/dotnet-nextjs-identity-server.md — ASP.NET Core IdentityServer + Next.js static export (Duende IdentityServer based solution)

Common Pitfalls

  1. Route conflicts: The frontend fallback overrides API 404 responses. Fix: limit the fallback to a specific prefix or check the Accept header.
  2. Missing basePath: Frontend internal links don't use basePath, causing 404s for assets. Fix: Next.js's basePath config handles this automatically.
  3. Wrong Docker build order: Frontend output not copied to the backend's static directory before packaging. Fix: ensure COPY --from=frontend happens before publish in the Dockerfile.
  4. Dev environment inconsistency: Frontend dev server and backend port conflict. Fix: pin the frontend to port 3000, backend to a different port.
  5. Caching issues: Browser still uses old version after frontend update. Fix: Next.js uses content-hashed filenames by default; make sure you don't set an overly long Cache-Control.
  6. output: 'export' missing → Docker COPY fails: Without output: 'export' in next.config.*, next build outputs to .next/ (Node.js runtime), NOT out/. Docker's COPY --from=frontend /app/out/ will fail with "/app/out": not found. Always set output: 'export' when the backend will serve the static files.
  7. trailingSlash: true recommended with static export: Next.js static export with trailingSlash: true generates index.html in every route directory (e.g., out/about/index.html for /about), which works correctly with UseStaticFiles() and MapFallback. Without it, some route patterns may 404.
  8. images: { unoptimized: true } required: Static export does not support Next.js image optimization (which requires a Node.js runtime). Must set this or images will fail at build time.

IdentityServer-Specific Considerations

When co-hosting a Next.js frontend with Duende IdentityServer, there are additional constraints beyond the generic ASP.NET Core case:

  • IdentityServer Razor Pages are protocol-critical: Pages under /Account/*, /Login/*, /Consent/*, /Device/*, /ExternalLogin/*, /CIBA/* participate in the OAuth2/OIDC protocol flow. They must NOT be replaced, overridden, or interfered with by any frontend fallback.
  • Cannot use a global MapFallback: A root-level fallback (app.MapFallback(...)) would intercept requests meant for IdentityServer endpoints. The fallback must be scoped to a specific prefix (e.g., /pcap/{**slug}).
  • Cannot install SpaProxy: The SpaProxy middleware would hijack non-API requests and redirect them to the dev server, breaking IdentityServer's protocol flow.
  • basePath must be a non-conflicting prefix: Choose a prefix that does not overlap with IdentityServer's reserved paths (/.well-known/*, /connect/*, /Account/*, etc.). Examples: /pcap, /portal, /admin-portal.
  • Authentication for admin pages: Pages under the frontend prefix must be protected by backend authentication policies (e.g., .RequireAuthorization("AdminPolicy")), not just frontend route guards.

IdentityServer Docker Pattern

# ---- Stage 1: Next.js Build ----
FROM node:22-alpine AS frontend
WORKDIR /app
COPY hosts/main/ClientApp/ ./
RUN corepack enable && corepack prepare pnpm@latest --activate
RUN pnpm install --frozen-lockfile
RUN pnpm build
# Output in /app/out/ (requires output: 'export' in next.config.ts)

# ---- Stage 2: .NET Build ----
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .

# Copy frontend output to wwwroot/pcap/ before .NET publish
COPY --from=frontend /app/out/ hosts/main/wwwroot/pcap/

RUN dotnet restore "hosts/main/Host.Main.csproj"
RUN dotnet publish "hosts/main/Host.Main.csproj" -c Release -o /app/publish --no-restore

# ---- Stage 3: Runtime ----
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
# No Node.js runtime needed — only .NET + static files

Language Policy

  • Detect user language
  • Respond in same language

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.