payloadcms / payload

The best way to build a modern backend + admin UI. No black magic, all TypeScript, and fully open-source, Payload is both an app framework and a headless CMS.
https://payloadcms.com
MIT License
21.04k stars 1.27k forks source link

When saving a document with a lot of content: PayloadTooLargeError: request entity too large #6879

Open hdodov opened 1 week ago

hdodov commented 1 week ago

Link to reproduction

N/A

Describe the Bug

We have a collection with a rich text field and versions and autosave enabled. When you have a lot of text, the changes can't be saved. You get the following error:

[16:16:32] ERROR (payload): PayloadTooLargeError: request entity too large
    at readStream (/app/node_modules/payload/node_modules/raw-body/index.js:156:17)
    at getRawBody (/app/node_modules/payload/node_modules/raw-body/index.js:109:12)
    at read (/app/node_modules/payload/node_modules/express/node_modules/body-parser/lib/read.js:79:3)
    at jsonParser (/app/node_modules/payload/node_modules/express/node_modules/body-parser/lib/types/json.js:135:5)
    at Layer.handle [as handle_request] (/app/node_modules/payload/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/app/node_modules/payload/node_modules/express/lib/router/index.js:328:13)
    at /app/node_modules/payload/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/app/node_modules/payload/node_modules/express/lib/router/index.js:346:12)
    at next (/app/node_modules/payload/node_modules/express/lib/router/index.js:280:10)
    at localizationMiddleware (/app/node_modules/payload/src/localization/middleware.ts:37:3)

In the browser, this request fails https://example.com/api/my-collection/6674404ef834dbb0e77a7e07?draft=true&autosave=true&locale=en with status code 413.

To Reproduce

  1. Set up a collection with rich text
  2. Paste a lot of content
  3. Try to save

Payload Version

2.16.0

Adapters and Plugins

No response

hdodov commented 1 week ago

@denolfe @jmikrut hey, guys, this error surprised us badly in the last moment. While it gets fixed, is there any quick and dirty workaround you could recommend?

I believe it's tied to body-parser. As explained here, I tried adding this:

app.use(express.urlencoded({limit: '50mb'}));

…and even tried hacking the internal packages/payload/src/express/middleware/index.ts file by adding limit: '50mb' there too, but it didn't work.

Sorry for the lack of reproduction repo, but we're in a bit of a hurry and have a deadline tomorrow. Do you have any quick ideas off the top of your head?

hdodov commented 1 week ago

In case anyone else bumps into this, there's the express.json.limit option in payload.config.ts to increase the limit:

export default buildConfig({
    express: {
        json: {
            limit: 512000,
        },
    },
});

But I'm going to leave this issue open because the Payload types and docs say that the limit is 2MB (which wouldn't have been a problem for me if it were true), when in reality it's 102400 bytes (I saw it in the debugger). The express docs that Payload links to from its docs also say that the default limit is "100kb":

image