图片是客户端渲染还是服务端渲染?React新手的实操疑问
Hey there! Let's break this down in super practical terms since you're getting started with React and client-side rendering (CSR). I know the concepts of CSR vs SSR can feel abstract at first, so let's tie them directly to your image loading questions.
<img src="airplane.png" /> load? Short answer: This image is always loaded by the client, no matter if you're using CSR or SSR. Here's the step-by-step breakdown of what happens:
- When you make an initial request to your server:
- For CSR: The server sends back a minimal "shell" HTML file (usually just a root
<div>and a script tag for your React bundle). - For SSR: The server sends back fully rendered HTML with all your page content (including the
<img>tag).
- For CSR: The server sends back a minimal "shell" HTML file (usually just a root
- In both cases, the HTML only contains the reference to the image (
src="airplane.png"), not the actual image file data. - Once the browser parses this HTML, it will send a separate HTTP request to your server (or a CDN) to fetch the
airplane.pngfile. Only then does the image load onto the page.
To see this in action: Open your browser's DevTools (F12), go to the Network tab, and refresh your page. You'll see:
- First, a request for your HTML file (e.g.,
index.html) - Then requests for your React JS bundles
- Finally, a separate request for
airplane.png(look for the "Img" type in the Network tab)
First, a quick clarification: The server can't "load" images for you in the way you might be thinking. Servers only send text-based content (HTML/JS/CSS) that tells the browser what resources to fetch. All actual image loading is handled by the client (browser).
That said, for large numbers of images, client-side optimization strategies are your best bet. Here's what to focus on:
- Lazy Loading: Only load images when they're about to enter the user's viewport. In React, you can use the native
loading="lazy"attribute on<img>tags, or libraries likereact-lazyloadfor more control. This cuts down on initial page load time by reducing the number of simultaneous requests. - Image Format & Size Optimization: Use modern, compressed formats like WebP or AVIF instead of PNG/JPEG. Also, use the
srcsetattribute to serve different image sizes based on the user's screen resolution (e.g., smaller images for mobile devices). - Caching: Configure your server or CDN to set proper
Cache-Controlheaders for images. This lets browsers cache images after the first load, so returning users don't have to re-download them.
What about SSR here? SSR just renders the <img> tags in the initial HTML, which might let the browser start requesting images a tiny bit earlier. But it doesn't change the fact that images are still client-loaded, and it won't help with reducing the total number of requests. Lazy loading and format optimization are far more impactful for large image sets.
One edge case: If you have tiny icons (a few KB), you could convert them to base64 and embed them directly in your HTML/JS. This means the image data is sent with the initial request (so "server-side delivered"), but this is only practical for very small images—doing this for lots of large images would make your initial HTML/JS bundle massive, slowing down page load.
Hope this clears up the practical side of things for you!
内容的提问来源于stack exchange,提问作者Bob Dole

