Build process crashes during pre-rendering when using vite-plugin-ssr with Icon component from @mdi/react #830
-
Issue DescriptionWhen using The discussion #704 seems to be the exact same error as this issue but it was deemed an "user land error". Expected BehaviorThe build process should successfully pre-render the page that includes an Actual BehaviorThe build process crashes during pre-rendering when an Reproduction StepsI have prepared a StackBlitz that follows the following reproduction steps:
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import ssr from 'vite-plugin-ssr/plugin';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react(),
ssr({ prerender: true, includeAssetsImportedByServer: true }),
],
});
// src/pages/doesnt-work.page.tsx
import { mdiAbTesting } from "@mdi/js";
import Icon from "@mdi/react";
export function Page() {
return (
<Icon path={mdiAbTesting} />
);
}
Additional InformationThis issue appears to be specific to As a temporary workaround, the issue can be resolved by rendering the component only on the client-side using conditional rendering, such as the NoSSR component used in the example below. However, this defeats the point of pre-rendering a page. // works.page.tsx
import { mdiAbTesting } from '@mdi/js';
import Icon from '@mdi/react';
import { useEffect, useState } from 'react';
type NoSSRProps = React.PropsWithoutRef<{
children?: React.ReactNode;
}>;
const NoSSR: React.FC<NoSSRProps> = ({ children }) => {
const [isClient, setIsClient] = useState(false);
useEffect(() => {
setIsClient(true);
}, []);
return isClient ? <>{children}</> : null;
};
export function Page() {
return (
<NoSSR>
<Icon path={mdiAbTesting} />
</NoSSR>
);
} Error Message + Error Stack❯ npm run build
$ tsc && vite build
vite v4.3.3 building for production...
✓ 157 modules transformed.
dist/client/vite-plugin-ssr.json 0.16 kB │ gzip: 0.14 kB
dist/client/manifest.json 2.32 kB │ gzip: 0.45 kB
dist/client/assets/chunks/chunk-4ed993c7.js 0.00 kB │ gzip: 0.02 kB
dist/client/assets/entries/src_pages_doesnt-work.page.18e030ae.js 0.16 kB │ gzip: 0.14 kB
dist/client/assets/entries/src_pages_works.page.609e92a9.js 0.31 kB │ gzip: 0.23 kB
dist/client/assets/entries/entry-server-routing.2e8097b5.js 2.52 kB │ gzip: 1.16 kB
dist/client/assets/chunks/chunk-8bc41161.js 7.40 kB │ gzip: 2.80 kB
dist/client/assets/chunks/chunk-33c17f51.js 7.71 kB │ gzip: 2.87 kB
dist/client/assets/chunks/chunk-55f9c175.js 28.76 kB │ gzip: 10.09 kB
dist/client/assets/entries/entry-client-routing.65682bcb.js 35.32 kB │ gzip: 11.64 kB
dist/client/assets/entries/src_renderer_default.page.client.726fcd61.js 134.86 kB │ gzip: 43.56 kB
vite v4.3.3 building SSR bundle for production...
✓ 11 modules transformed.
dist/server/package.json 0.02 kB
dist/server/importBuild.cjs 0.35 kB
dist/server/entries/src_pages_doesnt-work-page.mjs 0.22 kB
dist/server/importBuild.mjs 0.23 kB
dist/server/entries/src_pages_works-page.mjs 0.53 kB
dist/server/entries/src_renderer_default-page-server.mjs 1.67 kB
dist/server/pageFiles.mjs 3.22 kB
✓ built in 112ms
vite-plugin-ssr v0.4.119 pre-rendering HTML...
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
at Ib (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:72:155)
at W (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:73:89)
at Ib (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:67:482)
at W (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:73:89)
at Ib (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:71:304)
at W (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:73:89)
at Ib (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:67:482)
at W (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:73:89)
at Ib (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:67:482)
at W (file:///home/projects/vitejs-vite-crvbkk/node_modules/react-dom/cjs/react-dom-server-legacy.node.production.min.js:73:89) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
It’s possibly an ESM-CJS compatibility thing. I’d inspect how mdi exports the components and import them accordingly. Also check https://vite-plugin-ssr.com/common-issues#npm-packages-containing-invalid-code. |
Beta Was this translation helpful? Give feedback.
-
Reproduction reposted on GitHub: https://github.com/brillout/vps-mdi. |
Beta Was this translation helpful? Give feedback.
-
Further development on this, I tried installing // index.page.tsx
import { ReactComponent as MyIcon} from "../assets/icon.svg";
export function Page() {
return <MyIcon />;
} Error
|
Beta Was this translation helpful? Give feedback.
It’s possibly an ESM-CJS compatibility thing. I’d inspect how mdi exports the components and import them accordingly.
Also check https://vite-plugin-ssr.com/common-issues#npm-packages-containing-invalid-code.