feat(openresty): add origin error page GET-only option

Allow restricting custom origin error HTML to GET requests so other
methods pass through origin responses. Adds option seed, snapshot field,
edge limit_except/Lua handling, and admin UI switch.
This commit is contained in:
ryan
2026-08-06 20:22:44 +08:00
parent d17d8457f3
commit 6738abdec1
13 changed files with 118 additions and 7 deletions
@@ -10,15 +10,18 @@ export const OPTIONS_QUERY_KEY = ['openflare', 'options'] as const;
export const KEY_ENABLED = 'origin_error_page_enabled';
export const KEY_STATUS_CODES = 'origin_error_page_status_codes';
export const KEY_HTML = 'origin_error_page_html';
export const KEY_GET_ONLY = 'origin_error_page_get_only';
export type ErrorPageFields = {
enabled: boolean;
getOnly: boolean;
statusCodes: string[];
html: string;
};
export const defaultErrorPageFields: ErrorPageFields = {
enabled: true,
getOnly: false,
statusCodes: [...DEFAULT_ORIGIN_ERROR_PAGE_STATUS_TAGS],
html: '',
};
@@ -34,8 +37,10 @@ export function mapOptionsToFields(
optionMap: Record<string, string>,
): ErrorPageFields {
const enabledRaw = optionMap[KEY_ENABLED];
const getOnlyRaw = optionMap[KEY_GET_ONLY];
return {
enabled: enabledRaw === undefined ? true : enabledRaw === 'true',
getOnly: getOnlyRaw === undefined ? false : getOnlyRaw === 'true',
statusCodes: parseStatusCodeTagsJSON(optionMap[KEY_STATUS_CODES]),
html: optionMap[KEY_HTML] ?? '',
};
+25 -2
View File
@@ -30,6 +30,7 @@ import {
defaultErrorPageFields,
invalidateErrorPageQueries,
KEY_ENABLED,
KEY_GET_ONLY,
KEY_STATUS_CODES,
mapOptionsToFields,
OPTIONS_QUERY_KEY,
@@ -60,12 +61,13 @@ export default function ErrorPagesPage() {
[fields.html],
);
/** 仅保存启用开关 + 触发状态码(HTML 在编辑页单独保存) */
/** 仅保存策略项(HTML 在编辑页单独保存) */
const savePolicyMutation = useMutation({
mutationFn: async () => {
validateStatusCodeTags(fields.statusCodes);
await OptionService.updateBatch([
{ key: KEY_ENABLED, value: String(fields.enabled) },
{ key: KEY_GET_ONLY, value: String(fields.getOnly) },
{
key: KEY_STATUS_CODES,
value: JSON.stringify(fields.statusCodes),
@@ -124,13 +126,34 @@ export default function ErrorPagesPage() {
</CardHeader>
</Card>
<Card className='border-dashed shadow-none'>
<CardHeader className='flex flex-row items-center justify-between gap-4'>
<div>
<CardTitle className='text-base'>仅针对 GET 请求</CardTitle>
<CardDescription>
开启后仅对 GET
请求的匹配错误状态码返回自定义错误页;POST/PUT
等其它方法直接透传源站响应。
</CardDescription>
</div>
<Switch
checked={fields.getOnly}
disabled={!fields.enabled}
onCheckedChange={(getOnly) =>
setFields((prev) => ({ ...prev, getOnly }))
}
aria-label='仅针对 GET 请求'
/>
</CardHeader>
</Card>
<Card className='border-dashed shadow-none'>
<CardHeader className='flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between space-y-0'>
<div className='space-y-1.5'>
<CardTitle className='text-base'>触发状态码</CardTitle>
<CardDescription>
支持单码(如 502)或闭区间(如 500-599),范围 400–599。默认
500-599。修改启用开关或状态码后需点击保存。
500-599。修改策略开关或状态码后需点击保存。
</CardDescription>
</div>
<Button