From 31d452f7e50bbfd88fa14470e86e6359e2a15db3 Mon Sep 17 00:00:00 2001 From: Hunter Bertoson <44106297+hkbertoson@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:04:34 -0600 Subject: [PATCH] Updates --- .changeset/quiet-readers-beg.md | 5 +++++ package/lib/components/TurnstileForm.astro | 2 +- package/lib/components/TurnstileWidget.astro | 2 +- package/src/middleware.ts | 10 +++------- 4 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 .changeset/quiet-readers-beg.md diff --git a/.changeset/quiet-readers-beg.md b/.changeset/quiet-readers-beg.md new file mode 100644 index 0000000..13a7883 --- /dev/null +++ b/.changeset/quiet-readers-beg.md @@ -0,0 +1,5 @@ +--- +"astro-turnstile": minor +--- + +Updated to use data attribute instead of comment diff --git a/package/lib/components/TurnstileForm.astro b/package/lib/components/TurnstileForm.astro index d484659..7c909f5 100644 --- a/package/lib/components/TurnstileForm.astro +++ b/package/lib/components/TurnstileForm.astro @@ -25,11 +25,11 @@ const { const siteUrl = Astro.site; --- -
diff --git a/package/src/middleware.ts b/package/src/middleware.ts index f4f09e8..36c157c 100644 --- a/package/src/middleware.ts +++ b/package/src/middleware.ts @@ -1,6 +1,6 @@ import type { MiddlewareHandler } from "astro"; -const COMPONENT_IDENTIFIER = ''; +const COMPONENT_IDENTIFIER = 'data-turnstile-container'; const TURNSTILE_SCRIPT = ` `; -// Max bytes to scan for component identifier before giving up const MAX_SCAN_BYTES = 16384; // 16KB let bytesScanned = 0; @@ -50,6 +49,7 @@ function createFastScriptInjectionTransform(): TransformStream { // Fast path: haven't found component yet if (!hasFoundComponent) { + // Look for the data attribute if (chunk.includes(COMPONENT_IDENTIFIER)) { hasFoundComponent = true; buffer = chunk; @@ -61,19 +61,16 @@ function createFastScriptInjectionTransform(): TransformStream { buffer += chunk; } - // Look for only if we found the component const headCloseIndex = buffer.indexOf(''); if (headCloseIndex === -1) { - // If buffer is getting too large, flush it and give up if (buffer.length > MAX_SCAN_BYTES) { controller.enqueue(buffer); buffer = ''; - hasInjected = true; // Prevent further processing + hasInjected = true; } return; } - // Inject the script const injectedContent = buffer.slice(0, headCloseIndex) + TURNSTILE_SCRIPT + @@ -88,7 +85,6 @@ function createFastScriptInjectionTransform(): TransformStream { if (buffer) { controller.enqueue(buffer); } - // Reset static counter for next request bytesScanned = 0; } });