ionic-team / ionic-team/ionic-framework
bug: Ionic component styles do not apply in Next.js application
- Dominant language
- TypeScript
- Stars
- 52.7k
- Forks
- 13.3k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 51
Description
### Prerequisites
- [X] I have read the [Contributing Guidelines](https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#creating-an-issue).
- [X] I agree to follow the [Code of Conduct](https://ionicframework.com/code-of-conduct).
- [X] I have searched for [existing issues](https://github.com/ionic-team/ionic-framework/issues) that already report this problem, without success.
### Ionic Framework Version
- [ ] v4.x
- [ ] v5.x
- [X] v6.x
### Current Behavior
SSR/SSG of Nextjs do not work with ionic/react: "^6.0.0". The version worked well with ionic/react 5.
I have reached out to ionic community in discord and was suggested creating the issue. The styles do not apply the behaviour is odd when we use SSR or SSG. It works well on client side though.
### Expected Behavior
I expect the code to work similar as it worked on version 5.9.3.
All the styles do apply there.
### Steps to Reproduce
1. git clone https://github.com/MarianDabrowski/next-app
2. cd next-app
3. npm i
4. npm run dev
### Code Reproduction URL
https://github.com/MarianDabrowski/next-app
### Ionic Info
[WARN] You are not in an Ionic project directory. Project context may be
missing.
Ionic:
Ionic CLI : 6.19.0
Utility:
cordova-res : 0.15.4
native-run : not installed globally
System:
NodeJS : v17.3.1
npm : 8.5.3
OS : macOS Monterey
### Additional Information
Based on the discussion with @sean-perkins I believe Sean is right saying: "...custom elements should workin SSR/SSG, so the fact that it isn't, is a little concerning. The root issue is likely Stencil, if the styles are not being applied, but I would recommend creating a ticket in ionic-framework, since the UI kit should support Next.js."
_app.tsx is rendered before each page render
As i understand setupIonicReact shall be called at the initial stage.
Altering pages/index.tsx to
```
const Home: NextPage = () => {
const [component, setComponent] = useState(
useEffect(() => {
setTimeout(() => {
setComponent();
}, 3000);
});
return component;
};
```
makes the code work properly
Contributor guide
Research direction
Clone the linked next-app reproduction, install its dependencies, and run npm run dev to compare client-side behavior with SSR or SSG. Inspect _app.tsx and pages/index.tsx, including setupIonicReact and the delayed rendering workaround. Done means Ionic component styles apply during SSR/SSG as they did with ionic/react 5.9.3.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- next.js, react, typescript
- Domain
- frontend, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100