iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🥀

Beware of Range header errors when combining Vercel and react-admin

に公開
2022/07/18

This is a note about a pitfall I encountered when trying to combine React Admin with Next.js.
Note: I am currently inquiring about this error, so there is a possibility it may be resolved in some way. Added an update at the bottom of the article.

Prerequisite: React Admin

When using the standard dataProvider for React Admin, ra-data-simple-rest, it uses the Content-Range header for pagination. It also sends the Range header to support Chrome.

https://github.com/marmelab/react-admin/blob/7aa5559ef8552605b02dadf277ae2a2f38e86c79/packages/ra-data-simple-rest/src/index.ts#L57-L61

What is the Range header anyway?

The Range is an HTTP request header that indicates the part of a document that the server should return.

I find myself feeling like "Wait, is it really appropriate to use the Range header for pagination?", but if I interpret it favorably, I can just barely convince myself that "pagination could be considered a part of the document the server should return."

Conditions under which the error occurs

I have placed a sample for this case here. It is basically just the standard template converted to TypeScript.

Trying to run it locally

Let's try running this locally first. This works without any issues.

$ curl http://localhost:3000/api/hello
{"name":"John Doe"}

Next, add the Range header. Naturally, this is also fine.

$ curl http://localhost:3000/api/hello -H "Range: foo=0/1"
{"name":"John Doe"}

Running on Vercel

First, run it normally. It works.

$ curl https://example-range-error.vercel.app/api/hello
{"name":"John Doe"}

Next, the problematic Range header:

$ curl https://example-range-error.vercel.app/api/hello -H "Range: foo=0/1"
A server error has occurred

INTERNAL_SERVER_ERROR

It failed. Incidentally, no logs appear in the Functions log, so it's highly likely it is failing before it gets there.
It is presumably failing because the Vercel side is attempting to process it.

Immediate Workarounds

If you use ra-data-json-server, it does not use the Range header, so you can avoid this issue for now by using that instead.

Update: Response from Vercel

I received the following response from Vercel:

I can see that your usage of the Range header is actually incorrect and was able to get a successful response by adjusting the syntax of the header as documented by MDN.
Notice that I get only 10 bytes back in my response when I issue the following request for the byte range of 0 - 10 and don't run into any errors:

Basically, they said, "Your usage of the Range header is incorrect, so if you do it like this, it will work."
Well, that makes sense.

$ curl https://example-range-error.vercel.app/api/hello -H "Range: bytes=0-10"
{name: "John Doe"}

I had expected it to be ignored if the unit in the Range header was incorrect, but it's entirely possible for an implementation not to do that.
Fortunately, you can choose the DataProvider for React Admin, so it seems safest to "avoid Data Providers that utilize the Range header."

GitHubで編集を提案

Discussion