Repository navigation
Description
🐞 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
- Initialize app via latest Angular CLI
- Add universal
- Add a new route/component. E.g.: a lazy module
dashboard - Call
localStorage.getItemin the constructor of the new component - Call prerender script (
npm run prerender -- --routes /dashboard /) - See
dist/unversal-test/browser/dashboard/index.html, there is content fromsrc/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
- Dependencies
- MacOS Monterey 12.4
Activity
alan-agius4 commentedon Aug 4, 2022
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.
angular/packages/router/src/router.ts
Lines 1398 to 1402 in d583f85
atscott commentedon Aug 4, 2022
@alan-agius4 Can you provide some more details? This will throw an error in the browser. Why is the prerender not throwing?
atscott commentedon Aug 4, 2022
I updated the router bundle in
node_modulesso thescheduleNavigationalways just returnsPromise.reject('lkajsdflkjasdf')and there is no error in the prerender. If I update the function tothrow 'some error', then it does cause the prerender to fail. This seems like an issue outside of the Router code.atscott commentedon Aug 5, 2022
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:
alan-agius4 commentedon Aug 5, 2022
@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
navigateByUrlandscheduleNavigationare 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.alan-agius4 commentedon Aug 5, 2022
@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_modulesdirectly) and did indeed result in the error to bubble up correctly to the prerender worker.alan-agius4 commentedon Apr 3, 2023
This issue surfaced again in angular/universal#3061
alan-agius4 commentedon Apr 18, 2023
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-appto create a new project where you reproduce the problem.angular-automatic-lock-bot commentedon May 19, 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.