Skip to content

Memoize text, html, json on response? #19

Closed
@excid3

Description

@excid3

I was playing with this today and noticed that a fetch response doesn't allow reading the body twice. To reproduce:

const request = new FetchRequest("post", url)
const response = await request.perform()
await response.text
await response.text
// => Failed to execute 'text' on 'Response': body stream already read

And the situation that caught me was:

const request = new FetchRequest("post", url, { responseKind: "turbo-stream" })
const response = await request.perform()
await response.text
// => Failed to execute 'text' on 'Response': body stream already read

A solution is that we could memoize the text, html, json getters so that you could call this multiple times.

  get text () {
    return this.bodyText || (this.bodyText = this.response.text())
  }

I noticed this because I tried to read the text on a Turbo Stream response. Since renderTurboStream already accessed the text body, it seems unexpected.

Is this something worth fixing?

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