Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSR Compatibility Issue #123

Closed
bebeal opened this issue Jul 27, 2024 · 3 comments
Closed

SSR Compatibility Issue #123

bebeal opened this issue Jul 27, 2024 · 3 comments

Comments

@bebeal
Copy link

bebeal commented Jul 27, 2024

Problem

When using SVGR with Vite in a Server-Side Rendering (SSR) configuration, the default type definitions provided by SVGR lead to errors during the server-side rendering process.

Using .svg?react to import SVGs as React components cause "Invalid tag" errors during server-side rendering.

Example

// Using Vite SSR
import React from 'react';
import NodeLogo from './assets/node.svg?react'

export const App = () => {
  return (
    <div>
      <h1>Welcome to my app</h1>
      <NodeLogo width={50} height={50} />
    </div>
  );
}

Error

Error: Invalid tag: /src/assets/node.svg?react
    at startChunkForTag (/Users/bebeal/Prod/vite-amplify/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:2734:13)
    at pushStartGenericElement (/Users/bebeal/Prod/vite-amplify/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:2560:15)
    at pushStartInstance (/Users/bebeal/Prod/vite-amplify/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:2838:18)
    at renderHostElement (/Users/bebeal/Prod/vite-amplify/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js:5640:18)
    ...

Workaround

A simple workaround is to override the type definitions as follows:

// Defines the type for .svg?url imports to be a string
declare module '*.svg?url' {
  const src: string;
  export default src;
}

// Defines the type for .svg imports to be React components
declare module '*.svg' {
  const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>> & { title?: string };
  export default ReactComponent;
}

Usage

The updated usage works for both react components and urls, without any errors.

// Using Vite SSR
import React from 'react';
import NodeLogo from './assets/node.svg'
import NodeUrl from './assets/node.svg?url'

export const App = () => {
  return (
    <div>
      <h1>Welcome to my app</h1>
      <NodeLogo width={50} height={50} />
      <img src={NodeUrl} alt="Node.js logo" width={50} height={50} />
    </div>
  );
}

I'm open to discussing further and contributing if we can get this functionality to work out of the box. But feel free to address this as you see fit, I just wanted to bring attention to it.

@pd4d10
Copy link
Owner

pd4d10 commented Aug 15, 2024

get this functionality to work out of the box.

Sure. Which SSR framework are you using? Could you please provide a minimal reproduction?

@bebeal
Copy link
Author

bebeal commented Aug 26, 2024

Vite SSR - here's a single page app that can reproduce https://github.com/bebeal/vite-amplify

@bebeal
Copy link
Author

bebeal commented Sep 10, 2024

closing this issue - I believe the root cause is related to vite vitejs/vite#18034

@bebeal bebeal closed this as completed Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants