Skip to content

Plugin causes Vite to hang indefinitely after build with React 19.0.0 #3

Open
@tim-meredith-acoa

Description

@tim-meredith-acoa

Something about the new version of React seems to interact negatively with this plugin. When I try to use the plugin with React 19.0.0, it will build successfully, but then after the build is done, Vite just doesn't exit, it stays blocking the terminal and seemingly doing nothing, which is mildly annoying in development, but more importantly it completely breaks automated building for deployment.

Steps to reproduce

Make a new React project with Vite:

npm create vite@latest . -- --template react-ts

Go into package.json, and update the following packages to version 19.0.0:

  • react
  • react-dom
  • @types/react
  • @types/react-dom

and then run npm install to install the new version.

Install the plugin:

npm install -D vite-prerender plugin

Replace vite.config.ts with the following:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { vitePrerenderPlugin } from 'vite-prerender-plugin'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
    vitePrerenderPlugin({
      renderTarget: '#root'
    })
  ]
})

Add the prerender attribute to the <script> tag in index.html.

Replace src/main.tsx with the following:

import { StrictMode } from 'react'
import { hydrateRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'

const Root = () => {
  return <StrictMode>
    <App />
  </StrictMode>
}

if (typeof window !== 'undefined') {
  hydrateRoot(document.getElementById('root')!, <Root />)
}

export async function prerender() {
  const { renderToString } = await import('react-dom/server')
  const html = renderToString(<Root />)
  return { html }
}

Finally, run npm run build. The build will be successful, but after it finishes, Vite will never exit.

> [email protected] build
> tsc -b && vite build

vite v6.0.7 building for production...
✓ 40 modules transformed.
dist/index.html                           1.09 kB │ gzip:  0.53 kB
dist/assets/react-CHdo91hT.svg            4.13 kB │ gzip:  2.05 kB
dist/assets/main-n_ryQ3BS.css             1.39 kB │ gzip:  0.71 kB
dist/assets/index-CPPIKvJG.js             0.74 kB │ gzip:  0.41 kB │ map:   0.10 kB
dist/assets/server.browser-DcgWWkx7.js  160.51 kB │ gzip: 49.25 kB │ map: 659.32 kB
dist/assets/main-nV0DskNB.js            187.10 kB │ gzip: 59.33 kB │ map: 871.42 kB
✓ built in 1.68s

I'd be happy to help debug this more, but I don't really know what else to try. One thing I've observed is that the import of react-dom/server does seem to be important, because if I keep everything else the same and change the prerender function to this:

export async function prerender() {
  //const { renderToString } = await import('react-dom/server')
  //const html = renderToString(<Root />)
  return { html: "<div>test</div>" }
}

Then it will build without hanging. Hope that helps narrow things down, and let me know if you want me to try out anything else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions