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

SVG loading stops working when upgrading from 18.0.4 to 18.0.5 for @nx/next #22362

Closed
1 of 4 tasks
samvloeberghs opened this issue Mar 18, 2024 · 9 comments
Closed
1 of 4 tasks
Assignees
Labels
outdated scope: nextjs Issues related to NextJS support for Nx type: bug

Comments

@samvloeberghs
Copy link

samvloeberghs commented Mar 18, 2024

Current Behavior

We are loading in SVGs in our NextJS application as follows:

import { ReactComponent as AsicLogoSvg } from '../public/imgv2/logos/asic-logo.svg';

This works in @nx/next v18.0.4, but stops working when upgrading to 18.0.5 or 18.1.x.
It seems that in both the newer 18.0 and 18.1 releases the following PR was released:
#21761

Expected Behavior

Code should keep working as is, especially if no major or minor upgrade done.

GitHub Repo

samvloeberghs/nx-error-example@2194432

Steps to Reproduce

  1. Clone and install library
  2. Run npx nx dev example
  3. Observe

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.10.0
OS     : darwin-arm64
npm    : 10.2.3

nx                 : 18.1.1
@nx/js             : 18.1.1
@nx/jest           : 18.1.1
@nx/linter         : 18.1.1
@nx/eslint         : 18.1.1
@nx/workspace      : 18.1.1
@nx/angular        : 18.1.1
@nx/cypress        : 18.1.1
@nx/devkit         : 18.1.1
@nx/eslint-plugin  : 18.1.1
@nx/next           : 18.0.5
@nx/node           : 18.1.1
@nx/react          : 18.1.1
@nx/storybook      : 18.1.1
@nrwl/tao          : 18.1.1
@nx/web            : 18.1.1
@nx/webpack        : 18.1.1
nx-cloud           : 18.0.0
typescript         : 5.3.3
---------------------------------------
Community plugins:
@ng-bootstrap/ng-bootstrap  : 16.0.0
@nx-aws-plugin/nx-aws-cache : 3.2.0
@storybook/angular          : 7.6.17
---------------------------------------
The following packages should match the installed version of nx
  - @nx/next@18.0.5
  - @nrwl/next@18.0.5

To fix this, run `nx migrate nx@18.1.1`

Failure Logs

Failure example reproduction repo:

$ npx nx build example                                                   

