analog icon indicating copy to clipboard operation
analog copied to clipboard

bug(ssr): API calls return Http failure response

Open eduardoRoth opened this issue 1 year ago • 4 comments

Please provide the environment you discovered this bug in.

AnalogJS v1.5.0

Which area/package is the issue in?

Don't know / other

Description

Repo: https://stackblitz.com/edit/github-fpzype?file=src%2Fapp%2Fpages%2Findex.page.ts

First API call hits without problem, second complains during SSR.

Please provide the exception or error you saw

ERROR HttpErrorResponse {
  headers: HttpHeaders {
    normalizedNames: Map(0) {},
    lazyUpdate: null,
    headers: Map(0) {}
  },
  status: 0,
  statusText: 'Unknown Error',
  url: 'api/v1/stack',
  ok: false,
  name: 'HttpErrorResponse',
  message: 'Http failure response for api/v1/stack: 0 undefined',
  error: TypeError: Failed to parse URL from api/v1/stack
      at node:internal/deps/undici/undici:12502:13
      at _ZoneDelegate.invoke (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/zone.js/fesm2015/zone-node.js:365:28)
      at Object.onInvoke (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/@angular/core/fesm2022/core.mjs:16129:33)
      at _ZoneDelegate.invoke (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/zone.js/fesm2015/zone-node.js:364:34)
      at ZoneImpl.run (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/zone.js/fesm2015/zone-node.js:111:43)
      at /Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/zone.js/fesm2015/zone-node.js:1192:40
      at _ZoneDelegate.invokeTask (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/zone.js/fesm2015/zone-node.js:398:33)
      at eval (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/@angular/core/fesm2022/core.mjs:15814:55)
      at AsyncStackTaggingZoneSpec.onInvokeTask (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/@angular/core/fesm2022/core.mjs:15814:36)
      at _ZoneDelegate.invokeTask (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/zone.js/fesm2015/zone-node.js:397:38) {
    [cause]: TypeError: Invalid URL
        at new URL (node:internal/url:814:29)
        at new Request (node:internal/deps/undici/undici:4853:25)
        at fetch (node:internal/deps/undici/undici:9662:25)
        at fetch (node:internal/deps/undici/undici:12500:10)
        at value (node:internal/bootstrap/web/exposed-window-or-worker:72:12)
        at /Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/zone.js/fesm2015/zone-node.js:1443:40
        at proto.<computed> (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/zone.js/fesm2015/zone-node.js:890:24)
        at FetchBackend.doRequest (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/@angular/common/fesm2022/http.mjs:1514:39)
        at Observable.eval [as _subscribe] (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/@angular/common/fesm2022/http.mjs:1506:18)
        at Observable._trySubscribe (/Users/eduardoroth/webapps/analog-ionic-capacitor-template/node_modules/rxjs/dist/cjs/internal/Observable.js:41:25) {
      code: 'ERR_INVALID_URL',
      input: 'api/v1/stack'
    }
  }
}

Other information

No response

I would be willing to submit a PR to fix this issue

  • [X] Yes
  • [ ] No

eduardoRoth avatar Jun 29 '24 03:06 eduardoRoth

Am I missing some server side base URL setting?

eduardoRoth avatar Jun 29 '24 04:06 eduardoRoth

Currently, the full URL including the base URL is required when doing HTTP requests. In the repo, the second URL is /api/v1/hello compared to the first which is http://localhost:5173/api/v1/hello

brandonroberts avatar Jun 30 '24 18:06 brandonroberts

Thanks @brandonroberts , I'm asking due to the second url (/api/v1/hello) working on the client side while not working on the server side (while being rendered).

Would it be feasible to include a base url to allow having urls like /api/v1/hello in the server side?

eduardoRoth avatar Jul 01 '24 15:07 eduardoRoth

We are looking at a solution for that here https://github.com/analogjs/analog/issues/995#issuecomment-2168514614

brandonroberts avatar Jul 01 '24 21:07 brandonroberts