diff --git a/lib/errors/index.ts b/lib/errors/index.ts index 109b1c618..99269ed5d 100644 --- a/lib/errors/index.ts +++ b/lib/errors/index.ts @@ -41,8 +41,6 @@ export const errorHandler: ErrorHandler = (error, ctx) => { }, }); } else { - ctx.header('Content-Type', 'text/html; charset=UTF-8'); - if (error instanceof RequestInProgressError) { ctx.header('Cache-Control', `public, max-age=${config.requestTimeout / 1000}`); ctx.status(503); @@ -59,7 +57,7 @@ export const errorHandler: ErrorHandler = (error, ctx) => { const requestPath = ctx.req.path; - return ctx.body( + return ctx.html( art(path.resolve(__dirname, '../views/error.art'), { requestPath, message, diff --git a/lib/middleware/template.ts b/lib/middleware/template.ts index 2ac5c3aea..88f7f1122 100644 --- a/lib/middleware/template.ts +++ b/lib/middleware/template.ts @@ -14,15 +14,12 @@ const middleware: MiddlewareHandler = async (ctx, next) => { // only enable when debugInfo=true if (config.debugInfo) { if (outputType === 'debug.json') { - ctx.header('Content-Type', 'application/json; charset=UTF-8'); - return ctx.body(ctx.get('json') ? JSON.stringify(ctx.get('json'), null, 4) : JSON.stringify({ message: 'plugin does not set debug json' })); + return ctx.json(ctx.get('json') || { message: 'plugin does not set debug json' }); } if (/(\d+)\.debug\.html$/.test(outputType)) { - ctx.header('Content-Type', 'text/html; charset=UTF-8'); - const index = Number.parseInt(outputType.match(/(\d+)\.debug\.html$/)?.[1] || '0'); - return ctx.body(data?.item?.[index]?.description || `data.item[${index}].description not found`); + return ctx.text(data?.item?.[index]?.description || `data.item[${index}].description not found`); } } @@ -87,8 +84,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { } if (outputType === 'ums') { - ctx.header('Content-Type', 'application/json; charset=UTF-8'); - return ctx.body(rss3Ums(result)); + return ctx.json(rss3Ums(result)); } else if (outputType === 'json') { ctx.header('Content-Type', 'application/feed+json; charset=UTF-8'); return ctx.body(json(result)); diff --git a/lib/routes/index.ts b/lib/routes/index.ts index da0acbeaa..be7a6180a 100644 --- a/lib/routes/index.ts +++ b/lib/routes/index.ts @@ -8,7 +8,6 @@ import { getDebugInfo } from '@/utils/debug-info'; const startTime = Date.now(); const handler: Handler = (ctx) => { - ctx.header('Content-Type', 'text/html; charset=UTF-8'); ctx.header('Cache-Control', 'no-cache'); const debug = getDebugInfo(); @@ -18,7 +17,7 @@ const handler: Handler = (ctx) => { const duration = Date.now() - startTime; - return ctx.body( + return ctx.html( art(path.resolve(__dirname, '../views/welcome.art'), { showDebug, disallowRobot, diff --git a/lib/routes/robots.txt.ts b/lib/routes/robots.txt.ts index 0f0e8ab2a..3185d1bdb 100644 --- a/lib/routes/robots.txt.ts +++ b/lib/routes/robots.txt.ts @@ -3,11 +3,10 @@ import { config } from '@/config'; const handler: Handler = (ctx) => { if (config.disallowRobot) { - ctx.header('Content-Type', 'text/plain'); - return ctx.body('User-agent: *\nDisallow: /'); + return ctx.text('User-agent: *\nDisallow: /'); } else { ctx.status(404); - return ctx.body(''); + return ctx.text(''); } }; diff --git a/lib/views/json.ts b/lib/views/json.ts index 8810a8dcd..20111b8df 100644 --- a/lib/views/json.ts +++ b/lib/views/json.ts @@ -42,7 +42,7 @@ const json = (data: Data) => { _extra: item._extra || undefined, })), }; - return JSON.stringify(jsonFeed, null, 4); + return JSON.stringify(jsonFeed); }; export default json; diff --git a/lib/views/rss3-ums.ts b/lib/views/rss3-ums.ts index 453f4ffef..6f932c0b7 100644 --- a/lib/views/rss3-ums.ts +++ b/lib/views/rss3-ums.ts @@ -46,7 +46,7 @@ const rss3Ums = (data) => { }; }), }; - return JSON.stringify(umsResult, null, 4); + return umsResult; }; // we treat the domain as the owner of the content