Skip to content

Commit 88489fb

Browse files
eviltesterCopilot
andauthored
Fix testenv site page header rendering (#186)
* fix testenv site page header rendering * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent dca460f commit 88489fb

2 files changed

Lines changed: 65 additions & 27 deletions

File tree

scripts/create-testenv.mjs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function runCommand(command, args, options = {}) {
6464

6565
if (result.status !== 0) {
6666
throw new Error(
67-
`Command failed: ${command} ${args.join(' ')} (status ${result.status ?? 1}${result.signal ? `, signal ${result.signal}` : ''})`,
67+
`Command failed: ${command} ${args.join(' ')} (status ${result.status ?? 1}${result.signal ? `, signal ${result.signal}` : ''})`
6868
);
6969
}
7070
}
@@ -191,10 +191,7 @@ function upsertHeadStyle(html, markerAttribute, styleTag) {
191191

192192
function applySeoDirectivesToHtml(
193193
html,
194-
{
195-
canonicalUrl = ROOT_CANONICAL_URL,
196-
robotsDirectives = TESTENV_ROBOTS_DIRECTIVES,
197-
} = {},
194+
{ canonicalUrl = ROOT_CANONICAL_URL, robotsDirectives = TESTENV_ROBOTS_DIRECTIVES } = {}
198195
) {
199196
let nextHtml = html;
200197

@@ -495,13 +492,22 @@ const TESTENV_HIDE_HEADER_STYLE = `
495492
}
496493
</style>`;
497494

495+
function hideTopHeaderInBuiltHtml(html) {
496+
if (html.includes('data-testenv-hide-header')) {
497+
return html;
498+
}
499+
500+
return html.replace('</head>', `${TESTENV_HIDE_HEADER_STYLE}\n </head>`);
501+
}
502+
498503
async function hideTopHeaderInBuiltPage(pagePath) {
499504
const html = await readFile(pagePath, 'utf8');
500-
if (html.includes('data-testenv-hide-header')) {
505+
const nextHtml = hideTopHeaderInBuiltHtml(html);
506+
507+
if (nextHtml === html) {
501508
return;
502509
}
503510

504-
const nextHtml = html.replace('</head>', `${TESTENV_HIDE_HEADER_STYLE}\n </head>`);
505511
await writeFile(pagePath, nextHtml, 'utf8');
506512
}
507513

@@ -541,11 +547,7 @@ async function createTemporaryDocsAppPlaceholder() {
541547
}
542548

543549
async function removeTemporaryDocsAppPlaceholder() {
544-
await writeFile(
545-
docsAppPlaceholderPath,
546-
'<p>This will be overwritten during the npm build.</p>',
547-
'utf8',
548-
);
550+
await writeFile(docsAppPlaceholderPath, '<p>This will be overwritten during the npm build.</p>', 'utf8');
549551
}
550552

551553
async function main() {
@@ -578,26 +580,19 @@ async function main() {
578580
await createTemporaryDocsAppPlaceholder();
579581

580582
try {
581-
runCommand(
582-
'pnpm',
583-
['--dir', 'docs-src', 'exec', 'docusaurus', 'build', '--out-dir', '../testenv/site'],
584-
{
585-
env: {
586-
DOCS_BASE_URL: fullSiteBaseUrl,
587-
DOCS_SITE_URL: 'https://eviltester.github.io',
588-
DOCS_TEST_BUILD: 'true',
589-
DOCS_TEST_CANONICAL_SITE_URL: TESTENV_CANONICAL_SITE_URL,
590-
},
583+
runCommand('pnpm', ['--dir', 'docs-src', 'exec', 'docusaurus', 'build', '--out-dir', '../testenv/site'], {
584+
env: {
585+
DOCS_BASE_URL: fullSiteBaseUrl,
586+
DOCS_SITE_URL: 'https://eviltester.github.io',
587+
DOCS_TEST_BUILD: 'true',
588+
DOCS_TEST_CANONICAL_SITE_URL: TESTENV_CANONICAL_SITE_URL,
591589
},
592-
);
590+
});
593591
} finally {
594592
await removeTemporaryDocsAppPlaceholder();
595593
}
596594

597595
await copyWebBuildIntoDirectory(tempWebDir, fullSiteDir);
598-
await hideTopHeaderInBuiltPage(path.join(fullSiteDir, 'app.html'));
599-
await hideTopHeaderInBuiltPage(path.join(fullSiteDir, 'generator.html'));
600-
await hideTopHeaderInBuiltPage(path.join(fullSiteDir, 'combinatorial.html'));
601596
await rm(tempWebDir, {
602597
recursive: true,
603598
force: true,
@@ -627,6 +622,7 @@ export {
627622
createLlmsTxt,
628623
createSiteRobotsTxt,
629624
createTestenvRobotsTxt,
625+
hideTopHeaderInBuiltHtml,
630626
renderIndexPage,
631627
};
632628

tests/integration/create-testenv-seo.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createLlmsTxt,
66
createSiteRobotsTxt,
77
createTestenvRobotsTxt,
8+
hideTopHeaderInBuiltHtml,
89
renderIndexPage,
910
} from '../../scripts/create-testenv.mjs';
1011

@@ -47,7 +48,9 @@ describe('create-testenv SEO helpers', () => {
4748
});
4849

4950
test('injects the test environment indicator into rewritten html pages', () => {
50-
const html = applySeoDirectivesToHtml('<!doctype html><html><head><title>Storybook</title></head><body></body></html>');
51+
const html = applySeoDirectivesToHtml(
52+
'<!doctype html><html><head><title>Storybook</title></head><body></body></html>'
53+
);
5154

5255
expect(html).toContain('data-testenv-indicator');
5356
expect(html).toContain('content: "Test Environment"');
@@ -57,4 +60,43 @@ describe('create-testenv SEO helpers', () => {
5760
expect(html).toContain('box-shadow: none;');
5861
expect(html).toContain('font: 700 6px/1.2 Arial, Helvetica, sans-serif;');
5962
});
63+
64+
test('SEO rewrite keeps existing headers intact while adding the test environment indicator', () => {
65+
const sourceHtml = `
66+
<!doctype html>
67+
<html>
68+
<head><title>App</title></head>
69+
<body>
70+
<div class="header" data-role="theme-toggle-host">AnyWayData</div>
71+
</body>
72+
</html>
73+
`;
74+
75+
const html = applySeoDirectivesToHtml(sourceHtml, {
76+
canonicalUrl: `${TESTENV_CANONICAL_SITE_URL}/app.html`,
77+
});
78+
79+
expect(html).toContain('data-testenv-indicator');
80+
expect(html).toContain('class="header"');
81+
expect(html).not.toContain('data-testenv-hide-header');
82+
expect(html).toContain('<link rel="canonical" href="https://anywaydata.com/app.html" />');
83+
});
84+
85+
test('standalone app pages can be rewritten to hide the top header', () => {
86+
const sourceHtml = `
87+
<!doctype html>
88+
<html>
89+
<head><title>Standalone App</title></head>
90+
<body>
91+
<div class="header" data-role="theme-toggle-host">AnyWayData</div>
92+
</body>
93+
</html>
94+
`;
95+
96+
const html = hideTopHeaderInBuiltHtml(sourceHtml);
97+
98+
expect(html).toContain('data-testenv-hide-header');
99+
expect(html).toContain('.header {');
100+
expect(html).toContain('display: none !important;');
101+
});
60102
});

0 commit comments

Comments
 (0)