Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prerendering fails with undefined stack trace on invalid http calls #28740

Closed
1 task
thecp opened this issue Oct 29, 2024 · 4 comments
Closed
1 task

Prerendering fails with undefined stack trace on invalid http calls #28740

thecp opened this issue Oct 29, 2024 · 4 comments
Assignees

Comments

@thecp
Copy link

thecp commented Oct 29, 2024

Command

build, serve

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When using SSR, you typically wrap http calls in an afterRender or afterNextRender lifecycle hook. If not doing so it might happen that prerendering fails when e.g. the url is either invalid, unreachable or does not return JSON. In this you'll get an error which doesn't really tell you a lot of context what is going on:

✘ [ERROR] An error occurred while prerendering route '/test'.

undefined

My expectation is that there should be some better error message to tell the user what (s)he is doing wrong.

Minimal Reproduction

  1. Set up a project with SSR and routing enabled
  2. add router-outlet to the app.component
  3. set up some routing, e.g.
  {
    path: 'test',
    component: TestComponent,
  },
  {
    path: '',
    redirectTo: '/test',
    pathMatch: 'full',
  }
  1. In a component of any route (in this case the TestComponent), set up an http call like:
constructor(private http: HttpClient) {
    this.http
      .get('htp://invalid.url')
      .subscribe();
  }
  1. run ng build

Exception or Error

✘ [ERROR] An error occurred while prerendering route '/test'.

undefined

Your Environment

Angular CLI: 18.2.10
Node: 22.9.0
Package Manager: npm 10.8.3
OS: linux x64

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

Package Version

@angular-devkit/architect 0.1802.10
@angular-devkit/build-angular 18.2.10
@angular-devkit/core 18.2.10
@angular-devkit/schematics 18.2.10
@angular/cli 18.2.10
@angular/ssr 18.2.10
@schematics/angular 18.2.10
rxjs 7.8.1
typescript 5.5.4
zone.js 0.14.10

Anything else relevant?

No response

@alan-agius4
Copy link
Collaborator

We should improve the error message although I suspect this has been addressed in v19 which is currently in prerelease. I’ll verify and get back to you on this.

When using SSR, you typically wrap http calls in an afterRender or afterNextRender lifecycle hook.

I haven’t seen usage of this approach and truth this approach would be far from ideal. This could lead to hydration mismatches, it’s best to make HTTP requests directly during SSR, ensuring that the view is fully rendered on the server. This way, when the page hydrates on the client side, it can use the data already fetched by the server, keeping everything in sync.

@alan-agius4
Copy link
Collaborator

Confirmed that this has been addressed in version 19. I'll back port the fix to version 18.

alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Oct 30, 2024
Handle cases where the error stack is missing by displaying a relevant message.

Closes angular#28740
alan-agius4 added a commit that referenced this issue Oct 30, 2024
Handle cases where the error stack is missing by displaying a relevant message.

Closes #28740
@alan-agius4
Copy link
Collaborator

Closed via #28744

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants