r/webdev • u/anakinmcfly • 22d ago
Question Best way to insert a html snippet into a html file on a static site?
I’m archiving a website as a fully static site so I can host it for free on Cloudflare via GitHub. Unfortunately this means that the PHP includes I’ve been using (header, navbar, footer) will stop working across a few hundred pages, and I’ve been looking for alternatives.
Server Side Includes did not work; I finally had success using JavaScript fetch() and inserting that content into selected <div>s, but I’m not sure if that’s the best option.
Are there better ways to achieve the same result that would work on a static site?
5
Upvotes
22
u/Serpico99 22d ago edited 22d ago
Use a static site generator like Astro, 11ty, or Hugo. If it's just mergeing stuff into a layout, it should not take too much time or effort on your part
4
u/Level_Imagination544 22d ago
fetch() does work, but the header and footer pop in after load, which can cause layout shift and means crawlers might not see them. With a few hundred pages I'd do the includes at build time instead. A static site generator like 11ty or Hugo handles it nicely, or even a one-off script that stitches the header, navbar and footer into each HTML file once and commits the output. Cloudflare Pages can then serve plain files with no runtime work.
4
u/Early-Community9943 22d ago
Since it's a one-time archive, I'd skip the JS fetch and run a quick script that pastes the header and footer into every file before you push. No layout shift, and it works on any host.
1
u/AryanThakor 22d ago
u could do it with a cloudflare worker using HTMLRewriter, keep header.html and footer.html separate and it swaps them in before the page loads
but if u want to change the layout later id just go with 11ty. move the header and footer into one layout file and ur html pages barely change
3
u/thekwoka 22d ago
Just use Astro.
Either built statically, or run in Cloudflare workers
2
u/kemalios 22d ago
If it's a real archive, drop the fetch. Every page turns into extra requests and the header lands after the body. Anything that blocks JS leaves you with a blank nav, and an archive that depends on fetch is only an archive as long as those partial files and that script keep existing.
Cloudflare won't run SSI, so that route isn't coming back. If the PHP still runs locally, serve the site with PHP and mirror it with wget. What comes down is plain HTML with the includes already rendered, so you don't port a few hundred pages by hand.
2
u/nekenaharry 22d ago
If the goal is a static archive, the cleanest way is to do the includes at build time instead of in the browser. A small script in Node or Python, or even a shell loop with sed, can replace a marker comment in each HTML file with the snippet content, and you deploy the output. A tiny static site generator like Eleventy or Hugo also handles partials natively. Client side fetch works, but the snippet is not in the HTML that crawlers and archives see, and it appears after the page loads.
1
u/seweso 22d ago
https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/template
?
1
u/karmatics29 22d ago
For an archive, I'd render the shared pieces at build time with 11ty and keep the original URL paths as permalinks; you can leave each page's content as HTML rather than converting it. Before switching from fetch, I'd crawl the generated output against the old URL list—nested paths and relative asset links are easy to break even when the build succeeds.
1
u/ZestycloseCress447 20d ago
The fetch() approach works but the header and nav won't be in the HTML anymore, so anything that doesn't run JS (search engines on a bad day, archive crawlers, reader mode) sees pages with no navigation, and you get a little layout jump on every load. For an archive I'd do the 11ty route once and commit the built HTML, then you never have to think about it again.
1
u/micalm <script>alert('ha!')</script> 16d ago
Yes. Walk through your website directories with PHP, get the rendered content and save it as static HTML.
ob_start()->$html = ob_get_clean()->ob_end_clean()PHP is a hypertext preprocessor, it can do SSG since... forever? Probably not, but a long long time.
0
u/IncomeOk7237 22d ago
fetch() works but you get a flash of empty header and nav on load, and crawlers or no-js readers see nothing, which is bad for an archive. since it's only a few hundred pages, i'd just do a one time build step. 11ty or even a tiny node script that stitches header.html and footer.html into each page at build time. you keep the includes as separate files and the output is plain html, so cloudflare doesn't care.