Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

node.js - Troubleshooting SVG error in Gatsby build

I'm trying to get a simple Gatsby site online -- gatsby develop works fine but on gatsby build I get this error message:

enter image description here

UNHANDLED REJECTION 
- Error in parsing SVG: 
- Unencoded <
- Line: 0
- Column: 2
- Char: <

I'm not explicitly using any SVGs so struggling to troubleshoot this - any advice?

My project is here, adapted from the lumen starter theme.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your Icon.js (line 14) file you are using a <svg>. This is causing your issue:

// @flow strict
import React from 'react';
import styles from './Icon.module.scss';

type Props = {
  name: string,
  icon: {
    viewBox?: string,
    path?: string
  }
};

const Icon = ({ name, icon }: Props) => (
  <svg className={styles['icon']} viewBox={icon.viewBox}>
    <title>{name}</title>
    <path d={icon.path} />
  </svg>
);

export default Icon;

When dealing with SVG I would recommend using gatsby-plugin-react-svg or using a React built-in usage.

  • With gatsby-plugin-react-svg you just to isolate the SVG in a separate folder that must only contain SVGs):

    {
       resolve: 'gatsby-plugin-react-svg',
       options: {
         rule: {
           include: /assets/
         }
       }
    }
    
    

    Then import it as a React component:

    import Icon from "./path/assets/icon.svg";
    
    // ...
    
    <Icon />;
    
  • React built-in usage (only available with [email protected] or higher, and [email protected] or higher, more details in https://create-react-app.dev/docs/adding-images-fonts-and-files/):

      import { ReactComponent as FacebookIcon } from '../images/svg/facebookF.svg';
      import { ReactComponent as Email } from '../images/svg/email.svg';
    
      ...
    
        <FacebookIcon />
        <Email /> 
    

Since you are extending from a starter theme, remove that component and it's using from your project and add it on-demand if needed.

You can read this StackOverflow answer for further details about dealing SVG in Gatsby.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...