TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Lessons from Building a Static Site Generator (2020)

104 点作者 ramshorst将近 4 年前

12 条评论

eatonphil将近 4 年前
This is a good review of a fairly complex piece of software. But don&#x27;t let this convince you that all static site generators must be complex.<p>Out of laziness, most sites I run have their own 100-200 line Python static site generator that takes Markdown (if I&#x27;m really feeling it) or HTML files with Jinja templates and generates pages around them. The core generator code hardly ever changes year by year. Here&#x27;s an example [0].<p>This isn&#x27;t to say that everyone should always write their own. I am just a bit surprised by all the debate around each generator because they all produce the same thing and the only (or major) variables are the template language and what themes are built in (though of course you can always bring your own CSS).<p>But _using_ a static site generator is a very good idea. If you have no other inclinations, I think the stack that makes sense for anyone with multiple contributors is to use WordPress for editing and then have a plugin that will generate static pages from it so not every request to your site hits the database.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;eatonphil&#x2F;notes.eatonphil.com&#x2F;blob&#x2F;master&#x2F;scripts&#x2F;build.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;eatonphil&#x2F;notes.eatonphil.com&#x2F;blob&#x2F;master...</a>
评论 #27693757 未加载
评论 #27689580 未加载
评论 #27689554 未加载
评论 #27690176 未加载
评论 #27688948 未加载
评论 #27690819 未加载
评论 #27694571 未加载
na85将近 4 年前
I went down this rabbit hole and found everything to be overcomplicated for my use case. I&#x27;m so sick of static site generators that have seven layers of templating engines and complicated build systems.<p>Most of the static site builders I tried were either way too complex, or else just straight-up didn&#x27;t work at all (looking at you, coleslaw).<p>I tried to go full emacs and use org export (org being my favorite text format) but the default export is horrendous and the documentation for org to html export is so bad it might as well not exist.<p>Software is simultaneously awesome and infuriating.<p>So after three days I just gave up on org-export and now I have pandoc shit out an html snippet that I concatenate with a hand-rolled html preamble and postamble via a Makefile.<p>Found a few rough edges, probably because org is underspecified. It&#x27;s not elegant but it works for my use case.
评论 #27691778 未加载
评论 #27691350 未加载
评论 #27691230 未加载
nickreese将近 4 年前
Hey all -- Author here. This was a reflection on building Elder.js.[0]<p>Happy to answer any questions. I&#x27;ll be going through and adding context to the questions I see.<p>[0]: <a href="https:&#x2F;&#x2F;elderguide.com&#x2F;tech&#x2F;elderjs&#x2F;" rel="nofollow">https:&#x2F;&#x2F;elderguide.com&#x2F;tech&#x2F;elderjs&#x2F;</a>
评论 #27693978 未加载
pier25将近 4 年前
This is a great post, but why doesn&#x27;t it have a date?<p>It&#x27;s infuriating to see blog posts or even news that don&#x27;t clearly display the publish date near the title.
评论 #27688958 未加载
评论 #27689567 未加载
评论 #27688966 未加载
评论 #27689217 未加载
评论 #27689666 未加载
klodolph将近 4 年前
I wrote a static site generator for my own personal site. I&#x27;ve been using it for over 10 years, and it&#x27;s gone through several major refactors &#x2F; redesigns. A few comments:<p>1. Template system<p>There are tons of different template systems out there for things like the &quot;shortcodes&quot; in the article.<p><pre><code> {{youtube id=&quot;123asdf4&quot; &#x2F;}} </code></pre> My conclusion is that the correct way to do this is with custom tags,<p><pre><code> &lt;embed-youtube id=&quot;123asdf4&quot;&gt;&lt;&#x2F;embed-youtube&gt; </code></pre> I apologize for the verbosity... but this is completely valid HTML5, and you do not need anything but an ordinary HTML5 parser to parse this. This maximizes your choices for the libraries you use in the static site generator and it maximizes the level of support in whatever editor you choose to author the site in. For example, you can just use the HTML mode in Vim or Emacs, or you can use VS Code, TextMate, Sublime Text, etc. and get a ton of features: syntax highlighting, indenting, etc.<p>While on the surface it looks verbose because of the closing tag, in most editors, you only have to press a key or two to close the tag. HTML5, strictly speaking, does not support self-closing tag syntax for custom tags. That syntax is only supported for void elements. There are only 16 void elements in HTML5.<p>I use &quot;prefix-suffix&quot; syntax to avoid ambiguity... any tag with a hyphen is obviously a custom tag.<p>2. Routing<p>Something you can use to tackle the routing complexity is to place your source files in the same path as the canonical URL. You only need routes for generated content, like index pages and such.<p>3. Index data<p>You&#x27;ll naturally want to generate indexes and create previews for links. I suggest that you start by looking at the schema.org schema for web pages and work with a useful subset of that. This way, you can generate indexes on your web page using the same exact data, same exact schema, that you use for the JSON-LD data you provide for search engines like Google.<p>This is a minor point, but it reduces duplicated effort between the code for generating content for your website and the code for generating JSON-LD metadata.<p>Don&#x27;t dive too deep into the schema.org schema, just take a couple bits and pieces that you need, and refer to the feature guides in Google&#x27;s documentation:<p><a href="https:&#x2F;&#x2F;developers.google.com&#x2F;search&#x2F;docs&#x2F;guides&#x2F;intro-structured-data" rel="nofollow">https:&#x2F;&#x2F;developers.google.com&#x2F;search&#x2F;docs&#x2F;guides&#x2F;intro-struc...</a>
评论 #27690441 未加载
评论 #27692478 未加载
z3t4将近 4 年前
If you are going static with a SSG I think you should embrace HTML5 semantic elements. Write your web site like it&#x27;s 1999! Only there are much more elements to choose from now with HTML5! Ohh and we have CSS! Don&#x27;t complicate things! The only real problem is how you get the marketing team to write HTML and use a version control system (git, mercurial, et.al). Or do you give up and give them a WYSIWYG content editor? The thing with semantic HTML is that it&#x27;s much nicer then a pile of of divs with classes sprinkled over it.
klelatti将近 4 年前
A big thank you to the author for open sourcing this - I&#x27;ve been playing with this to implement a largish static site (11,000 pages) and (as a definite non expert) have found it relatively easy to understand and use - and it&#x27;s lightning fast.<p>Just one comment: found implementing a Svelte Leaflet Map component a bit of a struggle - an addition to the docs on this would be very useful.
encryptluks2将近 4 年前
I&#x27;m a huge fan of Hugo because it is a single binary and full of useful features, however I certainly think there is a lot of room for improvement. While Go templating may not be everyone&#x27;s cup of tea, it is simple enough and the additional functions added by Hugo cover a lot of use cases.<p>Hugo has shown me that my ideal generator will likely be built with Go, although I could see a C, C++, or possibly Rust equivalent being fine for my needs. However, what I desire more than anything is to have a really performant default template using pagination and hierarchy based on the file system.<p>I don&#x27;t want to have to define titles or empty _index.md files to define sections, just want to point it at a nestable directory on my file system and use filenames for pages and directories for sections. This significantly improves the usefulness for anyone, because at that point it can be used as a knowledgebase&#x2F;note-taking system from the get-go without needing any custom templating to make it work out of the box.<p>The other important factor is building in search capabilities. While I think that client-side JavaScript and a JSON output works for this goal and can be optimized, Xapian would be a great alternative and extend the usefulness outside of the JavaScript browserbase. So far Hugo has left serving production sites up to third-party web servers, but I see no reason why they couldn&#x27;t finish implementing the `serve` components to be production-ready and implement a search component and possibly other dynamic components.<p>I really hope to see someone with a strong understanding of efficiency come in and own this arena building off Hugo&#x27;s single binary approach, but with an open mind to make it robust to handle all kinds of self-hosted content. Even implementing a bookmark manager using this approach would be awesome, having it auto-sort YAML&#x2F;TOML&#x2F;HCL or whatever and be able to serve up searchable bookmarks without even relying on JavaScript. The possibilities are endless with the types of apps that can be built with this design using readable files as data for the content.
whydoineedthis将近 4 年前
What is hydration? You talk a lot about it, but I&#x27;ve never had to add water to my websites before.
评论 #27691120 未加载
评论 #27691126 未加载
ghego1将近 4 年前
I will still encounter many of the issues that they also mentions.<p>In my experience, writing a static site generator is very much a continuous trade between flexibility and ease of use.<p>I also agree with most comments that it is often easier to write your own static site generator rather than finding the one to use amongst all the available choices.<p>That is why I made my own, and obviously I ended up writing a whole bunch of documentation because you can&#x27;t escape the need to clarify how things work.<p>If you&#x27;re interested you can check it out at <a href="https:&#x2F;&#x2F;yassb-foss.github.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;yassb-foss.github.io&#x2F;</a>
jrm4将近 4 年前
Alright, I&#x27;m skimming this whole idea of newfangled &quot;static site generators&quot; that involve a lot of Javascript and I&#x27;m left with a whole lot of &quot;Isn&#x27;t this just ____ with extra steps?&quot;<p>I&#x27;m seeing &quot;shortcodes&quot; and I&#x27;m like -- as in variables and&#x2F;or configuration files?<p>Or, more broadly -- why Javascript for the backend? This looks like a silly level of complexity. I&#x27;d start thinking about it in Bash and then probably head over to e.g. Python once databases et al start getting involved. What am I missing here?
评论 #27689494 未加载
评论 #27689969 未加载
评论 #27689591 未加载
评论 #27689298 未加载
EricE将近 4 年前
Having recently done a small Gatsby site I can identify with the comments about complexity over time. And graphql does seem like utter overkill too!