Fixing 520 Errors in the Database REST API
在数据库 API 的情况下,Cloudflare 520 错误 最常发生在你的请求头或 URL 中有超过 16KB 的数据时。
🌐 In the context of the database API, Cloudflare 520 errors most often occur when 16+KB worth of data is present in the headers/URL of your requests.
API 会在 URL 中包含过滤器,所以像这样的请求:
🌐 The API will include filters within the URL, so a request like so:
1let { data: countries, error } = await supabase.from('countries').select('name')翻译成类似这样的网址:
🌐 translates to a URL like:
1https://<project ref>.supabase.co/rest/v1/countries?select=name然而,将太多数据附加到 URL 上可能会超过 16KB 的限制,从而触发 520 错误。这通常发生在很长的 in 条款中,如下所示:
🌐 However, appending too much data to the URL can exceed the 16KB limitation, triggering a 520 failure. This typically occurs with lengthy in clauses, as demonstrated here:
1const { data, error } = await supabase2 .from('countries')3 .select()4 .not('id', 'in', '(5,6,7,8,9,...10,000)')要绕过这个问题,你必须使用 RPCs。它们是可以从 API 调用的数据库函数。它们不是把查询结构放在 URL 或头信息里,而是把它放到请求的负载中。
🌐 To circumvent this issue, you must use RPCs. They are database functions that you can call from the API. Instead of including a query's structure within the URL or header, they move it into the request's payload.
这里是一个基本的数据库函数示例
🌐 Here is a basic example of a database function
1create or replace function example(id uuid[])2returns uuid[]3language plpgsql4as $$5begin6 raise log 'the function example was called with an array size of: %', (select array_length(id, 1));7 return id;8end;9$$;RPC 然后可以用一个包含超过16KB数据的数组来调用这个函数
🌐 The RPC can then call the function with an array that contains more than 16KB of data
1const { data, error } = await supabase.rpc('example', { id: ['e2f34fb9-bbf9-4649-9b2f-09ec56e67a42', ...900 more UUIDs] })