angular / angular

Deliver web apps with confidence 🚀
https://angular.dev
MIT License
96.23k stars 25.5k forks source link

Angular is doing httpClinet calls during build #56319

Closed vlio20 closed 5 months ago

vlio20 commented 5 months ago

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

Yes

Description

I have an ssr angular application that I upgraded from ng17 to ng18. When my app is being built it is doing http calls with the httpClinet to my backend.

Please provide a link to a minimal reproduction of the bug

I can't share a link as this app is private

Please provide the exception or error you saw

> ng build --configuration production

⠇ Building...
ERROR TypeError: Failed to parse URL from /api/v1/users/initData
    at new Request (node:internal/deps/undici/undici:5272:19)
    at fetch (node:internal/deps/undici/undici:9508:25)
    ... 5 lines matching cause stack trace ...
    at e.<anonymous> (file:///***/.angular/prerender-root/5481afbb-7a0c-44f6-a9b9-4948f3d1b25a/chunk-KYUJVXDE.mjs:3:13915)
    at Generator.next (<anonymous>)
    at file:///***/.angular/prerender-root/5481afbb-7a0c-44f6-a9b9-4948f3d1b25a/chunk-NDYDZJSS.mjs:2:1601 {
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:405:5)
      at new URL (node:internal/url:676:13)
      at new Request (node:internal/deps/undici/undici:5270:25)
      at fetch (node:internal/deps/undici/undici:9508:25)
      at Object.fetch (node:internal/deps/undici/undici:11728:18)
      at fetch (node:internal/process/pre_execution:281:25)
      at patchedFetch (/***/node_modules/@angular/build/src/utils/server-rendering/fetch-patch.js:43:20)
      at file:///***/.angular/prerender-root/5481afbb-7a0c-44f6-a9b9-4948f3d1b25a/polyfills.server.mjs:4:3273
      at l.<computed> (file:///***/.angular/prerender-root/5481afbb-7a0c-44f6-a9b9-4948f3d1b25a/polyfills.server.mjs:3:12959)
      at e.<anonymous> (file:///***/.angular/prerender-root/5481afbb-7a0c-44f6-a9b9-4948f3d1b25a/chunk-KYUJVXDE.mjs:3:13915) {
    input: '/api/v1/users/initData',
    code: 'ERR_INVALID_URL'
  }
}
ie {
  headers: t {
    normalizedNames: Map(0) {},
    lazyUpdate: null,
    headers: Map(0) {}
  },
  status: 0,
  statusText: 'Unknown Error',
  url: '/api/v1/users/initData',
  ok: false,
  name: 'HttpErrorResponse',
  message: 'Http failure response for /api/v1/users/initData: 0 undefined',
  error: TypeError: Failed to parse URL from /api/v1/users/initData
      at Object.fetch (node:internal/deps/undici/undici:11730:11) {
    [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
        at new NodeError (node:internal/errors:405:5)
        at new URL (node:internal/url:676:13)
        at new Request (node:internal/deps/undici/undici:5270:25)
        at fetch (node:internal/deps/undici/undici:9508:25)
        at Object.fetch (node:internal/deps/undici/undici:11728:18)
        at fetch (node:internal/process/pre_execution:281:25)
        at patchedFetch (/***/node_modules/@angular/build/src/utils/server-rendering/fetch-patch.js:43:20)
        at file:///***/.angular/prerender-root/5481afbb-7a0c-44f6-a9b9-4948f3d1b25a/polyfills.server.mjs:4:3273
        at l.<computed> (file:///***/.angular/prerender-root/5481afbb-7a0c-44f6-a9b9-4948f3d1b25a/polyfills.server.mjs:3:12959)
        at e.<anonymous> (file:///***/.angular/prerender-root/5481afbb-7a0c-44f6-a9b9-4948f3d1b25a/chunk-KYUJVXDE.mjs:3:13915) {
      input: '/api/v1/users/initData',
      code: 'ERR_INVALID_URL'
    }
  }
⠹ Building...
ERROR TypeError: Failed to parse URL from /api/v1/users/initData
    at new Request (node:internal/deps/undici/undici:5272:19)
    at fetch (node:internal/deps/undici/undici:9508:25)
    ... 5 lines matching cause stack trace ...
    at e.<anonymous> (file:///***/.angular/prerender-root/4d325ab2-0412-4cd8-96ae-1d1c5dfa978b/chunk-KYUJVXDE.mjs:3:13915)
    at Generator.next (<anonymous>)
    at file:///***/.angular/prerender-root/4d325ab2-0412-4cd8-96ae-1d1c5dfa978b/chunk-NDYDZJSS.mjs:2:1601 {
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:405:5)
      at new URL (node:internal/url:676:13)
      at new Request (node:internal/deps/undici/undici:5270:25)
      at fetch (node:internal/deps/undici/undici:9508:25)
      at Object.fetch (node:internal/deps/undici/undici:11728:18)
      at fetch (node:internal/process/pre_execution:281:25)
      at patchedFetch (/***/node_modules/@angular/build/src/utils/server-rendering/fetch-patch.js:43:20)
      at file:///***/.angular/prerender-root/4d325ab2-0412-4cd8-96ae-1d1c5dfa978b/polyfills.server.mjs:4:3273
      at l.<computed> (file:///***/.angular/prerender-root/4d325ab2-0412-4cd8-96ae-1d1c5dfa978b/polyfills.server.mjs:3:12959)
      at e.<anonymous> (file:///***/.angular/prerender-root/4d325ab2-0412-4cd8-96ae-1d1c5dfa978b/chunk-KYUJVXDE.mjs:3:13915) {
    input: '/api/v1/users/initData',
    code: 'ERR_INVALID_URL'
  }
}

### Please provide the environment you discovered this bug in (run `ng version`)

```true
Angular CLI: 18.0.2
Node: 18.19.0
Package Manager: npm 10.2.3
OS: darwin arm64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.2
@angular-devkit/build-angular   18.0.2
@angular-devkit/core            18.0.2
@angular-devkit/schematics      18.0.2
@angular/cli                    18.0.2
@angular/ssr                    18.0.2
@schematics/angular             18.0.2
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.6

Anything else?

Note that though I have those errors during the build, the build is eventually successful. The problem is that I want to create a cookie interceptor and then the build hangs. Please see interceptor and the error:

export function cookieInterceptor(req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
  const platformId = inject(PLATFORM_ID);

  if (isPlatformBrowser(platformId)) {
    return next(req);
  }

  const reqCopy = req.clone({ headers: req.headers.append('Cookie', req.headers.get('cookie')!) });

  return next(reqCopy);
}

Error:

⠧ Building...
TypeError: Cannot read properties of null (reading 'length')
    at t.applyUpdate (file:///***/.angular/prerender-root/8d95606a-5d39-407c-b309-3cb6235c2a38/chunk-KYUJVXDE.mjs:3:1611)
    at file:///***/.angular/prerender-root/8d95606a-5d39-407c-b309-3cb6235c2a38/chunk-KYUJVXDE.mjs:3:1151
    at Array.forEach (<anonymous>)
    at t.init (file:///****.angular/prerender-root/8d95606a-5d39-407c-b309-3cb6235c2a38/chunk-KYUJVXDE.mjs:3:1135)
    at t.has (file://****/.angular/prerender-root/8d95606a-5d39-407c-b309-3cb6235c2a38/chunk-KYUJVXDE.mjs:3:470)
    at Ro (file://****/.angular/prerender-root/8d95606a-5d39-407c-b309-3cb6235c2a38/chunk-KYUJVXDE.mjs:3:22739)
    at wo (file://****/.angular/prerender-root/8d95606a-5d39-407c-b309-3cb6235c2a38/chunk-KYUJVXDE.mjs:3:22066)
    at file://****/.angular/prerender-root/8d95606a-5d39-407c-b309-3cb6235c2a38/chunk-KYUJVXDE.mjs:3:16133
    at Cd (file://****/.angular/prerender-root/8d95606a-5d39-407c-b309-3cb6235c2a38/chunk-4VKW4VRS.mjs:8:13005)
⠇ Building...
JeanMeche commented 5 months ago

You likely have prerendering enabled in you angular.json.

vlio20 commented 5 months ago

Looking how to turn it off. This wasn't the default with ng17?

vlio20 commented 5 months ago

Is this the right way to turn it off? I followed this doc: https://angular.dev/guide/prerendering#build-options-for-prerender

"prerender": {
              "discoverRoutes": false
            }
JeanMeche commented 5 months ago

It's "prerender": false.

vlio20 commented 5 months ago

Thanks for the quick reply! Really appreciated!

angular-automatic-lock-bot[bot] commented 4 months ago

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.