I am trying to import an SVG image from file into a Next.js component.
In the assets folder I have google.svg (icon):
JavaScript
x
21
21
1
<svg className="svgIcon-use" width="25" height="37" viewBox="0 0 25 25">
2
<g fill="none" fillRule="evenodd">
3
<path
4
d="M20.66 12.693c0-.603-.054-1.182-.155-1.738H12.5v3.287h4.575a3.91 3.91 0 0 1-1.697 2.566v2.133h2.747c1.608-1.48 2.535-3.65 2.535-6.24z"
5
fill="#4285F4"
6
/>
7
<path
8
d="M12.5 21c2.295 0 4.22-.76 5.625-2.06l-2.747-2.132c-.76.51-1.734.81-2.878.81-2.214 0-4.088-1.494-4.756-3.503h-2.84v2.202A8.498 8.498 0 0 0 12.5 21z"
9
fill="#34A853"
10
/>
11
<path
12
d="M7.744 14.115c-.17-.51-.267-1.055-.267-1.615s.097-1.105.267-1.615V8.683h-2.84A8.488 8.488 0 0 0 4 12.5c0 1.372.328 2.67.904 3.817l2.84-2.202z"
13
fill="#FBBC05"
14
/>
15
<path
16
d="M12.5 7.38c1.248 0 2.368.43 3.25 1.272l2.437-2.438C16.715 4.842 14.79 4 12.5 4a8.497 8.497 0 0 0-7.596 4.683l2.84 2.202c.668-2.01 2.542-3.504 4.756-3.504z"
17
fill="#EA4335"
18
/>
19
</g>
20
</svg>
21
I need to import that SVG inside of this inside of the Next.js component:
JavaScript
1
23
23
1
import googleLogo from '../assets/google.svg';
2
3
class Login extends React.Component {
4
render() {
5
return (
6
<LoginLayout title="Login Page">
7
<div>
8
<Link href="/auth/google">
9
<a className="button">
10
<div>
11
<span className="svgIcon t-popup-svg">
12
{googleLogo} // <---- import here icon
13
</span>
14
15
</div>
16
</a>
17
</Link>
18
</div>
19
</LoginLayout>
20
);
21
}
22
}
23
I installed this package: https://www.npmjs.com/package/next-images
and set configuration based on that documentation in next.config.js
:
JavaScript
1
17
17
1
const withCSS = require('@zeit/next-css');
2
const withSass = require('@zeit/next-sass');
3
const withImages = require('next-images');
4
5
module.exports = withCSS(
6
withSass({
7
webpack: (config) => config,
8
distDir: '../_next'
9
}),
10
withImages({
11
exclude: path.resolve(__dirname, 'client/assets/svg'),
12
webpack(config, options) {
13
return config;
14
}
15
})
16
);
17
Why my import of SVG does not work?
Advertisement
Answer
Another approach would be to make a component for your svg. I like this approach because I can easily pass in props for width, height color etc if required on the svg.
JavaScript
1
27
27
1
//GoogleLogo.js
2
import React from "react";
3
export default function GoogleLogo() {
4
return (
5
<svg className="svgIcon-use" width="25" height="37" viewBox="0 0 25 25">
6
<g fill="none" fillRule="evenodd">
7
<path
8
d="M20.66 12.693c0-.603-.054-1.182-.155-1.738H12.5v3.287h4.575a3.91 3.91 0 0 1-1.697 2.566v2.133h2.747c1.608-1.48 2.535-3.65 2.535-6.24z"
9
fill="#4285F4"
10
/>
11
<path
12
d="M12.5 21c2.295 0 4.22-.76 5.625-2.06l-2.747-2.132c-.76.51-1.734.81-2.878.81-2.214 0-4.088-1.494-4.756-3.503h-2.84v2.202A8.498 8.498 0 0 0 12.5 21z"
13
fill="#34A853"
14
/>
15
<path
16
d="M7.744 14.115c-.17-.51-.267-1.055-.267-1.615s.097-1.105.267-1.615V8.683h-2.84A8.488 8.488 0 0 0 4 12.5c0 1.372.328 2.67.904 3.817l2.84-2.202z"
17
fill="#FBBC05"
18
/>
19
<path
20
d="M12.5 7.38c1.248 0 2.368.43 3.25 1.272l2.437-2.438C16.715 4.842 14.79 4 12.5 4a8.497 8.497 0 0 0-7.596 4.683l2.84 2.202c.668-2.01 2.542-3.504 4.756-3.504z"
21
fill="#EA4335"
22
/>
23
</g>
24
</svg>
25
);
26
}
27
and in your file from above.
JavaScript
1
23
23
1
import GoogleLogo from "./GoogleLogo";
2
3
class Login extends React.Component {
4
render() {
5
return (
6
<LoginLayout title="Login Page">
7
<div>
8
<Link href="/auth/google">
9
<a className="button">
10
<div>
11
<span className="svgIcon t-popup-svg">
12
<GoogleLogo />
13
</span>
14
15
</div>
16
</a>
17
</Link>
18
</div>
19
</LoginLayout>
20
);
21
}
22
}
23