ionic-framework: bug: angular, cannot read property of 'whenDefined' with ssr
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already report this problem, without success.
Ionic Framework Version
- v4.x
- v5.x
- v6.x
Current Behavior
Folks, I’m using the brand new Ionic 6, trying to integrate it with Angular Universal through NestJs. It was working pretty well, until I noted that I was getting this error when running using server side rendering:
Menu: must have a "content" element to listen for drag events on.
After some time, I noticed that the <ion-menu-button> wasn’t working. I tried using it programmatically using the MenuController, but it also didn’t work.
I run the code without SSR and the button was working properly. So I went out to find out what was the issue and I found this issue (https://github.com/ionic-team/ionic-framework/issues/21918#issue-678980726) where the solution apparently was to add @ionic/angular-server to my Angular AppModule. I did so, but now every time I try to access Angular’s pages, I’m getting this error (in server console):
ReferenceError: Cannot access 'ActionSheet' before initialization
and this one (browser console):
Uncaught SyntaxError: Strict mode code may not include a with statement
I tried to search for those issues but sadly I couldn’t find much related.
If I remove IonicServerModule from AppModule the code runs, but the button naturally still doesn’t work.
I’m not sure why this is happening. Is there any error with the Ionic version I’m using? Any substantial package that I’m missing?
From the SSR standpoint I understand that menu’s contentId looks for its sibling with same id as it, and since that document is not available in that context, in fact, the menu won’t have any content at all. But in browser, that doesn’t make much sense. I also tried to conditionally render <ion-menu> tags for platform browsers. The Menu: must have a "content" element to listen for drag events on. error disappear, but still <ion-menu-button> isn’t working.
Any insights on this matter?
I really appreciate it, thanks in advance.
Expected Behavior
The menu button should work after the implementation of IonicServerModule.
Steps to Reproduce
For reproducing issue with IonicServerModule
- Download repo from code reproduction URL and Install dependencies;
- Try running
npm run dev:ssrornpm run serve:ssr; - Check logs on console when trying to access
localhost:4200.
For reproducing issue with <ion-menu-button> not working / Menu: must have a “content” element to listen for drag events on.:
- Download repo from code reproduction URL;
- Remove
IonicServerModulefrom AppModule (src/app/app.module.ts); - Run
npm run dev:ssrornpm run serve:ssr; - Access localhost:4200/home;
- Resize screen to <768px;
- Click on hamburger icon at top right corner of the screen.
Code Reproduction URL
https://github.com/vitordhers/confraria
Ionic Info
Ionic:
Ionic CLI : 6.18.1 (/usr/lib/node_modules/@ionic/cli) Ionic Framework : @ionic/angular 6.0.5 @angular-devkit/build-angular : 13.1.4 @angular-devkit/schematics : 13.1.4 @angular/cli : 13.1.4 @ionic/angular-toolkit : 4.0.0
Utility:
cordova-res : not installed globally native-run (update available: 1.5.0) : 1.4.1
System:
NodeJS : v16.13.2 (/home/dina/.nvm/versions/node/v16.13.2/bin/node) npm : 8.1.2 OS : Linux 5.13
Additional Information
Content related with cannot access "action-sheet" before initialization:
https://github.com/ionic-team/ionic-framework/issues/24448
Content related with Uncaught SyntaxError: Strict mode code may not include a with statement:
https://stackoverflow.com/questions/60114758/uncaught-syntaxerror-strict-mode-code-may-not-include-a-with-statement
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 16 (6 by maintainers)
Commits related to this issue
- fix(menu, split-pane): ssr does not fail on null customElements check (#26854) resolves #24714 — committed to ionic-team/ionic-framework by liamdebeasi a year ago
Any updates?
Hi @liamdebeasi are there any work in progress on this issue? Or maybe an idea on how it should be fixed?
It is reproducible by following https://ionicframework.com/blog/ssr-with-angular-universal-and-ionic/ but with the
sidemenustarter instead ofblank. So you do:1. Create the project
(you might need to run
npx ionic start ...)2. Enable Ionic SSR
And updating
app.server.module.tswith:3. Run
Build & serve the application with:
(
npm run dev:ssris broken because some of theangular.jsonis missing adevelopmentconfiguration)4. Visit localhost:4000
When you visit localhost with javascript off (on chrome,
Ctrl+Shift+Pand run the “disable javascript” action), you will not see the sidebar. If you enable javascript, you will see the sidebar) The error logged to the console is:I hope this helps 🙂
Hello Liam, thanks again for your support. I’ve cloned my repo (I’ve made for reproduction in my folder, in order to assure it was working, before submitting to you.), tried installing the dev packages, got a dependency error, though (please, find the logs below). I was able to install the dependencies (first the
@ionic/angular, then@ionic/angular-serverwith --force argument). I’ve got some errors, deletednode_modulesandpackage-lock.json, runnpm install again, and in order for it to work I had to use --force argument too. Then it installed correctly, the code run, but so far, I’m still getting this error in browser console:Uncaught SyntaxError: Strict mode code may not include a with statement. I’m also getting this error in server console:TypeError: Cannot read properties of null (reading 'whenDefined')Theion-menu-buttondoesn’t show its icon and also is not working. I’m pretty sure that’s not my code fault, since with regularnpm run startcommand it does.Please let me know if you need anything else from me, and again, thanks for your support.
ps.: I’ve updated the repo with latest changes
---------- npm install full log
Yes this works for my case in a pure stencil-ionic app with
npm install @ionic/core@6.5.7-dev.11677189602.12905db0. Now the ssg works in this case. I do not have any projects with angular and ssg. Thanks!Any idea when this will be resolved? It’s a bummer not to have SSR. Bing apparently doesn’t like rendering SPAs and refuses to index my site