Skip to content

fetch with data URL throws error when used in SvelteKit #10657

@danielniccoli

Description

@danielniccoli

Describe the bug

I have a web app that takes a zlib-compressed and base64-encoded blob as user input. I validate the data on the client, before sending it to the server, where it is validated again.

The validation happens by passing the base64-encoded blob to fetch() as a data URL, which returns a ReadableStream that is then deflated using a DecompressionStream. The validation function works in the browser and in node.js, which was a priority to not have nearly identical duplicate code.

Adding the function call on the server side (+page.server.ts) raises an exception.

Error: Cannot use relative URL (data:application/octet-stream;base64,eNrtfV1vXMm…) with global fetch — use `event.fetch` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis
    at globalThis.fetch …

Svelte is mistakenly identifying a data URL as a relative URL.
And forcibly raising an error when global fetch() is used is a problem as well, because this prevents the use of functions that are designed to work in both environments, client and server.

Reproduction

Run this line in nodejs and in a +page.server.js file.

await fetch("data:application/octet-stream;base64,eNrtfV==")

Logs

No response

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i5-13600K
    Memory: 16.37 GB / 31.75 GB
  Binaries:
    Node: 20.3.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD
    npm: 9.8.1 - ~\AppData\Roaming\npm\npm.CMD
    pnpm: 7.25.0 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Spartan (44.22621.2134.0), Chromium (116.0.1938.62)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    svelte: ^4.0.5 => 4.2.0

Severity

blocking all usage of SvelteKit

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions