Ant Design StyleAnt Design Style
Quick Start
Best Practices
API Reference
Release Notes
⌘ K

Table of Contents

Integration in Next.js
Page Router
App Router
Integration with dumi
1. Wrap StyledProvider in the global Layout
2. Use the extractStaticStyle method to extract styles to a separate file
Related API
Basic Knowledge
Introduction
Quick Start to CSS in JS
Comparison of CSS in JS Writing Methods
Design Philosophy and Implementation Strategy
Quick Start
Writing Style
Theme Switching
Custom Theme
Advanced Usage
Babel Plugin
SSR Integration
Component Development
Integrated styled
🚧 Stylish Composite Styles
Performance Comparison of CSS-in-JS Libraries
Migrating from Less
Less Application Automated Migration
Manual Migration of Less Applications
Migrating Less Components
🚧 Compilation Differences Between CSS-in-JS and Less
PREV
Babel Plugin
NEXT
Component Development

相关资源

Ant Design
Ant Design Pro
Ant Design Pro Components
Umi-React 应用开发框架
Dumi-组件/文档研发工具
qiankun-微前端框架

社区

Medium
Twitter
yuqueAnt Design 语雀专栏
Ant Design 知乎专栏
体验科技专栏
seeconfSEE Conf-SEE Conf-蚂蚁体验科技大会

帮助

GitHub
更新日志
讨论

more products更多产品

yuque语雀-知识创作与分享工具
AntVAntV-数据可视化解决方案
EggEgg-企业级 Node.js 框架
kitchenKitchen-Sketch 工具集
xtech蚂蚁体验科技
Copyright © 2022-2024
Made with ❤️ by 蚂蚁集团 - AFX & 数字科技
‌
‌
‌
‌

SSR Integration

SSR (Server-side rendering) refers to the direct output of dynamically generated HTML to the browser on the server side in order to improve the initial loading speed of the page and SEO optimization. For SSR scenarios, antd-style provides a dedicated method to extract antd components and antd-style styles statically, thereby improving the rendering speed and performance of the page. The following is a guide on using antd-style in SSR:

Integration in Next.js

Page Router

In Next.js server-side rendering, you need to use the extractStaticStyle method in the getInitialProps method of the component to extract static styles and add them to the page's head. The specific usage is as follows:

tsx

1. Import and wrap the StyleProvider component, and wrap it around the component that needs to extract static styles:

Pass the enhanceApp parameter to the renderPage method. enhanceApp is a function used to enhance the App component. Here, we wrap the App component in the StyleProvider component to extract static styles.

tsx

Here, the cache field mounts the @ant-design/cssinjs cache object.

2. Use the extractStaticStyle method to extract static styles

Call the renderPage method to get the rendered page content, and use the extractStaticStyle method to extract the static styles from it.

tsx

The styles extracted by the extractStaticStyle method is an array, which includes the style objects of antd and antd-style. The structure of each style object is as follows:

PropertyTypeDescription
keystringKey value, the key of antd style is antd, and the default key of antd-style style is acss
styleJSX.ElementStyle element, represented using the JSX.Element type
cssstringCorresponding CSS string
idsstring[]Array of element IDs to which the style is applied
tagstringCSS string with <style> tag

3. Add the extracted styles to the page's head

Since Next.js requires directly inserting <style> style elements, we can extract the style style elements from the array of style objects obtained from extractStaticStyle and add them to the page's head.

tsx

App Router

INFO

Requires antd-style version 3.5.0 or above

App Router is a fully-fledged application mode introduced in Next.js version 13.4. antd-style also supports this mode. The integration method is as follows:

Create a StyleRegistry.tsx component to collect and insert the extracted static styles into the HTML:

tsx

Import this component in app/layout.tsx:

tsx
WARNING

Due to the lack of a hook to get the HTML in Next.js's App Router, extractStaticStyle cannot analyze the styles used in the current application, which means it lacks TreeShaking capability. Therefore, the styles introduced by App Router will be larger in size compared to Page Router.

Integration with dumi

In this example, the styles are extracted to a CSS static file and then imported into the HTML.

1. Wrap StyledProvider in the global Layout

Wrap StyledProvider in the global Layout of umi, and define a global cache variable to cache antd styles:

tsx

2. Use the extractStaticStyle method to extract styles to a separate file

In the server-side rendering related files of umi (e.g., plugin.ts), use the extractStaticStyle method to extract styles to a separate file:

ts
Reference

dumi-theme-antd-style: https://github.com/arvinxx/dumi-theme-antd-style/blob/master/src/plugin/index.ts

Related API

extractStaticStyle