From c7dca758277fde177e39c696d848ee939b1c81ff Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 28 Jan 2025 10:53:42 +0100 Subject: [PATCH] fix(server): Fix response content manipulation for HTML files with injectReload --- changelog.md | 6 ++++++ ts/00_commitinfo_data.ts | 2 +- ts/classes.typedserver.ts | 5 +++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 4c23f8c..e782e7b 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2025-01-28 - 3.0.55 - fix(server) +Fix response content manipulation for HTML files with injectReload + +- Moved fileString declaration inside HTML file handling block to prevent unnecessary string conversion for non-HTML files. +- Corrected responseContent assignment to ensure modified HTML strings are converted back to Buffer format. + ## 2025-01-28 - 3.0.54 - fix(servertools) Fixed an issue with compression results handling in HandlerStatic where content was always being written even if not compressed. diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 9e0c685..ba2239d 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@api.global/typedserver', - version: '3.0.54', + version: '3.0.55', description: 'A TypeScript-based project for easy serving of static files with support for live reloading, compression, and typed requests.' } diff --git a/ts/classes.typedserver.ts b/ts/classes.typedserver.ts index 6a7fa34..b57adc8 100644 --- a/ts/classes.typedserver.ts +++ b/ts/classes.typedserver.ts @@ -135,8 +135,8 @@ export class TypedServer { '/*', new servertools.HandlerStatic(this.options.serveDir, { responseModifier: async (responseArg) => { - let fileString = responseArg.responseContent.toString(); if (plugins.path.parse(responseArg.path).ext === '.html') { + let fileString = responseArg.responseContent.toString(); const fileStringArray = fileString.split(''); if (this.options.injectReload && fileStringArray.length === 2) { fileStringArray[0] = `${fileStringArray[0]} @@ -152,6 +152,7 @@ export class TypedServer { `; fileString = fileStringArray.join(''); console.log('injected typedserver script.'); + responseArg.responseContent = Buffer.from(fileString); } else if (this.options.injectReload) { console.log('Could not insert typedserver script'); } @@ -164,7 +165,7 @@ export class TypedServer { return { headers, path: responseArg.path, - responseContent: Buffer.from(fileString), + responseContent: responseArg.responseContent, }; }, serveIndexHtmlDefault: true,