Beyond SEO: Post-Render HTML Processing
Freestruct isn’t just for SEO. It’s a build-time HTML processor that runs after your SSG completes. This opens up possibilities beyond meta tags.
What Is Post-Render Processing?
Your SSG builds -> Freestruct modifies -> deploy
^
This is your hook
Your static site generator (Jekyll, Hugo, Docusaurus) outputs HTML. Freestruct reads that HTML and rewrites it before deploy. The examples below sketch what a postBuild script can do with the baked files - they are illustrations of the transformations, not an engine API you call from inside the injector.
Why This Matters
1. Fix Legacy SSG Issues
Your Jekyll/Hugo version is ancient. Can’t upgrade without breaking things. But you can use Freestruct to inject fixes:
// Fix broken internal links
html = html.replace(/href="\/docs\/old-path/g, 'href="/docs/new-path');
// Inject emergency banner
html = html.replace('</body>', '<div class="banner">Maintenance in progress</div></body>');
2. A/B Testing Without Client-Side Bloat
// Inject split-test script into specific pages
if (filePath.includes('pricing.html')) {
html = html.replace('</head>', '<script src="/ab-test.js"></script></head>');
}
3. Conditional Content Injection
// Different content for staging vs production
if (process.env.NODE_ENV === 'staging') {
html = html.replace('</body>', '<div class="staging-badge">STAGING</div></body>');
}
4. Inject Analytics That Require HTML Changes
// Some analytics need more than a script tag
if (config.analytics && config.analytics.gtag) {
html = html.replace('</head>', '<script>gtag("config", "' + config.analytics.gtag + '");</script></head>');
}
5. Image Optimization Hooks
// Add lazy loading to images that don't have it
html = html.replace(/<img (?!.*loading=)/g, '<img loading="lazy" ');
Build Hook System
Freestruct gives you two real seams, and neither one asks you to fork the engine.
Template snippets are the head/body hook surface. Drop a file in docs/_freestruct/ and it is injected on the next run:
| File | Injected |
|---|---|
inject-header.html |
Before </head> |
inject-body-start.html |
After <body> |
inject-footer.html |
Before </body> |
They substitute , and `` (see Custom Injection), so conditional markup and site-aware links live in plain HTML, no engine edit required.
hooks.postBuild chains arbitrary shell commands after the whole pass - custom schema injection scripts, audits, image post-processing, IndexNow submissions:
runHooks: true # commands stay dormant without it
hooks:
postBuild:
- node scripts/faq-schema.mjs
- npm run seo:audit
PostBuild hooks inherit the purge security contract: nothing executes without runHooks: true, each command gets a 30s timeout with inherited output, and the $SITE_URL / $BUILD_HASH / $OUTPUT_DIR placeholders are substituted (plus FREESTRUCT_SITE_URL / FREESTRUCT_BUILD_HASH / FREESTRUCT_OUTPUT_DIR env vars on the child, so scripts never have to do string surgery). A nonzero hook exit fails the whole build with exit code 4.
For anything the snippets cannot express, a postBuild script reads the baked HTML directly and rewrites it - the output tree is plain files by then.
Best Practice: Comment Your Modifications
When you modify HTML in your build pipeline, tell your team.
Post-render modifications can confuse developers who inspect the page and wonder “where did this come from?”
Do This
<!-- freestruct: Added staging banner -->
<div class="staging-badge">STAGING</div>
<!-- freestruct: Legacy link fix from v2.5 migration -->
<a href="/docs/new-path">
<!-- freestruct: Lazy loading added to all images -->
<img src="..." loading="lazy">
Not This
<!-- Just appeared after deploy, no idea why -->
<div class="staging-badge">
<!-- No comment, just modified links -->
<a href="/docs/new-path">
Why This Matters
- Debugging - Team members can trace changes back to source
- Auditing - Security reviews can verify intentional modifications
- Onboarding - New devs understand the build pipeline
- Maintenance - Future upgrades won’t accidentally break custom injections
Freestruct automatically adds this:
<!-- Freestruct SEO -->
<meta name="freestruct-build" content="abc123">
<!-- Freestruct -->
Add your own markers following the same pattern.
Security Considerations
When processing HTML at build time:
- Trust your config - Don’t let user input in freestruct.config.yml execute arbitrary code
- Validate modifications - Test your transformations don’t break HTML
- Version control your hooks - Keep custom scripts in repo, not hardcoded
- Comment changes - See above
Use Cases Summary
| Use Case | Example |
|---|---|
| Fix broken links | Redirect old URLs to new ones |
| Environment badges | Show STAGING/PRODUCTION banners |
| A/B testing | Inject test scripts on specific pages |
| Analytics | Add tracking tags that need HTML modification |
| Image optimization | Add lazy loading to all images |
| Build metadata | Inject timestamps, git info, version |
| Conditional features | Enable/disable features per environment |
Related
- Cache Busting - Built-in hash system
- Template - How inject-brand.html works
- Freestruct.config.yml Reference - Configuration reference