Skip to content

CSR hydration fails for 204 response #13972

@shoaibakhtar9611

Description

@shoaibakhtar9611

Describe the bug

When using event.fetch inside load() for any API that throws 204 No Content status, and when we call response.text(), SvelteKit stores that value (an empty string "" for 204 response) inside a <script data-sveltekit-fetched> tag as part of SSR serialization. This is because SvelteKit proxies .text() function. So, when someone calls response.text(), it internally calls this proxy function async text(),

However, when SvelteKit hydrates the page on the client, it doesn't re-fetch API calls made during SSR. Instead, it uses the serialized <script data-sveltekit-fetched> tag containing the response and reconstructs it using Response inside initial_fetch() in fetcher.js.

However, if the API returned a 204 No Content, SvelteKit incorrectly tries to create a Response with an empty string as the body — which violates the Fetch spec, since 204 responses are not allowed to have a body. This causes the hydration to throw the error:

TypeError: Failed to construct 'Response': Response with null body status cannot have body

Reproduction

  • Load a page in SvelteKit where a load() function calls an API returning 204.
  • Call .text() on the response.
  • Observe that SSR works, but CSR hydration crashes with a TypeError in browser console.

Logs

System Info

System:
    OS: macOS 14.5
    CPU: (12) arm64 Apple M3 Pro
    Memory: 69.48 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.14.0/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
    pnpm: 9.10.0 - ~/.nvm/versions/node/v18.12.1/bin/pnpm
  Browsers:
    Chrome: 138.0.7204.101
    Edge: 138.0.3351.77
    Safari: 17.5
  npmPackages:
    @sveltejs/adapter-auto: 3.0.0 => 3.0.0 
    @sveltejs/adapter-node: 1.3.1 => 1.3.1 
    @sveltejs/kit: 2.5.18 => 2.5.18 
    @sveltejs/vite-plugin-svelte: 3.0.0 => 3.0.0 
    svelte: 4.2.18 => 4.2.18 
    vite: 5.3.4 => 5.3.4

Severity

serious, but I can work around it

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