Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
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

Closed as not planned

Description

@guiligan

What modules are related to this issue?

  • builders
  • common
  • express-engine

Is this a regression?

No

Description

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?

🌍 Your Environment


Angular CLI: 15.0.4
Node: 18.12.1
Package Manager: npm 8.19.2
OS: darwin arm64

Angular: 15.0.4
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1500.4
@angular-devkit/build-angular   15.0.4
@angular-devkit/core            15.0.4
@angular-devkit/schematics      15.0.4
@angular/cdk                    15.0.3
@angular/material               15.0.3
@nguniversal/builders           15.0.0
@nguniversal/express-engine     15.0.0
@schematics/angular             15.0.4
rxjs                            7.8.0
typescript                      4.8.4

Activity

  1. alan-agius4 commented on Mar 29, 2023

    @alan-agius4
    Collaborator

    Are you using the HTTPClient and TransferHttpCacheModule?

  2. guiligan commented on Mar 29, 2023

    @guiligan
    Author

    hey @alan-agius4 , we are using HTTPClient, but not TransferHttpCacheModule. However, we don't see the request being made on server-side

  3. alan-agius4 commented on Mar 29, 2023

    @alan-agius4
    Collaborator

    @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.

  4. added
    need: repro steps
    on Mar 29, 2023
  5. guiligan commented on Mar 29, 2023

    @guiligan
    Author

    @alan-agius4 I have added everything in this repo

    Run using npm run dev:ssr

    Loading 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

  6. alan-agius4 commented on Mar 30, 2023

    @alan-agius4
    Collaborator

    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 the baseUrl and url but this breaks the Angular router as it cannot locate the right routes since localhost/b-module does not exist.

  7. guiligan commented on Mar 30, 2023

    @guiligan
    Author

    @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

  8. alan-agius4 commented on Mar 30, 2023

    @alan-agius4
    Collaborator

    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

    Typically misconfiguration of the router APP_BASE_HREF and/or incorrect request parameters

  9. guiligan commented on Mar 30, 2023

    @guiligan
    Author

    @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

  10. guiligan commented on Mar 31, 2023

    @guiligan
    Author

    Same happens when MatInputModule is imported

  11. alan-agius4 commented on Mar 31, 2023

    @alan-agius4
    Collaborator

    No, they should work. At least no one reported such issues.

  12. guiligan commented on Mar 31, 2023

    @guiligan
    Author

    @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! 💪🏽

  13. guiligan commented on Mar 31, 2023

    @guiligan
    Author

    @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.

    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:

    image

    However, if I import MatInputModule, like here, it will stop working and display the default state of <no state>

    image

    Only two changes from before (not the same environment from original issue):

    • Return server.ts to its default
    • Installed Angular Material

    Material modules found so far that breaks server-side

    • MatInputModule
    • MatTabsModule
    • MatTooltipModule
    • MatMenuModule
    
    Angular CLI: 15.0.5
    Node: 18.12.1
    Package Manager: npm 8.19.2
    OS: darwin arm64
    
    Angular: 15.0.4
    ... animations, common, compiler, compiler-cli, core, forms
    ... material, platform-browser, platform-browser-dynamic
    ... platform-server, router
    
    Package                         Version
    ---------------------------------------------------------
    @angular-devkit/architect       0.1502.4
    @angular-devkit/build-angular   15.2.4
    @angular-devkit/core            15.2.4
    @angular-devkit/schematics      15.0.5
    @angular/cli                    15.0.5
    @nguniversal/builders           15.0.0
    @nguniversal/express-engine     15.0.0
    @schematics/angular             15.0.5
    rxjs                            7.8.0
    typescript                      4.8.4
    
  14. alan-agius4 commented on Mar 31, 2023

    @alan-agius4
    Collaborator

    Let me take a look at this on Monday.

  15. self-assigned this
    on Mar 31, 2023
  16. removed
    need: repro steps
    on Mar 31, 2023
  17. guiligan commented on Apr 3, 2023

    @guiligan
    Author

    @alan-agius4 great, thank you very much for the help, was able to make everything work here.

  18. angular-automatic-lock-bot commented on May 4, 2023

    @angular-automatic-lock-bot

    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.

  19. locked and limited conversation to collaborators on May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions