+3. **Data Fetching Redundancy**: Be mindful of redundant data fetching. Consider implementing caching strategies to avoid fetching the same data multiple times, both on the server and client sides.
-## FAQ
+4. **Data Privacy**: Think about data privacy and security. Ensure that sensitive data is not exposed during server-side rendering and that any client-side data fetching adheres to security best practices.
-### window is not defined, document is not defined, navigator is not defined
+5. **Performance Impact**: Excessive or inefficient data pre-fetching can negatively impact performance. Be aware of the performance implications of your data fetching logic and optimize it as needed.
-SSR 因为会在服务端执行 render 渲染方法,而服务端没有 DOM/BOM 变量和方法,为解决这类问题,提供以下几个方法:
+6. **Client-Side Re-fetching**: By default, `getInitialProps` may re-fetch data on the client side when navigating between pages. If this behavior is not desired, consider using client-side state management or caching to avoid unnecessary data fetches.
-1. 如果是项目自身的代码,建议将访问的 DOM/BOM 方法放在 `componentDidMount`、`useEffect` 中(服务端不会执行),避免服务端执行时报错,例如:
+7. **Loading Indicators**: Implement loading indicators or placeholders to provide a better user experience while data is being fetched, especially on the client side.
-```diff
-import React from 'react';
+8. **Testing**: Test your data pre-fetching logic thoroughly to catch potential issues early. Pay attention to edge cases and error scenarios.
-export default () => {
-- window.alert(1);
- React.useEffect(() => {
-+ window.alert(1);
- }, []);
+9. **Documentation**: Document your data pre-fetching patterns and conventions, especially if multiple team members are working on the project. Clear documentation can help ensure consistency and reduce confusion.
- return (
- 组件动态加载中...
-export default dynamic({ - loader: async () => { - // 动态加载第三方组件 - const { default: DynamicComponent } = await import( - /* webpackChunkName: "dynamic-component" */ 'dynamic-component' - ); - return DynamicComponent; - }, - loading: () => renderLoading(), -}); -``` -避免ssr渲染时报 ` did not match.`警告,使用时候ssr应当渲染相同`loading`组件 -``` -import React from 'react'; -import { isBrowser } from 'umi'; -import DynamicComponent from 'DynamicComponent'; -export default () => { - if(isBrowser()) returnHello
' }} /> -+