question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Requesting a non-SvelteKit API endpoint on localhost from __layout causes an error

See original GitHub issue

Describe the bug

When I add the following code to my top-level __layout.svelte file, I get a strange error.

<script context="module">
  export async function load({ fetch }) {
    try {
      const response = await fetch("http://localhost:8000/api/calendars");
      return {};
    } catch (err) {
      console.log(err);
      return {
        status: 500,
        error: "Error when talking to the server. Please try again.",
      };
    }
  }
</script>

<main>
  <slot />
</main>

Error:

FetchError: request to http://localhost:8000/api/calendars failed, reason: connect ECONNREFUSED ::1:8000
    at ClientRequest.<anonymous> (file:///Users/kevin/Workspace/sw-repro/node_modules/@sveltejs/kit/dist/install-fetch.js:6246:11)
    at ClientRequest.emit (node:events:390:28)
    at Socket.socketErrorListener (node:_http_client:442:9)
    at Socket.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:164:8)
    at emitErrorCloseNT (node:internal/streams/destroy:129:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  type: 'system',
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  erroredSysCall: 'connect'
}

My backend is running on localhost port 8000, and requests made to http://localhost:8000/api/calendars do work fine; they result in a 200 with some JSON content. No problem whatsoever. It also all works fine when I run my SvelteKit site with npm run dev, I only get the error when I npm run build && npm run preview.

If I change the URL to a non-localhost one (for example https://api.critical-notes.com/api/calendars), then the error doesn’t happen. It also doesn’t happen with a SvelteKit endpoint URL, for example if I add /routes/endpoint.js and change the URL to http://localhost:3000/endpoint, no error.

It only happens when the fetch is made from SSR: if I disable SSR then the error doesn’t happen.

Reproduction

I do have a repo here but it’s not super useful without some kind of external backend running on localhost 😬

Logs

No response

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 2.25 GB / 32.00 GB
    Shell: 3.3.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 17.3.0 - /opt/homebrew/bin/node
    npm: 8.3.0 - /opt/homebrew/bin/npm
  Browsers:
    Edge: 96.0.1054.62
    Safari: 15.2
  npmPackages:
    @sveltejs/adapter-node: next => 1.0.0-next.59
    @sveltejs/kit: next => 1.0.0-next.213
    svelte: ^3.44.0 => 3.44.3

Severity

serious, but I can work around it

Additional Information

npm run dev works and the actual production server works, but it’s now very hard for me to test my SvelteKit site using npm run preview, since the initial page load is always causing an error, which is shown using __error.svelte.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
stalkergcommented, Jan 22, 2022

@bluwy I hope you can close this issue. It’s basically @kevinrenskers misunderstanding what on MacOs the localhost resolved into ::1 by default if application support ipv6. @kevinrenskers is running a separate backend, and it probably does not support ipv6, and it’s not a SvelteKit issue at all.

0reactions
bluwycommented, Jan 8, 2022

Perhaps a misconfiguration in /etc/hosts 🤔I believe the requests was made to localhost by node, but the system (macos) decides that ::1:8000 should respond. Or maybe the backend server should listen on the ipv6 address too. I’m not sure if SvelteKit is the one explicitly requesting ::1:8000 though. These are just my wild guesses though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection is being rejected for localhost · Issue #523 - GitHub
Symptom: Connection is being rejected. The service isn't running on the server, or incorrect proxy settings in vscode, or a firewall is blocking...
Read more >
Any ideas why using localhost instead of 127.0.0.1 would ...
When specifying the strapi api url as "localhost", it throws an error "econnrefused". Yet, when using "127.0.0.1", the error is resolved.
Read more >
Resolve connection issues with API Gateway private ... - AWS
Note: Connection issues might be caused by a misconfigured API Gateway ... If API requests are reaching the endpoint, then an error message ......
Read more >
Retrieving, updating, and deleting an entry using REST API
The entry object will contain field values for all data fields to which the user has permission. Example; Sample Java Code. Request URL....
Read more >
Routing & Input - R Plumber
If no endpoint matches the request, then a 404 Not Found error will be ... These map to the HTTP methods that an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found