From 37cf634a8331e075f835c357ae54e4bcf7f85108 Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Mon, 12 Oct 2026 14:48:02 +1300 Subject: [PATCH] fix: broken multi-line tag replacement --- .../routes/__tests__/preview.index.test.ts | 34 +++++++++++++++++++ .../lambda-tiler/src/routes/preview.index.ts | 18 ++++------ 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/packages/lambda-tiler/src/routes/__tests__/preview.index.test.ts b/packages/lambda-tiler/src/routes/__tests__/preview.index.test.ts index e42e963fd..52dbe95db 100644 --- a/packages/lambda-tiler/src/routes/__tests__/preview.index.test.ts +++ b/packages/lambda-tiler/src/routes/__tests__/preview.index.test.ts @@ -104,6 +104,40 @@ describe('/@*', () => { assert.equal(getBody(resB)?.toString().includes(''), true); }); + it('should replace a meta tag that spans multiple lines', async () => { + const ctx = new LambdaAlbRequest(baseRequest, {} as Context, LogConfig.get()); + process.env[Env.StaticAssetLocation] = 'memory://assets/'; + + const indexHtml = [ + '', + ' ', + ' ', + ' ', + ' ', + '', + ].join('\n'); + + await fsa.write(new URL('memory://assets/index.html'), indexHtml); + + const res = await loadAndServeIndexHtml( + ctx, + null, + new Map([['og:description', '']]), + ); + const body = getBody(res)?.toString(); + + assert.equal( + body?.includes(''), + true, + ); + + assert.equal(body?.includes(''), true); + }); + it('should include config url', async () => { process.env[Env.StaticAssetLocation] = 'memory://assets/'; diff --git a/packages/lambda-tiler/src/routes/preview.index.ts b/packages/lambda-tiler/src/routes/preview.index.ts index f3829a122..3d582ec81 100644 --- a/packages/lambda-tiler/src/routes/preview.index.ts +++ b/packages/lambda-tiler/src/routes/preview.index.ts @@ -64,17 +64,13 @@ export async function loadAndServeIndexHtml( return res; } - // Replace open graph tags - const output = String(indexHtml) - .split('\n') - .map((f) => { - for (const [key, value] of tags.entries()) { - if (f.includes(key)) return value; - } - return f; - }); - - const outHtml = output.join('\n'); + // Replace open graph tags, matching the whole `` element as it + // may be formatted across multiple lines + let outHtml = String(indexHtml); + for (const [key, value] of tags.entries()) { + outHtml = outHtml.replace(new RegExp(`[ \\t]*]*property="${key}"[^>]*/>`), value); + } + res.header(HttpHeader.ETag, Etag.key(outHtml)); res.buffer(outHtml, `text/html; charset=utf-8`); return res;