Skip to content

Cannot use relative URL with global fetch in .svelte file #8536

@kegesch

Description

@kegesch

Describe the bug

According to the documentation, I should be able to create an endpoint in a +server.js file and call that endpoint with a relative url via fetch from +page.svelte (https://kit.svelte.dev/docs/routing#server-receiving-data).

This is not possible anymore. I get an error message that was introduced with #8370 :

Cannot use relative URL (<relative-url>) with global fetch — use `event.fetch` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis

Hower, the referenced documentation does not even mention event.fetch and also does not explain why this is not possible in .svelte files.

Reproduction

The example of https://kit.svelte.dev/docs/routing#server-receiving-data .

Logs

Server Log:

Error: Cannot use relative URL (/search?query=) with global fetch — use `event.fetch` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis
    at globalThis.fetch (file:///<path>/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:35:10)
    at getSearchResult (/src/routes/search/+layout.svelte:12:26)
    at Timeout.eval [as _onTimeout] (/src/routes/search/+layout.svelte:30:11)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)


### System Info

```Shell
OS: Windows 10 10.0.22621
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
    Memory: 8.86 GB / 31.93 GB
  Binaries:
    Node: 18.12.1 - C:\Program Files\nodejs\node.EXE
    npm: 8.19.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.1105.0), Chromium (108.0.1462.76)
    Internet Explorer: 11.0.22621.1

Severity

blocking an upgrade

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions