4

The documentation says that turbopack works with @svgr/webpack, but doesn't really want to work.

If you run the project without --turbo (turbopack), everything works fine. And if run with --turbo, it gives an error:

Error run turbopack:

Processing image failed
  Failed to parse svg source code for image dimensions
  
  Caused by:
  - Source code does not contain a <svg> root element

my next.config.js

const nextConfig = {
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/i,
      use: ["@svgr/webpack"],
    });

    return config;
  },

  experimental: {
    appDir: true,
    turbo: {
      loaders: {
        ".svg": ["@svgr/webpack"],
      },
    },
  },
};

module.exports = nextConfig;
1
  • Does this answer your question? Can't import SVG into Next.js (refer last part of my answer, should work fine with latest canary) Commented Feb 9, 2024 at 15:30

2 Answers 2

7

Error occurs because next tries to get dimensions of svg component but @svgr/webpack already change svg to React component. to prevent next.js doing that you can specify "as: '*.js'" in rules options;

next.confing.js

const nextConfig = {
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/i,
      use: ["@svgr/webpack"],
    });

    return config;
  },

  experimental: {
    turbo: {
      rules: {
        '*.svg': {
          loaders: ['@svgr/webpack'],
          as: '*.js'
        }
      },
    }
  },
};

module.exports = nextConfig;
Sign up to request clarification or add additional context in comments.

Comments

0

You can refer to the official next js documentation:
turbopack doc

To use "@svgr/webpack" you can add this section to the next.config file:

turbopack: {
  rules: {
    '*.svg': {
      loaders: ['@svgr/webpack'],
      as: '*.js',
    },
  },
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.