Token导航 LogoToken导航TokenDH.com
研究检索操作浏览器github未标认证来源可访问许可证需确认审计通过

ssr-nextjsSSR Next.js 搜索

Agent Skill

用于辅助前端页面、组件、样式和交互逻辑的开发与维护。它适合让 Agent 生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码,整理组件结构,或定位布局和性能问题。使用时需要结合项目现有设计系统、路由和构建方式,避免只生成孤立片段;涉及页面改动时,应配合本地预览和构建检查确认视觉效果。

总安装

212

周安装

9

GitHub Stars

11

下载量

74
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:ssr-nextjs(SSR Next.js 搜索)
来源仓库:https://github.com/lobbi-docs/claude
仓库路径:skills/ssr-nextjs
安装命令:
npx skills add https://github.com/lobbi-docs/claude --skill ssr-nextjs
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/lobbi-docs/claude --skill ssr-nextjs

简介

用于辅助前端页面、组件、样式和交互逻辑的开发与维护。它适合让 Agent 生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码,整理组件结构,或定位布局和性能问题。

  • 使用时需要结合项目现有设计系统、路由和构建方式,避免只生成孤立片段;涉及页面改动时,应配合本地预览和构建检查确认视觉效果。
  • 可结合来源仓库、安装命令和原始 README 继续核验具体用法。
  • npx skills add https://github.com/lobbi-docs/claude --skill ssr-nextjs
  • https://github.com/lobbi-docs/claude/tree/main/skills/ssr-nextjs

SKILL.md

MUI SSR with Next.js

1. Next.js App Router Setup (v13+)

The App Router requires a client-side ThemeRegistry component that flushes Emotion's server-generated styles into the document head via useServerInsertedHTML.

ThemeRegistry client component

// src/components/ThemeRegistry/EmotionCache.tsx
'use client';

import * as React from 'react';
import createCache from '@emotion/cache';
import { useServerInsertedHTML } from 'next/navigation';
import { CacheProvider } from '@emotion/react';

export default function NextAppDirEmotionCacheProvider(
  props: { options: Parameters<typeof createCache>[0]; children: React.ReactNode }
) {
  const { options, children } = props;

  const [registry] = React.useState(() => {
    const cache = createCache(options);
    cache.compat = true;
    const prevInsert = cache.insert;
    let inserted: { name: string; isGlobal: boolean }[] = [];

    cache.insert = (...args) => {
      const [selector, serialized] = args;
      if (cache.inserted[serialized.name] === undefined) {
        inserted.push({
          name: serialized.name,
          isGlobal: !selector,
        });
      }
      return prevInsert(...args);
    };

    return { cache, flush: () => { const prev = inserted; inserted = []; return prev; } };
  });

  useServerInsertedHTML(() => {
    const names = registry.flush();
    if (names.length === 0) return null;

    let styles = '';
    let dataEmotionAttribute = registry.cache.key;
    const globals: { name: string; style: string }[] = [];

    for (const { name, isGlobal } of names) {
      const style = registry.cache.inserted[name];
      if (typeof style === 'string') {
        if (isGlobal) {
          globals.push({ name, style });
        } else {
          styles += style;
          dataEmotionAttribute += ` ${name}`;
        }
      }
    }

    return (
      <>
        {globals.map(({ name, style }) => (
          <style
            key={name}
            data-emotion={`${registry.cache.key}-global`}
            dangerouslySetInnerHTML={{ __html: style }}
          />
        ))}
        {styles && (
          <style
            data-emotion={dataEmotionAttribute}
            dangerouslySetInnerHTML={{ __html: styles }}
          />
        )}
      </>
    );
  });

  return <CacheProvider value={registry.cache}>{children}</CacheProvider>;
}

ThemeRegistry wrapper

// src/components/ThemeRegistry/ThemeRegistry.tsx
'use client';

import * as React from 'react';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import NextAppDirEmotionCacheProvider from './EmotionCache';
import theme from './theme';

export default function ThemeRegistry({ children }: { children: React.ReactNode }) {
  return (
    <NextAppDirEmotionCacheProvider options={{ key: 'mui', prepend: true }}>
      <ThemeProvider theme={theme}>
        <CssBaseline />
        {children}
      </ThemeProvider>
    </NextAppDirEmotionCacheProvider>
  );
}

app/layout.tsx integration

// app/layout.tsx
import ThemeRegistry from '@/components/ThemeRegistry/ThemeRegistry';

export const metadata = {
  title: 'My App',
  description: 'MUI + Next.js App Router',
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <ThemeRegistry>{children}</ThemeRegistry>
      </body>
    </html>
  );
}

Required dependencies

npm install @mui/material @emotion/react @emotion/styled @emotion/cache

2. Next.js Pages Router Setup

The Pages Router uses _document.tsx to extract critical CSS at render time and inject it into the initial HTML response.

createEmotionCache utility

// src/createEmotionCache.ts
import createCache from '@emotion/cache';

export default function createEmotionCache() {
  return createCache({ key: 'css', prepend: true });
}

_app.tsx with CacheProvider

// pages/_app.tsx
import * as React from 'react';
import Head from 'next/head';
import { AppProps } from 'next/app';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { CacheProvider, EmotionCache } from '@emotion/react';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();

interface MyAppProps extends AppProps {
  emotionCache?: EmotionCache;
}

export default function MyApp(props: MyAppProps) {
  const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
  return (
    <CacheProvider value={emotionCache}>
      <Head>
        <meta name="viewport" content="initial-scale=1, width=device-width" />
      </Head>
      <ThemeProvider theme={theme}>
        <CssBaseline />
        <Component {...pageProps} />
      </ThemeProvider>
    </CacheProvider>
  );
}

_document.tsx with extractCriticalToChunks

// pages/_document.tsx
import * as React from 'react';
import Document, {
  Html,
  Head,
  Main,
  NextScript,
  DocumentProps,
  DocumentContext,
} from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import { AppType } from 'next/app';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

interface MyDocumentProps extends DocumentProps {
  emotionStyleTags: React.ReactElement[];
}

export default function MyDocument({ emotionStyleTags }: MyDocumentProps) {
  return (
    <Html lang="en">
      <Head>
        <meta name="theme-color" content={theme.palette.primary.main} />
        <link rel="shortcut icon" href="/favicon.ico" />
        {emotionStyleTags}
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}

MyDocument.getInitialProps = async (ctx: DocumentContext) => {
  const originalRenderPage = ctx.renderPage;
  const cache = createEmotionCache();
  const { extractCriticalToChunks } = createEmotionServer(cache);

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App: React.ComponentType<React.ComponentProps<AppType> & { emotionCache: ReturnType<typeof createEmotionCache> }>) =>
        function EnhanceApp(props) {
          return <App emotionCache={cache} {...props} />;
        },
    });

  const initialProps = await Document.getInitialProps(ctx);
  const emotionStyles = extractCriticalToChunks(initialProps.html);
  const emotionStyleTags = emotionStyles.styles.map((style) => (
    <style
      data-emotion={`${style.key} ${style.ids.join(' ')}`}
      key={style.key}
      dangerouslySetInnerHTML={{ __html: style.css }}
    />
  ));

  return {
    ...initialProps,
    emotionStyleTags,
  };
};

Additional dependency for Pages Router

npm install @emotion/server

3. Server Components Compatibility

MUI components do NOT work in React Server Components

Every MUI component uses React context (ThemeProvider), hooks (useTheme, useState), or event handlers. None of them can be rendered as RSC. Any file importing from @mui/material must include 'use client' at the top, or be imported from a file that does.

Client boundary patterns

Pattern 1: Thin client wrapper around server data

// app/users/page.tsx (Server Component — fetches data)
import UserTable from './UserTable';

export default async function UsersPage() {
  const users = await db.user.findMany(); // server-side data fetch
  return <UserTable users={users} />;     // pass plain data to client
}
// app/users/UserTable.tsx (Client Component — renders MUI)
'use client';

import {
  Table, TableBody, TableCell, TableContainer,
  TableHead, TableRow, Paper
} from '@mui/material';

interface User { id: string; name: string; email: string; }

export default function UserTable({ users }: { users: User[] }) {
  return (
    <TableContainer component={Paper}>
      <Table>
        <TableHead>
          <TableRow>
            <TableCell>Name</TableCell>
            <TableCell>Email</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {users.map((user) => (
            <TableRow key={user.id}>
              <TableCell>{user.name}</TableCell>
              <TableCell>{user.email}</TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </TableContainer>
  );
}

Pattern 2: Client island for interactive sections only

// app/dashboard/page.tsx (Server Component)
import DashboardStats from './DashboardStats';  // server-rendered plain HTML
import DashboardCharts from './DashboardCharts'; // 'use client' — interactive

export default async function DashboardPage() {
  const stats = await fetchStats();
  const chartData = await fetchChartData();

  return (
    <div>
      {/* Server-rendered: zero JS shipped for this section */}
      <DashboardStats stats={stats} />

      {/* Client boundary: MUI charts with interactivity */}
      <DashboardCharts data={chartData} />
    </div>
  );
}

Pattern 3: Re-export barrel for 'use client' boundary

// src/components/mui.tsx
'use client';

// Single 'use client' boundary for all MUI re-exports used across the app.
// Keeps individual page components clean.
export {
  Button,
  TextField,
  Dialog,
  DialogTitle,
  DialogContent,
  DialogActions,
  AppBar,
  Toolbar,
  Typography,
  Box,
  Container,
  Stack,
} from '@mui/material';

Where to place 'use client'

Push the 'use client' boundary as far down the component tree as possible. Server components at the top fetch data, client components at the leaves render UI.

app/
  layout.tsx          ← Server (ThemeRegistry is a 'use client' child)
  page.tsx            ← Server (fetches data, passes to client children)
  components/
    Header.tsx        ← 'use client' (uses MUI AppBar, needs onClick)
    Footer.tsx        ← Server (plain HTML, no MUI needed)
    DataTable.tsx     ← 'use client' (uses MUI DataGrid)

4. CSS Variables Mode for SSR (FOUC Prevention)

MUI v6+ supports CSS variables mode via cssVariables: true or Experimental_CssVarsProvider in v5. This eliminates the flash of unstyled content (FOUC) on page load because the color scheme is applied via a synchronous script before React hydrates.

Setup with getInitColorSchemeScript

// app/layout.tsx
import { getInitColorSchemeScript } from '@mui/material/styles';
import ThemeRegistry from '@/components/ThemeRegistry/ThemeRegistry';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body>
        {/* This script runs synchronously before React hydrates.
            It reads localStorage/system preference and sets a data attribute
            on <html> so styles apply immediately — no FOUC. */}
        {getInitColorSchemeScript({ defaultMode: 'system' })}
        <ThemeRegistry>{children}</ThemeRegistry>
      </body>
    </html>
  );
}

Theme configuration for CSS variables mode

// src/theme.ts
import { createTheme } from '@mui/material/styles';

const theme = createTheme({
  cssVariables: {
    colorSchemeSelector: 'data-mui-color-scheme',
    // or 'class' to use className-based toggling
    // or 'media' to follow prefers-color-scheme only
  },
  colorSchemes: {
    light: {
      palette: {
        primary: { main: '#1976d2' },
        background: { default: '#fafafa', paper: '#fff' },
      },
    },
    dark: {
      palette: {
        primary: { main: '#90caf9' },
        background: { default: '#121212', paper: '#1e1e1e' },
      },
    },
  },
});

export default theme;

Toggle color scheme at runtime

'use client';

import { useColorScheme } from '@mui/material/styles';
import IconButton from '@mui/material/IconButton';
import Brightness4Icon from '@mui/icons-material/Brightness4';
import Brightness7Icon from '@mui/icons-material/Brightness7';

export default function ColorModeToggle() {
  const { mode, setMode } = useColorScheme();
  return (
    <IconButton
      onClick={() => setMode(mode === 'light' ? 'dark' : 'light')}
      aria-label="toggle color mode"
    >
      {mode === 'dark' ? <Brightness7Icon /> : <Brightness4Icon />}
    </IconButton>
  );
}

5. Pigment CSS with Next.js

Pigment CSS is MUI's zero-runtime CSS-in-JS solution. It extracts styles at build time, producing static CSS files. This means:

  • No Emotion runtime shipped to the browser
  • No useServerInsertedHTML or extractCriticalToChunks needed
  • Full RSC compatibility (styles are static, not context-dependent)

Installation

npm install @pigment-css/react @pigment-css/nextjs-plugin

next.config.mjs setup

// next.config.mjs
import { withPigment } from '@pigment-css/nextjs-plugin';

const nextConfig = {
  // your existing Next.js config
};

export default withPigment(nextConfig, {
  theme: {
    palette: {
      primary: { main: '#1976d2' },
      background: { default: '#fafafa' },
    },
    typography: {
      fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
    },
    // Pigment CSS theme uses the same shape as createTheme
  },
  // Pigment-specific options:
  transformLibraries: ['@mui/material'],
  // Transforms MUI's styled() calls into static CSS at build time
});

Using Pigment CSS utilities directly

import { styled, css } from '@pigment-css/react';

// These are extracted at build time — zero runtime cost
const StyledCard = styled('div')(({ theme }) => ({
  padding: theme.spacing(2),
  backgroundColor: theme.palette.background.paper,
  borderRadius: theme.shape.borderRadius,
}));

// className-based utility
const highlightClass = css(({ theme }) => ({
  color: theme.palette.primary.main,
  fontWeight: 700,
}));

Pigment CSS vs Emotion SSR comparison

AspectEmotion SSRPigment CSS
Runtime JS~12 kB gzipped0 kB
SSR extractionRequired (extractCriticalToChunks / useServerInsertedHTML)Not needed
RSC supportNeeds 'use client' boundaryWorks in server components
Dynamic stylesFull runtime supportLimited (CSS variables for dynamic values)
Build timeNormalSlightly longer (extraction step)
MUI compatibilityAll componentsMUI v6+ with @pigment-css/react

6. Common SSR Pitfalls

Hydration mismatch from useMediaQuery

useMediaQuery uses window.matchMedia which does not exist on the server. By default, it returns false on the server. If the client evaluates true, you get a hydration mismatch.

Fix: provide ssrMatchMedia

import { createTheme, ThemeProvider } from '@mui/material/styles';
import useMediaQuery from '@mui/material/useMediaQuery';

// Server-side: pass the user-agent to approximate the device
function getServerTheme(userAgent: string) {
  return createTheme({
    components: {
      MuiUseMediaQuery: {
        defaultProps: {
          ssrMatchMedia: (query: string) => ({
            matches: mediaQuery.match(query, {
              // Provide a width matching the UA
              width: /mobile/i.test(userAgent) ? '0px' : '1024px',
            }),
          }),
        },
      },
    },
  });
}

Alternative: defer rendering until client

'use client';

import useMediaQuery from '@mui/material/useMediaQuery';

export default function ResponsiveComponent() {
  const isMobile = useMediaQuery('(max-width:600px)', {
    // Do not render on server — avoids mismatch entirely
    noSsr: true,
    // defaultMatches controls server-side return value
    defaultMatches: false,
  });

  return isMobile ? <MobileView /> : <DesktopView />;
}

Flash of unstyled content (FOUC) prevention checklist

  1. Use prepend: true in Emotion cache so MUI styles are injected before other styles
  2. Use getInitColorSchemeScript for dark/light mode (see section 4)
  3. In Pages Router, ensure _document.tsx extracts critical CSS (see section 2)
  4. In App Router, ensure useServerInsertedHTML flushes styles (see section 1)
  5. Add suppressHydrationWarning to <html> when using color scheme scripts

Portal components (Dialog, Menu, Popper, Tooltip) and SSR

Portal components render into document.body via createPortal. On the server, document does not exist. MUI handles this internally by deferring portal mounting, but be aware:

  • Do not conditionally render portals based on server/client detection. MUI already does this. Extra checks cause hydration mismatches.
  • Use disablePortal if you need server-rendered content (e.g., for SEO in Dialog):
<Dialog open={open} disablePortal>
  <DialogTitle>Server-Rendered Dialog</DialogTitle>
  <DialogContent>This content is in the DOM tree, not a portal.</DialogContent>
</Dialog>
  • Menu/Autocomplete disablePortal keeps the dropdown in the DOM flow, avoiding SSR issues with portals but requiring careful z-index management.

Dynamic imports for heavy MUI X components

Large components like DataGrid, DatePicker, and Charts add significant bundle weight. Use next/dynamic to code-split them:

import dynamic from 'next/dynamic';

const DataGrid = dynamic(
  () => import('@mui/x-data-grid').then((mod) => mod.DataGrid),
  {
    loading: () => <Skeleton variant="rectangular" height={400} />,
    ssr: false, // DataGrid is interactive-only; skip SSR
  }
);

const DatePicker = dynamic(
  () => import('@mui/x-date-pickers/DatePicker').then((mod) => mod.DatePicker),
  {
    loading: () => <Skeleton variant="rectangular" width={300} height={56} />,
    ssr: false,
  }
);

When to use ssr: false:

  • Component is purely interactive (DataGrid, Charts)
  • Component relies heavily on browser APIs (DatePicker with locale)
  • Component is below the fold and not SEO-relevant

When to keep SSR enabled:

  • Content is above the fold and needs fast FCP
  • Content is SEO-relevant (product listings, article text)

7. Emotion Cache Configuration

prepend: true is critical

const cache = createCache({
  key: 'mui',
  prepend: true, // MUI styles go BEFORE other <style> tags
});

Without prepend: true, MUI styles may be overridden by global CSS or other libraries because of CSS source order. With prepend: true, MUI styles are inserted at the top of <head>, giving them lower specificity in the cascade and allowing your custom styles to win.

Cache key naming to avoid conflicts

If you have multiple Emotion caches (e.g., MUI + your own styled-components via Emotion), each must have a unique key:

// MUI cache
const muiCache = createCache({ key: 'mui', prepend: true });

// App-specific cache
const appCache = createCache({ key: 'app' });

The key is used as a prefix in generated class names (mui-1a2b3c, app-4d5e6f) and as the data-emotion attribute value on <style> tags. Duplicate keys cause style clobbering.

Per-request cache on server (not singleton)

On the server, create a new Emotion cache for every request. A singleton cache accumulates styles across requests and causes:

  • Memory leaks (styles from request A bleed into request B)
  • Wrong styles rendered (stale cache entries)
  • Increasing response sizes over time
// WRONG: singleton cache on server
const cache = createCache({ key: 'mui' }); // created once at module scope

// CORRECT: per-request cache
function handleRequest(req, res) {
  const cache = createCache({ key: 'mui' }); // fresh for each request
  // ... render with this cache
}

In the App Router, the useState initializer in NextAppDirEmotionCacheProvider (section 1) already ensures per-render cache creation because React creates a new component instance for each server render.

In the Pages Router, _document.tsx's getInitialProps creates a new cache per request (section 2).


Quick Reference: Which Setup Do I Need?

SetupApp Router (v13+)Pages RouterStatic Export
ThemeRegistry + useServerInsertedHTMLRequiredN/ARequired
_document.tsx + extractCriticalToChunksN/ARequiredN/A
getInitColorSchemeScriptRecommendedRecommendedRecommended
Pigment CSSAlternative (replaces Emotion)AlternativeAlternative
@emotion/cache with prependRequiredRequiredRequired
@emotion/serverNot neededRequiredNot needed

Package Versions

These patterns apply to:

  • @mui/material v5.14+ and v6.x
  • next v13.4+ (App Router), v12+ (Pages Router)
  • @emotion/react v11.x, @emotion/cache v11.x
  • @pigment-css/react v0.0.x (early adoption, API may change)

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

34.17%
按下载量换算25

Claude

31.48%
按下载量换算23

Cursor

19.15%
按下载量换算14

Gemini CLI

9.84%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

操作浏览器

该 Skill 可能涉及浏览器控制能力,使用时可能读取或操作网页内容,需要在受控环境中确认权限边界。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills