Skip to content

Batching - safeguard for max url length #506

Closed

Description

@KATT

👋 Do you want to pick this up? See #506 (comment)


Challenge described:

  • Max characters for a url is ~2k characters (I think)
  • Currently the batching lumps everything in a request as /api/trpc/posts.byId,posts.byId.query,posts.byId?batch=1&input=${encodeURIComponent(JSON.stringify(input))}

This could potentially lead to situations where the client constructs a url that is too long.

A few things we could do to prevent this

  • Change the way we build the query
    • Given query, this could be built like like /api/trpc/3*posts.byId?batch=x
  • Make sure the data loader splits up the queries into separate requests when exceeding limit of x
  • What I don't want to do is to use anything but GET for queries

Refs in the code


I think the best interface way of handling this is to allow doing httpBatchLink({ maxURILength: 2048 }).

Activity

  1. changed the title [-]Batching - safeguard for max url lengt[/-] [+]Batching - safeguard for max url length[/+] on Jun 10, 2021
  2. alaister commented on Oct 11, 2021

    @alaister
    Contributor

    I just ran into a URL too long error (I think).

    I'm sending a batch (~300) of UUIDs in a query and it falls over on the server (browser doesn't seem to mind the 25,000 character url lol). It can handle up to 182 UUIDs but any more and it gives me a The page you were looking for cannot be served. error.

    Is there currently a way to increase the max query string length? (or is this a Next.js limitation?)

    I could rewrite it as a mutation but this would be a huge pain. I know you've said you don't want to use anything but GETs for queries, but is this a valid use case? Maybe if it was opt-in per query?

    EDIT: Turns out it's Nginx limiting my URI length. But I'd still like to know your thoughts on why you don't want to use anything other than GET requests :)

  3. KATT commented on Oct 11, 2021

    @KATT
    MemberAuthor

    I just ran into a URL too long error (I think).

    I'm sending a batch (~300) of UUIDs in a query and it falls over on the server (browser doesn't seem to mind the 25,000 character url lol). It can handle up to 182 UUIDs but any more and it gives me a The page you were looking for cannot be served. error.

    Sounds like you ran into it indeed!

    Wow - how come you need to send 300 ids in a query? 😳 Can you explain a bit how this works in your app?

    I'm surprised the browser didn't choke on it!

    Maybe you can rewrite it as a list query instead somehow?

    Is there currently a way to increase the max query string length? (or is this a Next.js limitation?)

    I don't think it's a Next.js limitation per se, but rather a Node.js-thing.

    I could rewrite it as a mutation but this would be a huge pain. I know you've said you don't want to use anything but GETs for queries, but is this a valid use case? Maybe if it was opt-in per query?

    For now, the GET is the only way to do queries, I'll give it a think, maybe we can do a POST query with a ?method=GET-query or something.

    I'll give this some thought but I won't pick this up for at least a few days so please see if you can find a way to avoid doing this.

    cc @mmkal - a penny for your thoughts 🪙😊

  4. alaister commented on Oct 11, 2021

    @alaister
    Contributor

    Thanks for your prompt response as always!

    Wow - how come you need to send 300 ids in a query? 😳 Can you explain a bit how this works in your app?

    Yeah it's a strange design decision to be sure. It's basically for a long page with hundreds of very small items, of which I only have the id. But I do have a query resolver that takes a long list of ids and gives me all the actual things.

    I'm surprised the browser didn't choke on it!

    Yeah me too - 300 UUIDs should be about the max so as long as that works I'm happy and will just try not to think about it.

    I don't think it's a Next.js limitation per se, but rather a Node.js-thing.

    The Node.js limit should be okay for my use case. I discovered it was my Nginx config limiting the size of the uri, so I just upped client_header_buffer_size and large_client_header_buffers and it works 100% fine now!

    maybe we can do a POST query with a ?method=GET-query or something.

    I am a fan of a good old method override. I think I've come full circle on this though already, because Nginx was the limiting factor, not the browser or node/next/trpc, I don't think this is a big of an issues as I originally thought!

  5. KATT commented on Oct 11, 2021

    @KATT
    MemberAuthor

    Yeah it's a strange design decision to be sure. It's basically for a long page with hundreds of very small items, of which I only have the id. But I do have a query resolver that takes a long list of ids and gives me all the actual things.

    You could also do frontend-only pagination as a workaround or debouncing rendering of the list items responsible for the loading

    The Node.js limit should be okay for my use case. I discovered it was my Nginx config limiting the size of the uri, so I just upped client_header_buffer_size and large_client_header_buffers and it works 100% fine now!

    Oh, cool!

    I am a fan of a good old method override. I think I've come full circle on this though already, because Nginx was the limiting factor, not the browser or node/next/trpc, I don't think this is a big of an issues as I originally thought!

    Will take this into consideration!

  6. ecwyne commented on Dec 30, 2021

    @ecwyne

    I'm running into this issue as well. Currently I've worked around it with an intersection observer that only loads when the element is in view but something like a httpBatchLink({ maxURILength: 2048 }) or httpBatchLink({ maxBatchSize: 20 }) would be very very helpful.

  7. darioielardi commented on Jan 19, 2022

    @darioielardi
    Contributor

    I've just ran into this as well, is there some work being done on this I can contribute to or maybe a workaround?
    My use case is a table with 20-100 rows, and maxBatchSize sounds like the perfect solution.

  8. KATT commented on Jan 19, 2022

    @KATT
    MemberAuthor

    Yes! Here's some rough steps:

  9. added
    👉 good first issue
    🙋‍♂️ help wanted
    on Jan 19, 2022
  10. KATT commented on Jan 28, 2022

    @KATT
    MemberAuthor

    Fixed by #1468

  11. KATT commented on Jan 28, 2022

    @KATT
    MemberAuthor
  12. github-actions commented on Oct 4, 2022

    @github-actions
    Contributor

    This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you.

  13. locked as resolved and limited conversation to collaborators on Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    @trpc/client👉 good first issue🙋‍♂️ help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions