|
45 | 45 | <a href="./nextjs-guide.html" class="active">Next.js</a> |
46 | 46 | <a href="./vite-guide.html">Vite</a> |
47 | 47 | </div> |
| 48 | + <div class="sidebar-group"> |
| 49 | + <div class="sidebar-label">Tutorials</div> |
| 50 | + <a href="./tutorial-editor.html">Editor + Preview</a> |
| 51 | + </div> |
48 | 52 | <div class="sidebar-group"> |
49 | 53 | <div class="sidebar-label">Reference</div> |
50 | 54 | <a href="./api-reference.html">API Reference</a> |
@@ -181,20 +185,28 @@ <h2>CSS Modules</h2> |
181 | 185 | <p>Classes are scoped with a hash to prevent collisions between components.</p> |
182 | 186 |
|
183 | 187 | <h2>Hot Module Replacement</h2> |
184 | | - <p>The dev server supports HMR through React Refresh. When you change a file in the VFS, the server detects it and pushes updates to the browser without a full page reload:</p> |
185 | | - <pre><code><span class="cm">// Initial page</span> |
186 | | -vfs<span class="op">.</span><span class="fn">writeFileSync</span><span class="op">(</span><span class="str">'/app/page.tsx'</span><span class="op">,</span> <span class="str">` |
187 | | - export default function Home() { |
188 | | - return <h1>Version 1</h1>; |
189 | | - } |
190 | | -`</span><span class="op">);</span> |
191 | | - |
192 | | -<span class="cm">// Later, update the file — HMR kicks in automatically</span> |
| 188 | + <p>The dev server supports HMR through React Refresh. Two things are required for HMR to work:</p> |
| 189 | + <ol> |
| 190 | + <li>Call <code>devServer.start()</code> — this enables file watching on the VFS</li> |
| 191 | + <li>Call <code>devServer.setHMRTarget(iframe.contentWindow)</code> — this tells the server where to send updates via <code>postMessage</code></li> |
| 192 | + </ol> |
| 193 | + <p>Once both are set up, writing to the VFS triggers HMR automatically:</p> |
| 194 | + <pre><code><span class="kw">const</span> devServer <span class="op">=</span> <span class="kw">new</span> <span class="fn">NextDevServer</span><span class="op">(</span>vfs<span class="op">,</span> <span class="op">{</span> port<span class="op">:</span> <span class="num">3000</span><span class="op">,</span> root<span class="op">:</span> <span class="str">'/'</span> <span class="op">});</span> |
| 195 | +devServer<span class="op">.</span><span class="fn">start</span><span class="op">();</span> <span class="cm">// Required: enables file watching for HMR</span> |
| 196 | + |
| 197 | +<span class="cm">// After bridge setup and iframe load:</span> |
| 198 | +devServer<span class="op">.</span><span class="fn">setHMRTarget</span><span class="op">(</span>iframe<span class="op">.</span>contentWindow<span class="op">);</span> <span class="cm">// Required: HMR delivery target</span> |
| 199 | + |
| 200 | +<span class="cm">// Now updating a file triggers HMR automatically</span> |
193 | 201 | vfs<span class="op">.</span><span class="fn">writeFileSync</span><span class="op">(</span><span class="str">'/app/page.tsx'</span><span class="op">,</span> <span class="str">` |
194 | 202 | export default function Home() { |
195 | 203 | return <h1>Version 2</h1>; |
196 | 204 | } |
197 | 205 | `</span><span class="op">);</span></code></pre> |
| 206 | + <div class="callout"> |
| 207 | + <div class="callout-title">Important</div> |
| 208 | + <p>Without <code>devServer.start()</code>, the server won't watch the VFS for changes and HMR updates won't fire. This is the most common cause of HMR not working.</p> |
| 209 | + </div> |
198 | 210 |
|
199 | 211 | <h2>Configuration</h2> |
200 | 212 | <p>You can provide a <code>next.config.js</code> for path aliases and other options:</p> |
|
0 commit comments