r/webdev • • 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

33 comments sorted by

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/guitarromantic 22d ago

This is what I did when I wanted to archive a dynamic PHP site into static HTML.

In fact, I actually just used curl to download the entire thing (thankfully it was crawlable via link discovery) and then I just uploaded the entire thing as a static site, no need for the generator. Would that work, OP?

If you have no plans to update it in future then a static snapshot would be easier to achieve than trying to convert the layout into SSG templates.

2

u/Serpico99 22d ago

This could work too, but chances are you want to do some changes to the site from time to time on common layout elements without a mass search / replace, so it's a good idea to set it up beforehand. It's easy, just not as easy as a curl copy but not by much.

0

u/anakinmcfly 22d ago

It should work, though I do plan to eventually make some site-wide formatting changes such as simplifying the layout and removing some common elements, now that it’s going to be a reference archive rather than an active site with users.

So that’s what makes me hesitant about merging everything for each page, since right now all the formatting sits neatly in a file of its own and the individual pages are just content.

1

u/coopaliscious 22d ago

You are 'compiling' the HTML output, not keeping it that way in your source code.

1

u/anakinmcfly 22d ago

Oh! Thanks for clarifying. I looked up 11ty and it does sound like it should work for what I need. Thanks!

3

u/Discordbothost 22d ago

one nice thing for your case: you don't have to convert the pages to some new format. 11ty runs plain .html files through liquid by default, so put header.html / footer.html in _includes and swap each <?php include 'header.php'; ?> for {% include "header.html" %}. one find-and-replace across the few hundred files and you're done, and when you simplify the layout later you only touch the include

1

u/anakinmcfly 22d ago

I’ll do that, thanks! I’m figuring out the setup portion now since I’m completely new to this, but there are some tutorials online so I should manage.

1

u/anakinmcfly 21d ago

Having some trouble with the setup - where should the _includes folder go? I initially created it on the same level as _site, then followed the website’s tutorial and created _includes/mylayout.njk. But that confusingly resulted in an _includes folder appearing within the _site folder, with a /mylayout subfolder, which doesn’t seem right since these are supposed to be the input files.

I tried looking up directory configurations that said to put input files under a src folder, but when I created a src folder, a copy of it likewise appeared inside _site.

2

u/Discordbothost 21d ago

by default 11ty uses the folder you run it from as the input, so anything it doesn't recognize as special gets copied/rendered into _site. that's why your src showed up in there, it didn't know src was meant to be the input.

add an eleventy.config.js next to package.json:

export default function (eleventyConfig) {
  return { dir: { input: "src", output: "_site" } };
}

(use module.exports = function... if you're on the .cjs setup)

then the layout is src/_includes/mylayout.njk, your pages go in src/, and _site stays at the root outside src. includes is resolved relative to input, so _includes has to be inside src. delete _site and rebuild so the old junk goes away

1

u/anakinmcfly 19d ago

That worked, thank you so much! I had to change the extension to .mjs to get it to run, but I should be able to figure out the rest now.

1

u/Serpico99 22d ago

Sorry I didn't specify how it works, but yes, you keep the individual parts separate on your PC as source, so you have only a single source of truth for each shared piece of HTML, then run a command the "compiles" the static site

0

u/guitarromantic 22d ago

This is probably where I'd reach for Claude or similar. A repetitive, easy-to-test task – you could scrape the pages via curl, point Claude Code at it, and show it the fragments you want extracted into a standalone template. Then tell it to reconstruct everything else into Jekyll/Hugo pages etc.

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

1

u/anakinmcfly 22d ago

Yep, I'm looking into 11ty now! Some parts of my site are a bit more complicated because there's a variety of templates / secondary nav bars / nested includes, but I should be able to figure it out.

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/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.