From a231e15f6234f8562a2a4a9b2e65db91f1ae9ae7 Mon Sep 17 00:00:00 2001 From: Lily Shen <115414357+lilyshen0722@users.noreply.github.com> Date: Wed, 26 Aug 2026 01:01:14 -0700 Subject: [PATCH 1/2] perf(seo): keep guide pages static for browsers --- frontend/index.html | 4 +++ frontend/scripts/generate-seo-pages.mjs | 28 +++++++++++++++++++- frontend/scripts/generate-seo-pages.test.mjs | 11 ++++++-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/frontend/index.html b/frontend/index.html index 8f52de9c09..513845ebbf 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -37,7 +37,9 @@ script runs before first paint: browsers (JS on) never show the prerender; crawlers without JS still get the full content. Not cloaking — JS-running crawlers see the real app. --> + + + +
diff --git a/frontend/scripts/generate-seo-pages.mjs b/frontend/scripts/generate-seo-pages.mjs index a4bde5ac66..d40d3c6235 100644 --- a/frontend/scripts/generate-seo-pages.mjs +++ b/frontend/scripts/generate-seo-pages.mjs @@ -9,6 +9,10 @@ const siteUrl = 'https://commonly.me'; const metadataStart = ''; const metadataEnd = ''; const pageContentMarker = ''; +const gateRuntimeStart = ''; +const gateRuntimeEnd = ''; +const navigationRuntimeStart = ''; +const navigationRuntimeEnd = ''; const escapeHtml = (value) => String(value) .replaceAll('&', '&') @@ -30,6 +34,25 @@ const replaceMarkedSection = (template, start, end, content) => { return `${template.slice(0, startIndex)}${start}\n${content}\n${end}${template.slice(endIndex + end.length)}`; }; +const removeMarkedSection = (template, start, end) => { + const startIndex = template.indexOf(start); + const endIndex = template.indexOf(end); + + if (startIndex < 0 || endIndex < 0 || endIndex < startIndex) { + throw new Error(`Missing or malformed ${start} marker in the Vite output.`); + } + + return `${template.slice(0, startIndex)}${template.slice(endIndex + end.length)}`; +}; + +const staticDocument = (template) => removeMarkedSection( + removeMarkedSection(template, gateRuntimeStart, gateRuntimeEnd), + navigationRuntimeStart, + navigationRuntimeEnd, +) + .replaceAll(/]*\brel="modulepreload")[^>]*>\s*/g, '') + .replaceAll(/]*\btype="module"[^>]*>\s*<\/script>\s*/g, ''); + const pageUrl = (path) => `${siteUrl}${path}`; const formatDate = (value) => { @@ -371,6 +394,7 @@ export const buildPageDefinitions = ({ landing, compare, useCases, guides = {} } title: 'Guides for teams working with AI agents | Commonly', description: 'Practical guides for teams that work with AI agents: shared context, task ownership, human review, and durable handoffs.', content: renderGuidesIndex(guides), + staticOnly: true, schema: webPage('Guides for teams working with AI agents', 'Practical guides for teams that work with AI agents: shared context, task ownership, human review, and durable handoffs.', guidesIndexPath), }; @@ -397,6 +421,7 @@ export const buildPageDefinitions = ({ landing, compare, useCases, guides = {} } title: guide.titleTag, description: guide.description, content: renderGuide(guide), + staticOnly: true, ogType: 'article', datePublished: provenance.datePublished, dateModified: provenance.dateModified, @@ -429,7 +454,8 @@ export const buildPageDefinitions = ({ landing, compare, useCases, guides = {} } }; export const renderStaticPage = (template, page) => { - const withMetadata = replaceMarkedSection(template, metadataStart, metadataEnd, metadata(page)); + const staticTemplate = page.staticOnly ? staticDocument(template) : template; + const withMetadata = replaceMarkedSection(staticTemplate, metadataStart, metadataEnd, metadata(page)); if (!withMetadata.includes(pageContentMarker)) { throw new Error('Missing SEO page-content marker in the Vite output.'); } diff --git a/frontend/scripts/generate-seo-pages.test.mjs b/frontend/scripts/generate-seo-pages.test.mjs index 9fa980e5d3..c62aa96056 100644 --- a/frontend/scripts/generate-seo-pages.test.mjs +++ b/frontend/scripts/generate-seo-pages.test.mjs @@ -54,13 +54,18 @@ test('emits a canonical crawlable page for every public route', async () => { const taskManagementArticle = taskManagementGuide.schema['@graph'].find((item) => item['@type'] === 'Article'); assert.equal(taskManagementArticle.datePublished, '2026-08-25'); assert.equal(taskManagementArticle.dateModified, '2026-08-25'); - const guideTemplate = '
'; + const guideTemplate = '
'; const taskManagementHtml = renderStaticPage(guideTemplate, taskManagementGuide); assert.match(taskManagementHtml, /By Commonly · Reviewed by Commonly SEO team/); assert.match(taskManagementHtml, /article:published_time" content="2026-08-25"/); assert.match(taskManagementHtml, /article:modified_time" content="2026-08-25"/); assert.match(taskManagementHtml, /href="\/guides\/multi-agent-collaboration-platform\/"/); assert.match(taskManagementHtml, /href="\/guides\/ai-agent-workspace\/"/); + assert.doesNotMatch(taskManagementHtml, /document\.documentElement/); + assert.doesNotMatch(taskManagementHtml, /popstate/); + assert.doesNotMatch(taskManagementHtml, /modulepreload/); + assert.doesNotMatch(taskManagementHtml, /type="module"/); + assert.match(taskManagementHtml, /#seo-page \{ color: #fff; \}/); for (const guidePath of [ '/guides/multi-agent-collaboration-platform/', '/guides/ai-agent-workspace/', @@ -81,10 +86,12 @@ test('puts route content, canonical metadata, and structured data in the documen content: '

Example page

Home
', schema: { '@context': 'https://schema.org', '@type': 'WebPage', name: 'Example page' }, }; - const template = '
'; + const template = '
'; const rendered = renderStaticPage(template, page); assert.match(rendered, /

Example page<\/h1>/); assert.match(rendered, / Date: Wed, 26 Aug 2026 01:17:36 -0700 Subject: [PATCH 2/2] fix(seo): avoid regex when stripping guide runtime --- frontend/scripts/generate-seo-pages.mjs | 58 +++++++++++++++++--- frontend/scripts/generate-seo-pages.test.mjs | 5 +- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/frontend/scripts/generate-seo-pages.mjs b/frontend/scripts/generate-seo-pages.mjs index d40d3c6235..12b640dad0 100644 --- a/frontend/scripts/generate-seo-pages.mjs +++ b/frontend/scripts/generate-seo-pages.mjs @@ -13,6 +13,7 @@ const gateRuntimeStart = ''; const gateRuntimeEnd = ''; const navigationRuntimeStart = ''; const navigationRuntimeEnd = ''; +const viteEntryAssetPrefix = 'src="/assets/index-'; const escapeHtml = (value) => String(value) .replaceAll('&', '&') @@ -45,13 +46,56 @@ const removeMarkedSection = (template, start, end) => { return `${template.slice(0, startIndex)}${template.slice(endIndex + end.length)}`; }; -const staticDocument = (template) => removeMarkedSection( - removeMarkedSection(template, gateRuntimeStart, gateRuntimeEnd), - navigationRuntimeStart, - navigationRuntimeEnd, -) - .replaceAll(/]*\brel="modulepreload")[^>]*>\s*/g, '') - .replaceAll(/]*\btype="module"[^>]*>\s*<\/script>\s*/g, ''); +const removeRuntimeEntryScript = (template) => { + const entryIndex = template.indexOf(viteEntryAssetPrefix); + const tagStart = template.lastIndexOf('<', entryIndex); + const tagEnd = template.indexOf('>', entryIndex); + + if ( + entryIndex < 0 + || tagStart < 0 + || tagEnd < entryIndex + || !template.startsWith(' { + let document = template; + let preloadIndex = document.indexOf('rel="modulepreload"'); + + while (preloadIndex >= 0) { + const tagStart = document.lastIndexOf('<', preloadIndex); + const tagEnd = document.indexOf('>', preloadIndex); + + if ( + tagStart < 0 + || tagEnd < preloadIndex + || !document.startsWith(' { + const withoutGateRuntime = removeMarkedSection(template, gateRuntimeStart, gateRuntimeEnd); + const withoutNavigationRuntime = removeMarkedSection( + withoutGateRuntime, + navigationRuntimeStart, + navigationRuntimeEnd, + ); + + return removeModulePreloads(removeRuntimeEntryScript(withoutNavigationRuntime)); +}; const pageUrl = (path) => `${siteUrl}${path}`; diff --git a/frontend/scripts/generate-seo-pages.test.mjs b/frontend/scripts/generate-seo-pages.test.mjs index c62aa96056..2dffa0595a 100644 --- a/frontend/scripts/generate-seo-pages.test.mjs +++ b/frontend/scripts/generate-seo-pages.test.mjs @@ -54,7 +54,7 @@ test('emits a canonical crawlable page for every public route', async () => { const taskManagementArticle = taskManagementGuide.schema['@graph'].find((item) => item['@type'] === 'Article'); assert.equal(taskManagementArticle.datePublished, '2026-08-25'); assert.equal(taskManagementArticle.dateModified, '2026-08-25'); - const guideTemplate = '
'; + const guideTemplate = '
'; const taskManagementHtml = renderStaticPage(guideTemplate, taskManagementGuide); assert.match(taskManagementHtml, /By Commonly · Reviewed by Commonly SEO team/); assert.match(taskManagementHtml, /article:published_time" content="2026-08-25"/); @@ -65,6 +65,7 @@ test('emits a canonical crawlable page for every public route', async () => { assert.doesNotMatch(taskManagementHtml, /popstate/); assert.doesNotMatch(taskManagementHtml, /modulepreload/); assert.doesNotMatch(taskManagementHtml, /type="module"/); + assert.doesNotMatch(taskManagementHtml, /src="\/assets\/index-/); assert.match(taskManagementHtml, /#seo-page \{ color: #fff; \}/); for (const guidePath of [ '/guides/multi-agent-collaboration-platform/', @@ -86,7 +87,7 @@ test('puts route content, canonical metadata, and structured data in the documen content: '

Example page

Home
', schema: { '@context': 'https://schema.org', '@type': 'WebPage', name: 'Example page' }, }; - const template = '
'; + const template = '
'; const rendered = renderStaticPage(template, page); assert.match(rendered, /

Example page<\/h1>/);