You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Angular 15 SSR and Lazy-Loading module not loading state on server #3061
Recently we started changing our architecture from Angular SSR to use SSR with Lazy Loading modules.
In the first versions the modules were all imported on our routes file.
A component, for instance, would load data from our API and if running at the server would add data to the transferState. At the browser, we would check if the state existed, if not, load from the backend.
Now our components are lazy-loaded in different modules:
App Module lazy-loads A module with A component
A Module lazy-loads B module with B component
before B component would load data as stated before (with transferState), but the behavior now is to load only on the client side.
Did we get it wrong that it would be possible to continue using server-side loading, or something was implemented wrongly?
@guiligan, without a reproduction it’s hard to tell what is happened. A good way to make a minimal repro is to create a new app via ng new repro-app and adding the minimum possible code to show the problem. Then you can push this repository to github and link it here.
This might be related to your directory structure so its really important to get an accurate repro to diagnose this.
Not really sure why you are adding localhost/ to the baseUrl and url but this breaks the Angular router as it cannot locate the right routes since localhost/b-module does not exist.
@alan-agius4 sorry about that, we are using a custom matcher for multiple domain serving. I have managed to make it work on the repo, but don't know why it does not work in the full project. Are there any circumstances that make it not load the module for pre-rendering on server? It seems it never gets to the second level router
@alan-agius4 any known issue with SSR and MatMenuModule? Found out that the modules that are not running on server-side are those that have Angular Material Menu Module imported
@alan-agius4 strange, as soon as I import them, it stop working. Will try to debug on a separate/cleaner project to understand and if so, I open a bug! 💪🏽
@alan-agius4 , just did a quick test using the clean repo stated before. I have removed the unused code that you pointed before.
Upon opening http://localhost:4200 it will lazy load AModule, display AComponent, lazy load BModule and display BComponent.
Activity
alan-agius4 commentedon Mar 29, 2023
Are you using the
HTTPClientandTransferHttpCacheModule?guiligan commentedon Mar 29, 2023
hey @alan-agius4 , we are using
HTTPClient, but notTransferHttpCacheModule. However, we don't see the request being made on server-sidealan-agius4 commentedon Mar 29, 2023
@guiligan, without a reproduction it’s hard to tell what is happened. A good way to make a minimal repro is to create a new app via
ng new repro-appand adding the minimum possible code to show the problem. Then you can push this repository to github and link it here.This might be related to your directory structure so its really important to get an accurate repro to diagnose this.
guiligan commentedon Mar 29, 2023
@alan-agius4 I have added everything in this repo
Run using
npm run dev:ssrLoading any of the 3 routes:
/a(App -> Module A -> Module B)/b-module(App -> Module B)/b-component(App -> Component B)will result in not setting the state key and showing default message
alan-agius4 commentedon Mar 30, 2023
What is happening here is actually that the page is not being server rendered.
The culprit code seems to be
https://github.com/guiligan/repro-app-3061/blob/6a545de42cf6621561d03a856094c09b78c57d9f/server.ts#L36-L44
Not really sure why you are adding
localhost/to thebaseUrlandurlbut this breaks the Angular router as it cannot locate the right routes sincelocalhost/b-moduledoes not exist.guiligan commentedon Mar 30, 2023
@alan-agius4 sorry about that, we are using a custom matcher for multiple domain serving. I have managed to make it work on the repo, but don't know why it does not work in the full project. Are there any circumstances that make it not load the module for pre-rendering on server? It seems it never gets to the second level router
alan-agius4 commentedon Mar 30, 2023
Typically misconfiguration of the router APP_BASE_HREF and/or incorrect request parameters
guiligan commentedon Mar 30, 2023
@alan-agius4 any known issue with SSR and
MatMenuModule? Found out that the modules that are not running on server-side are those that have Angular Material Menu Module importedguiligan commentedon Mar 31, 2023
Same happens when
MatInputModuleis importedalan-agius4 commentedon Mar 31, 2023
No, they should work. At least no one reported such issues.
guiligan commentedon Mar 31, 2023
@alan-agius4 strange, as soon as I import them, it stop working. Will try to debug on a separate/cleaner project to understand and if so, I open a bug! 💪🏽
guiligan commentedon Mar 31, 2023
@alan-agius4 , just did a quick test using the clean repo stated before. I have removed the unused code that you pointed before.
Upon opening
http://localhost:4200it will lazy loadAModule, displayAComponent, lazy loadBModuleand displayBComponent.In
BComponent, if it runs in the server, will set and pass the stateKey to the browser (https://github.com/guiligan/repro-app-3061/blob/main/src/app/B/b.component.ts#L22-L26) with the value<state key set in server>.It works fine:
However, if I import
MatInputModule, like here, it will stop working and display the default state of<no state>Only two changes from before (not the same environment from original issue):
server.tsto its defaultMaterial modules found so far that breaks server-side
alan-agius4 commentedon Mar 31, 2023
Let me take a look at this on Monday.
alan-agius4 commentedon Apr 3, 2023
The problem is https://github.com/guiligan/repro-app-3061/blob/f15cea8858754d45aeba3eb7c2077aaa2bf219a6/server.ts#L9
guiligan commentedon Apr 3, 2023
@alan-agius4 great, thank you very much for the help, was able to make everything work here.
angular-automatic-lock-bot commentedon May 4, 2023
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.