> nx run example:build

   ▲ Next.js 14.0.4

   Creating an optimized production build  ...<w> [webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item 'Compilation/modules|javascript/auto|/Users/svloeberghs/example/node_modules/@svgr/webpack/dist/index.js!/Users/svloeberghs/example/apps/example/src/app/asic-logo.svg|rsc': No serializer registered for SvgoParserError
<w> while serializing webpack/lib/cache/PackFileCacheStrategy.PackContentItems -> webpack/lib/NormalModule -> webpack/lib/ModuleBuildError -> SvgoParserError
Failed to compile.

./src/app/asic-logo.svg
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <svg viewBox="0 0 810 255" fill="none" xmlns="http://www.w3.org/2000/svg">
|   <g id="Group">
|     <path id="Vector"

Import trace for requested module:
./src/app/asic-logo.svg

./src/app/asic-logo.svg
SvgoParserError: /Users/svloeberghs/example/apps/example/src/app/asic-logo.svg:29:147: Invalid character in entity name
    at sax.onerror (/Users/svloeberghs/example/node_modules/svgo/lib/parser.js:247:19)
    at emit (/Users/svloeberghs/example/node_modules/@trysound/sax/lib/sax.js:518:35)
    at error (/Users/svloeberghs/example/node_modules/@trysound/sax/lib/sax.js:549:5)
    at strictFail (/Users/svloeberghs/example/node_modules/@trysound/sax/lib/sax.js:573:7)
    at SAXParser.write (/Users/svloeberghs/example/node_modules/@trysound/sax/lib/sax.js:1395:13)
    at parseSvg (/Users/svloeberghs/example/node_modules/svgo/lib/parser.js:259:7)
    at optimize (/Users/svloeberghs/example/node_modules/svgo/lib/svgo.js:66:17)
    at Object.optimize (/Users/svloeberghs/example/node_modules/svgo/lib/svgo-node.js:76:10)
    at svgoPlugin (/Users/svloeberghs/example/node_modules/@svgr/plugin-svgo/dist/index.js:76:23)
    at run (/Users/svloeberghs/example/node_modules/@svgr/core/dist/index.js:174:16)
    at Object.transform (/Users/svloeberghs/example/node_modules/@svgr/core/dist/index.js:180:10)
    at async /Users/svloeberghs/example/node_modules/@svgr/webpack/dist/index.js:66:20

Import trace for requested module:
./src/app/asic-logo.svg
./src/app/page.tsx


> Build failed because of webpack errors
   Creating an optimized production build  .Warning: command "next build" exited with non-zero status code
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Ran target build for project example (3s)

   ✖  1/1 failed
   ✔  0/1 succeeded [0 read from cache]

Failure internal project:

> nx run website:build:production

   ▲ Next.js 14.1.3

   Skipping linting
 ✓ Checking validity of types    
   Creating an optimized production build ...
<w> [webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item 'Compilation/modules|/Users/xxx/node_modules/@svgr/webpack/dist/index.js!/Users/xxx/apps/website/website/public/imgv2/logos/asic-logo-grey.svg': No serializer registered for SvgoParserError
<w> while serializing webpack/lib/cache/PackFileCacheStrategy.PackContentItems -> webpack/lib/NormalModule -> webpack/lib/ModuleBuildError -> SvgoParserError
<w> [webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item 'Compilation/modules|/Users/xxx/node_modules/@svgr/webpack/dist/index.js!/Users/xxx/apps/website/website/public/imgv2/logos/asic-logo.svg': No serializer registered for SvgoParserError
<w> while serializing webpack/lib/cache/PackFileCacheStrategy.PackContentItems -> webpack/lib/NormalModule -> webpack/lib/ModuleBuildError -> SvgoParserError
Failed to compile.

./public/imgv2/logos/asic-logo-grey.svg
SvgoParserError: /Users/xxx/apps/website/website/public/imgv2/logos/asic-logo-grey.svg:29:147: Invalid character in entity name
    at sax.onerror (/Users/xxx/node_modules/svgo/lib/parser.js:247:19)
    at emit (/Users/xxx/node_modules/@trysound/sax/lib/sax.js:518:35)
    at error (/Users/xxx/node_modules/@trysound/sax/lib/sax.js:549:5)
    at strictFail (/Users/xxx/node_modules/@trysound/sax/lib/sax.js:573:7)
    at SAXParser.write (/Users/xxx/node_modules/@trysound/sax/lib/sax.js:1395:13)
    at parseSvg (/Users/xxx/node_modules/svgo/lib/parser.js:259:7)
    at optimize (/Users/xxx/node_modules/svgo/lib/svgo.js:66:17)
    at Object.optimize (/Users/xxx/node_modules/svgo/lib/svgo-node.js:76:10)
    at svgoPlugin (/Users/xxx/node_modules/@svgr/plugin-svgo/dist/index.js:76:23)
    at run (/Users/xxx/node_modules/@svgr/core/dist/index.js:174:16)
    at Object.transform (/Users/xxx/node_modules/@svgr/core/dist/index.js:180:10)
    at async /Users/xxx/node_modules/@svgr/webpack/dist/index.js:66:20

Import trace for requested module:
./public/imgv2/logos/asic-logo-grey.svg
./pages/key-documents.tsx

./public/imgv2/logos/asic-logo.svg
SvgoParserError: /Users/xxx/apps/website/website/public/imgv2/logos/asic-logo.svg:29:147: Invalid character in entity name
    at sax.onerror (/Users/xxx/node_modules/svgo/lib/parser.js:247:19)
    at emit (/Users/xxx/node_modules/@trysound/sax/lib/sax.js:518:35)
    at error (/Users/xxx/node_modules/@trysound/sax/lib/sax.js:549:5)
    at strictFail (/Users/xxx/node_modules/@trysound/sax/lib/sax.js:573:7)
    at SAXParser.write (/Users/xxx/node_modules/@trysound/sax/lib/sax.js:1395:13)
    at parseSvg (/Users/xxx/node_modules/svgo/lib/parser.js:259:7)
    at optimize (/Users/xxx/node_modules/svgo/lib/svgo.js:66:17)
    at Object.optimize (/Users/xxx/node_modules/svgo/lib/svgo-node.js:76:10)
    at svgoPlugin (/Users/xxx/node_modules/@svgr/plugin-svgo/dist/index.js:76:23)
    at run (/Users/xxx/node_modules/@svgr/core/dist/index.js:174:16)
    at Object.transform (/Users/xxx/node_modules/@svgr/core/dist/index.js:180:10)
    at async /Users/xxx/node_modules/@svgr/webpack/dist/index.js:66:20

Import trace for requested module:
./public/imgv2/logos/asic-logo.svg
./pages/key-documents.tsx


> Build failed because of webpack errors
Error occurred while trying to run the build command
1

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Ran target build for project website (11s)

   ✖  1/1 failed
   ✔  0/1 succeeded [0 read from cache]

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

I generated a default next.js project.
Added a simple SVG file.
Imported it.
Set the configuration as described here: https://nx.dev/recipes/react/adding-assets-react

It does yield another error as the one I have, but it might be related I guess.

Additionaly, The message in nx report

The following packages should match the installed version of nx
  - @nx/next@18.0.5
  - @nrwl/next@18.0.5

To fix this, run `nx migrate nx@18.1.1`

can be ignored I guess, as I tried downgrading till I went back to the last working version and then worked my way up to see which version started failing.

@Mystilund
Copy link

By migrating from the v17 to v18 of NX I got also an error to use ReactComponent from svg imports. The workaround is to set/override with the right rules. You can do it in the next.config.js file, in the nextConfig you can add such thing :

webpack: (config) => {
    config.module.rules = config.module.rules.filter((rule) => {
      return !rule.test?.toString().includes('.svg');
    });

    config.module.rules.push({
      test: /\.svg$/i,
      issuer: /\.(js|ts|md)x?$/,
      use: [
        {
          loader: '@svgr/webpack',
          options: { svgo: false, titleProp: true, ref: true },
        },
        {
          loader: 'file-loader',
          options: { name: '[name].[hash].[ext]' },
        },
      ],
    });

    return config;
  }

First it will remove the existing rules for svg, then set the right rules to import as before. It requires the packages svgr and file-loader to be installed ofc
Probably the default webpack config changed at some points 🤔

@FrozenPandaz FrozenPandaz added the scope: nextjs Issues related to NextJS support for Nx label Mar 18, 2024
@shahkeyur
Copy link

Having same issue. I had to downgrade to 18.0.4.

@shahkeyur
Copy link

Seems something was added in 18.0.5 related to svg, #21761.

Pretty sure, that's causing it to break, as I modified my with-nx.js locally to what was there previously. It works fine.

@stephenwade
Copy link
Contributor

By migrating from the v17 to v18 of NX I got also an error to use ReactComponent from svg imports. The workaround is to set/override with the right rules. You can do it in the next.config.js file, in the nextConfig you can add such thing :

You can do the same thing without the deprecated file-loader by setting the exportType option to 'named'.

  webpack: (config) => {
    config.module.rules = config.module.rules.filter((rule) => {
      return !rule.test?.toString().includes('.svg');
    });

    config.module.rules.push({
      test: /\.svg$/i,
      issuer: /\.(js|ts|md)x?$/,
      use: [
        {
          loader: '@svgr/webpack',
          options: { svgo: false, titleProp: true, ref: true, exportType: 'named' },
        },
      ],
    });

    return config;
  }

@AlexPshul
Copy link
Contributor

I have the same issue. Spent the entire day trying to figure out what's wrong.
Even reproduced it on a clean environment with an empty React app that has ONLY an import to an SVG file and it fails with the same error.

Just now decided to check the open issues on GitHub.
Thanks @stephenwade for the workaround. Works great!

@BogdanMihalca
Copy link

BogdanMihalca commented Mar 27, 2024

I have the same issue:
I use both import types (URL and RectComponent) but in this case in a react lib.
As per my understanding, there's no way to configure the webpack in the lib, is there another way?

For me, the issue appears when upgrading from v17.3.2 to v18.0

WARNING in....
export 'ReactComponent' (imported as 'LogoImage') was not found in '../../images........'

@lytvynenko
Copy link

ran in the issue while upgrading to nx 18. Solved using @stephenwade snippet + react-svgr doco - https://react-svgr.com/docs/next/

added @svgr/webpack as dev. dependency and then modified next.config.js

 // first - disable nx svgr
 //.....
 "nx": {"svgr": false},
 //.....
 // then override the webpack rules
  webpack: (config, { webpack, isServer, nextRuntime }) => {

    const fileLoaderRule = config.module.rules.find((rule) =>
      rule.test?.test?.('.svg'),
    )
    
    config.module.rules.push(
      // Reapply the existing rule, but only for svg imports ending in ?url
      {
        ...fileLoaderRule,
        test: /\.svg$/i,
        resourceQuery: /url/, // *.svg?url
      },
      // Convert all other *.svg imports to React components
      {
        test: /\.svg$/i,
        issuer: fileLoaderRule.issuer,
        resourceQuery: { not: [...fileLoaderRule.resourceQuery.not, /url/] }, // exclude if *.svg?url
        use: [{
          loader: '@svgr/webpack',
          options: { svgo: false, titleProp: true, ref: true, exportType: 'named' },
        },],
      },
    )

    // Modify the file loader rule to ignore *.svg, since we have it handled now.
    fileLoaderRule.exclude = /\.svg$/i
   return config;
   }

@jaysoo
Copy link
Member

jaysoo commented Apr 3, 2024

This has been fixed here and is in the latest canary (18.3.0-canary.20240403-270788e).

A patch version will be released this week.

@jaysoo jaysoo closed this as completed Apr 3, 2024
Copy link

github-actions bot commented May 4, 2024

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: nextjs Issues related to NextJS support for Nx type: bug
Projects
None yet
Development

No branches or pull requests

10 participants