bug: @ionic/react doesn't work [great] with Next.js
See original GitHub issueBug Report
Versions:
@ionic/react
: 4.11.5next
: 9.1.4
Current behavior:
Out-of-the-box error :
./node_modules/ionicons/dist/ionicons/svg/ios-switch.svg 1:0
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 xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><circle cx="144" cy="368" r="42"/><path d="M367.5 272h-223C91.2 272 48 315.2 48 368.5S91.2 464 144.5 464h223c53.3 0 96.5-42.2 96.5-95.5S420.8 272 367.5 272zM144 432c-35.3 0-64-28.7-64-64s28.7-64 64-64 64 28.7 64 64-28.7 64-64 64z"/><circle cx="368" cy="144" r="42"/><path d="M144.5 240h223c53.3 0 96.5-42.2 96.5-95.5S420.8 48 367.5 48h-223C91.2 48 48 91.2 48 144.5S91.2 240 144.5 240zM368 80c35.3 0 64 28.7 64 64s-28.7 64-64 64-64-28.7-64-64 28.7-64 64-64z"/></svg>
After adding an SVG loader (next-optimized-images
) :
ReferenceError: window is not defined
at Object.<anonymous> (/home/***/front-next/node_modules/ionicons/dist/cjs/chunk-01d5c78f.js:24:13)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/***/front-next/node_modules/ionicons/dist/cjs/index.cjs.js:5:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
Updating webpack’s config with config.output.globalObject = 'this'
doesn’t change anything (if I get it well that’s because the file that is causing the problem was created by yarn at yarn add @ionic/react
time and not at my app’s build time) :
ReferenceError: window is not defined
# still same file
at Object.<anonymous> (/home/***/front-next/node_modules/ionicons/dist/cjs/chunk-01d5c78f.js:24:13)
...
Expected behavior:
I expected to be able to import Ionic’s React components in a Next.js app without build errors :
import {IonHeader} from '@ionic/react';
export default () => (
<IonHeader>
</IonHeader>
);
Steps to reproduce:
- Create a Next.js app (with TypeScript in my case)
- Add
@ionic/react
as a dependency - Import a Ionic React component like
IonHeader
and use it
Ionic info: Not using Ionic CLI.
Workaround:
Works when dynamically importing the component but not very handy, no SSR and no tree-shaking (I guess? cause the page’s size went from 2.4MB to 4.4MB) :
// import {IonHeader} from '@ionic/react';
import dynamic from 'next/dynamic';
const IonHeader = dynamic(
async () => {
const mod = await import('@ionic/react');
return mod.IonHeader;
},
{ ssr: false } // this is what makes it work
);
export default () => (
<IonHeader>
</IonHeader>
);
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:12 (2 by maintainers)
Top Results From Across the Web
Ionic-react - so many render issues related to routing
This is a very general question, but I'm wondering if there's someone actively working on the Ionic-React library?
Read more >Building a Native Mobile App with Next.js and Capacitor
In this tutorial we will start with a new Next.js app and move into native land using Capacitor and eventually also add Ionic...
Read more >NextJS Mobile App with Capacitor? - YouTube
Let's find out if we can convert a NextJS React application to a mobile app with Capacitor! Learn Ionic faster with...
Read more >Building a Native Mobile App with Next.js and Capacitor
If you are using Next. js you can easily add Capacitor to your project and build a native mobile app from ... The...
Read more >Ionic + React - Tutorial for Beginners - YouTube
Build cross-platform applications with React. js and the Ionic framework.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ionic React support for NextJS is in active development. Expect to hear more on it soon.
Any updates on this Ionic + Next.js integration, please?