Skip to content

Silent fail of preprendering #47039

Closed
Closed

Description

@chernodub

🐞 Bug report

Hi guys, firstly, I wanted to say thanks for your hard work, truly appreciate that 🙌

What modules are related to this issue?

  • builders, but I'm not sure
  • common
  • express-engine

Is this a regression?

Probably, no

Description

So, I was playing around with the prerendering feature, but faced an issue - some of my routes were empty after prerendering script was executed successfully (a pre-rendered page contained only <router-outlet></router-outlet><!----> instead of the component's template).
It seems that I was able to track it down. The issue only appears when I'm using localStorage in the component, but, I guess that could also be affected by the usage of any browser-specific API.

I believe that prerender script should either fail or show some kind of warning in case non-existent API (in the context of server) is used.

🔬 Minimal Reproduction

  1. Initialize app via latest Angular CLI
  2. Add universal
  3. Add a new route/component. E.g.: a lazy module dashboard
  4. Call localStorage.getItem in the constructor of the new component
  5. Call prerender script (npm run prerender -- --routes /dashboard /)
  6. See dist/unversal-test/browser/dashboard/index.html, there is content from src/app/dashboard/dashboard.component.html. And, there are no errors/warnings in the console

Here is the repository where I reproduced the issue, https://github.com/chernodub/ng-universal-reproduced-prerender-issue
This is a breaking change chernodub/ng-universal-reproduced-prerender-issue@656945b, without it, prerender works fine

🔥 Exception or Error

No error or warning, that's the problem. In case the application is somehow complex/advanced it may be very hard to understand, why prerender didn't work :(

🌍 Your Environment

Activity

  1. added
    area: server
    on Aug 4, 2022
  2. added this to the needsTriage milestone on Aug 4, 2022
  3. alan-agius4 commented on Aug 4, 2022

    @alan-agius4
    Contributor

    After looking a bit into this, it appears that the router is not re-throwing the error

    Basically the below is not working as intended.

    // Make sure that the error is propagated even though `processNavigations` catch
    // handler does not rethrow
    return promise.catch((e: any) => {
    return Promise.reject(e);
    });

  4. added and removed
    area: server
    on Aug 4, 2022
  5. atscott commented on Aug 4, 2022

    @atscott
    Contributor

    @alan-agius4 Can you provide some more details? This will throw an error in the browser. Why is the prerender not throwing?

  6. added
    area: server
    and removed on Aug 4, 2022
  7. atscott commented on Aug 4, 2022

    @atscott
    Contributor

    I updated the router bundle in node_modules so the scheduleNavigation always just returns Promise.reject('lkajsdflkjasdf') and there is no error in the prerender. If I update the function to throw 'some error', then it does cause the prerender to fail. This seems like an issue outside of the Router code.

  8. atscott commented on Aug 5, 2022

    @atscott
    Contributor

    I don't know why the rejected promise is getting swallowed during prerendering. You can at least solve this in user-space by always logging the router errors to the console:

    RouterModule.forRoot(routes, {
        initialNavigation: 'enabledBlocking',
        errorHandler: (e) => {
          console.error(e);
          throw e;
        }
    }
    
  9. alan-agius4 commented on Aug 5, 2022

    @alan-agius4
    Contributor

    @atscott, while the above is a workaround, it's far from ideal and it doesn't solve the root cause and would require a migration to update all users that are using universal.

    One thing that caught my eye is that in the router that are a number of floating promises.

    Example calls for navigateByUrl and scheduleNavigation are always never awaited. Which while I am not sure if they are the root cause they do rise potential problems.

    This does bring the question if we should enable the lint rule no-floating-promises.

  10. alan-agius4 commented on Aug 5, 2022

    @alan-agius4
    Contributor

    @atscott, I debugged this a bit more and as suspected this is indeed caused by the floating promises.

    I tried to remove a number of floating promises (By patching the node_modules directly) and did indeed result in the error to bubble up correctly to the prerender worker.

  11. added
    P3
    on Nov 16, 2022
  12. modified the milestones: needsTriage, Backlog on Nov 16, 2022
  13. alan-agius4 commented on Apr 3, 2023

    @alan-agius4
    Contributor

    This issue surfaced again in angular/universal#3061

  14. alan-agius4 commented on Apr 18, 2023

    @alan-agius4
    Contributor

    This should no longer be an issue in version 16 which is currently in pre-release.

    If the problem persists in your application after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior. You can use ng new repro-app to create a new project where you reproduce the problem.

  15. angular-automatic-lock-bot commented on May 19, 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.

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

Metadata

Metadata

Labels

P3area: routerarea: server

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions