-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
bug: Ionic component styles do not apply in Next.js application #25100
Comments
Context from my discovery & our discord conversation: The web components are being defined/registered/detected correctly when served through Next.js. The The fact that it worked < v6, suggests it is an issue with the custom elements build. I've confirmed that nothing in the Next.js project configuration points at an issue with the reproduction app, so my thoughts are that:
|
Hi there! Any updates on this topic? |
This is also happening with Ionic Core loaded from a CDN, so possibly related: #25398 |
Upon further discovery, this only effects components that have multiple stylesheets. For instance: <IonText color="danger">Hello world</IonText> Renders correctly, since it only has one stylesheet, regardless of the mode. Components that have stylesheets per-mode, seem to not create the constructed stylesheet correctly. I believe the source of this issue likely resides from Stencil's internals. Edit: Also confirmed that with a brand new Stencil component library making use of multiple stylesheets, it is not a problem within Next.js. This appears to be isolated to Ionic Framework. |
Does the Stencil component library use the custom elements bundle or lazy loaded bundle? |
Custom elements bundle, doing something similar to: const App: React.FC = () => {
useEffect(() => {
import("stencil-nextjs/dist/components/my-component.js").then((x) => {
x.defineCustomElement();
});
}, []);
return (
<my-component first="Sean" last="Perkins" mode="md"></my-component>
);
}; Compared with setting the mode explicitly for our components, but still does not render with the stylesheet. I am curious if our logic to return early if window is not defined in initialize is related: https://github.com/ionic-team/ionic-framework/blob/main/core/src/global/ionic-global.ts#L17-L19 |
I love to see some updates on this topic! Please tell me, if there are any tasks that I can help you with. |
@sean-perkins I wonder if we could make it so that the in-memory config is setup even if |
Something like this: const configObj = {
persistConfig: false,
...userConfig,
};
config.reset(configObj);
if (config.getBoolean('persistConfig')) {
saveConfig(win, configObj);
}
// we can't call || isPlatform(win, 'ios') ? 'ios' : 'md' here because `window`
// is not necessarily available. Maybe we can re-update `defaultMode` in the
// if block below?
defaultMode = config.get(
'mode',
doc.documentElement.getAttribute('mode')
);
if (typeof window !== 'undefined') {
// everything else
} |
@liamdebeasi as I understand, the consequences of this issue is that no meter the framework we cannot use SSR/SSG with ionic 6. Is it so? |
Have a similar issue. Any progress? @liamdebeasi @sean-perkins |
@liamdebeasi I wonder if you can elaborate on this, I tried to build it locally, but I failed to understand what changes are necessary. |
The main issue is that when setting up the config we return early if no This means that the in-memory config object is never created. As a result, certain things like the default mode are not defined. The in-memory config should be usable even if |
@liamdebeasi thank you. for the response it has helped me much. I have played with the order and guess the one below may solve the issue.
Are there any docs that describe how to build ionic so that I can check if it works with nextjs SSR/SSG? |
@MarianDabrowski our contributing guide includes steps for building both You will want to:
|
@sean-perkins thank you! I will do it! |
I have just tested the proposed idea and unfortunately the app does not work as expected. I have followed the steps to get the package (during npm i have used |
The problem appears to be that the Taking Editing the file and then saving it "fixes" the issue as the styles get loaded. |
There are a couple problems here:
While this prevents the config from being setup, it also prevents a critical piece of Stencil's "mode" infrastructure from being initialized:
This function is what automatically determines which mode to apply to the components. Since no mode could be determined, no stylesheets are loaded for components with mode-specific stylesheets. (See #25100 (comment))
This code reproduces the issue in Next.js: import { setupIonicReact, IonApp, IonButton } from "@ionic/react";
setupIonicReact();
const App: React.FC = () => (
<IonApp>
<IonButton color="danger">Cick me</IonButton>
</IonApp>
);
export default App; If you were to place the same code in a Create React App/Webpack React app, the styles would get loaded because |
Are there any updates? |
Our team is having the same exact issue, but we're trying to integrate our Stencil component library's React wrapper ( We've tried several iterations of Has anyone found any possible solutions or ideas that we can try? |
@romfilippini-gp I believe the issue is related to this ionic-team/stencil-ds-output-targets#323 |
Wow, calling |
@qbx2 Could you please post a sample of how you did it in your project? |
@romfilippini-gp I was trying to setup nextjs+tailwind+ionic+capacitor from scratch. Without |
Any update with Ionic 7? Thanks! |
Prerequisites
Ionic Framework Version
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
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
makes the code work properly
The text was updated successfully, but these errors were encountered: