TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How does a static site generator work?

11 pointsby litzerover 9 years ago
Short of reading source codes, I can't find any reading explaining how they work.

5 comments

patio11over 9 years ago
Short version: you maintain some data on our own machine which describe your content. Most commonly, this is done in flat files formatted with markdown or another plain-text option.<p>Your static site generator is built out of code. You execute it on your local machine, periodically, generally when you have a new blog post to publish or similar. The generator combines your content plus templates (either ones it ships with, ones you build, or ones you found from the community around that generator) to spit out a bunch of compiled HTML&#x2F;CSS&#x2F;Javascript.<p>You then upload the entire compiled site to your host of choice. From the perspective of someone visiting your website, they don&#x27;t know it was generated at all -- they just download HTML really, really fast which <i>appears</i> to have bells-and-whistles from dynamic sites like e.g. navigation which is consistent across multiple pages, related article links, etc etc.<p>There exist <i>numerous</i> other ways to achieve this objective, but the above description of a static site generator is the one most programmers independently re-invent when they attempt to build one. I had built four in my younger years before I realized that software like that had a name and was available on the Internet for free.
imakesnowflakesover 9 years ago
It takes text&#x2F;articles in markdown format [1] and converts it to html with some basic html tags, like headings, anchors and lists. Where these should occur can be declared using markdown syntax.<p>This html with simple html tags is injected into a bigger&#x2F;complex html (which often contains the main styling&#x2F;design) to form the final page of the site.<p>[1]<a href="https:&#x2F;&#x2F;daringfireball.net&#x2F;projects&#x2F;markdown&#x2F;syntax" rel="nofollow">https:&#x2F;&#x2F;daringfireball.net&#x2F;projects&#x2F;markdown&#x2F;syntax</a>
tomcamover 9 years ago
The simplest way to create a website is just HTML and CSS, right? You can type that shit up on your desktop, post it to a server, and voilà--website! Advantages: fastest possible performance and way less surface area for attacks.<p>Suppose you decide to add a footer to every page. If you&#x27;re not a wiz at sed and command-line scripting you&#x27;ll have a big chore ahead of you. Plus you might forget some pages. Plus logging into FileZilla or using SSH in order to post the files can be dreary and time-consuming.<p>If you&#x27;re putting up a catalog or other site derived from a database, you have a whole other set of problems. Now you really want to automate. You could write this in Symfony or web2py or Django an put it on your server. If people go to yoursite.com&#x2F;catalog&#x2F;flashlights it&#x27;s actually running a file called, say, flashlights.php that dynamically queries the database and creates HTML on the fly with a list of the flashlights category in your catalog. Advantage: it&#x27;s always up to date. Disadvantage: Slower (every visit to yoursite.com&#x2F;catalog&#x2F;flashlights results in a database hit) and much broader attack surface.<p>BTW that&#x27;s a little bit of a simplification. You can cache the results of the visit to yoursite.com&#x2F;catalog&#x2F;flashlights as an HTML file at the cost of added complexity.<p>A dynamic, data-driven site like that can sometimes be intercepted via evil methods such as a browser-issued SQL query or CSS injection (probably not with web2py because it&#x27;s got great security, but you get my point). Dynamically generated sites, which is most of the big ones, are particularly vulnerable because of the database. Database managers are big and complex.<p>OK, so imagine you have a program written in Python or C or Java or whatever on your desktop. It can easily be modified to add footers or render HTML files from a database, then post the pages to your server. Advantage: pre rendered HTML files are fast and way less likely to get hacked. Disadvantage: could be less up to date. The catalog database may be updated and the rendered HTML may lag.<p>If you really want to get fancy you can have the program running on the server. It goes through your database and templates to create each page of the site, but instead of creating dynamic pages that run PHP or Python in order to render a web page, it creates spits out straight HTML (aka &quot;static HTML&quot;) with the contents once a day or whatever. Now a visit to yoursite.com&#x2F;catalog&#x2F;flashlights actually resolves to yoursite.com&#x2F;catalog&#x2F;flashlights.html.<p>In theory static site generators are easier to understand and get started with. That&#x27;s not always true, but it is true that page renders are invariably faster because it&#x27;s just pure HTML and not database queries are needed.
评论 #10528873 未加载
budparrover 9 years ago
Some of the formatting is wonky, here, but you can read the post at the link at the bottom.<p>CONTENT IN<p>Most, but not all, static site generators use markdown files to store content, with information about each file of content specified using YAML formatted front matter. It looks like this:<p>--- title: &#x27;The Title of an Entry Here&#x27; date: 2015-04-15 category: &#x27;News&#x27; excerpt: &#x27;A short excerpt here&#x27; layout: default foo: &#x27;Bar&#x27; --- Some markdown content here, which is likely the longest part. This can be quite long, in fact.<p>HTML OUT<p>Now, imagine a folder of these markdown files on your hard drive. We&#x27;ll call this your project. The code of our static site generator could say &quot;for each markdown file in folder x, move it to folder y.&quot; But that&#x27;s not too exciting.<p>Let&#x27;s say, instead of just moving that file, the generator reads all the files in a certain directory or directories and creates an &quot;object&quot; with them: &quot;For each file in folder x, list its contents in our object.&quot; That object is just a stored list of all the files, including their file names, any meta-data specified in the front matter, and their content.<p>That object might look something like this (in Ruby, as output from Jeyll):<p><i></i>\{&quot;layout&quot;=&gt;&quot;default&quot;, &quot;title&quot;=&gt;&quot;The Title of an Entry Here&quot;, &quot;category&quot;=&gt;&quot;News&quot;, &quot;excerpt&quot;=&gt;&quot;A short excerpt here&quot;, &quot;published&quot;=&gt;true, &quot;url&quot;=&gt;&quot;&#x2F;news&#x2F;2015&#x2F;04&#x2F;14&#x2F;post-title-here&#x2F;&quot;, &quot;dir&quot;=&gt;&quot;&#x2F;news&#x2F;2015&#x2F;04&#x2F;14&quot;, &quot;date&quot;=&gt;2015-04-14 00:00:00 -0400, &quot;id&quot;=&gt;&quot;&#x2F;news&#x2F;2015&#x2F;04&#x2F;14&#x2F;post-title-here&quot;, &quot;categories&quot;=&gt;\[&quot;news&quot;], &quot;next&quot;=&gt;, &quot;previous&quot;=&gt;, &quot;tags&quot;=&gt;\[], &quot;path&quot;=&gt;&quot;\_posts&#x2F;2015-04-14-post-title-here.md&quot;, &quot;content&quot;=&gt;&quot;Some markdown content here, which is likely the longest part. This can be quite long, in fact.&quot;, &quot;section&quot;=&gt;&quot;post&quot;}<i></i><p>You can tell already that in creating the object the static site generator has extrapolated some information, like a &quot;url&quot; created from our specified date and category: &quot;For each file in folder x, write to our object and combine its category, date and file name to create a url in a format like &#x27;&#x2F;category&#x2F;yyyy-mm-dd&#x2F;original-base-filename.html&#x27;.&quot;<p>Once we&#x27;ve generated our object, the generator can do quite a bit with it. We can create an HTML page with the url as created above, and that file could be manipulated further using some of the meta-data we&#x27;ve specified.<p>More than likely, we don&#x27;t want to display content written in Markdown, so the generator will convert our markdown into HTML before outputting it to our new file: &quot;for each item in the object, convert the value of &#x27;content&#x27; from markdown to HTML.&quot;<p>And, of course, we don&#x27;t want to just display plain text, we want to style the page. We&#x27;ve likely created a set of templates in our project and specified in our content file which template to use for each file.<p>In our templates we lay out our HTML and also some tags that specify which parts of our file (the content or some piece of metadata) to display: &quot;For each item in our object, apply its content to the template specified as &#x27;layout&#x27; as we output it as HTML.&quot;<p>Each template may output other content too. A template might call: &quot;for each item in our object, loop through the ones that have a &#x27;category&#x27; equal to &#x27;news&#x27; and display the title.&quot;<p>So, along the way to just creating the HTML file, the generator will read the templates and output HTML in the format we&#x27;ve dictated. In the end, we have a new folder with our fully formed HTML files.<p>Asset files, like CSS and javascript are just moved into the new folder, so that in the end, that folder contains our fully formed website, which can then be moved to a web server.<p>So a static site generator converts one set of files into another using templates for instructions on how to output them in their final form.<p>If any of the formatting looks bad here, this is from my site &quot;Static is the New Dyanamic&quot; about static site generators: <a href="http:&#x2F;&#x2F;www.thenewdynamic.org&#x2F;articles&#x2F;how-do-static-site-generators-work&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.thenewdynamic.org&#x2F;articles&#x2F;how-do-static-site-gen...</a>
ghubbardover 9 years ago
How do you think they might work?