From 715205ac20fc12537fd90d0a55cf406084236e11 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Sun, 26 May 2024 22:34:59 -0300 Subject: [PATCH] fix: correct content-length value --- .changeset/tough-waves-float.md | 5 +++++ packages/fastify-html-plugin/index.js | 11 ++++------- 2 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 .changeset/tough-waves-float.md diff --git a/.changeset/tough-waves-float.md b/.changeset/tough-waves-float.md new file mode 100644 index 00000000..881fd753 --- /dev/null +++ b/.changeset/tough-waves-float.md @@ -0,0 +1,5 @@ +--- +'@kitajs/fastify-html-plugin': patch +--- + +Correct content-length value diff --git a/packages/fastify-html-plugin/index.js b/packages/fastify-html-plugin/index.js index fe67ac17..2793fa6f 100644 --- a/packages/fastify-html-plugin/index.js +++ b/packages/fastify-html-plugin/index.js @@ -17,7 +17,7 @@ const kAutoDoctype = Symbol.for('fastify-kita-html.autoDoctype'); * >} */ function plugin(fastify, opts, next) { - fastify.decorateReply(kAutoDoctype, (opts.autoDoctype ??= true)); + fastify.decorateReply(kAutoDoctype, opts.autoDoctype ?? true); fastify.decorateReply('html', html); return next(); } @@ -63,12 +63,9 @@ function handleHtml(htmlStr, reply) { const requestData = SUSPENSE_ROOT.requests.get(reply.request.id); if (requestData === undefined) { - return ( - reply - // Should be safe to use .length instead of Buffer.byteLength here - .header('content-length', handleHtml.length) - .send(htmlStr) - ); + return reply + .header('content-length', Buffer.byteLength(htmlStr, 'utf-8')) + .send(htmlStr); } requestData.stream.push(htmlStr);