静态块 静态方法 静态变量_静态的力量

静态块 静态方法 静态变量

In this article I will quickly summarise the concept of the ‘static’ website: what it is, and what its benefits and drawbacks are.

在本文中,我将快速总结“静态”网站的概念:它是什么,它的优点和缺点是什么。

历史 (History)

In the beginning, all websites were collections of documents, the most significant of which were HTML (HyperText Markup Language). Such documents were simply plain text files, with special embedded commands to describe their structure, which could then be formatted by a web browser. As a basic example:

最初,所有网站都是文档的集合,其中最重要的是HTML(超文本标记语言)。 这些文档只是纯文本文件,带有特殊的嵌入式命令来描述其结构,然后可以通过网络浏览器对其进行格式化。 作为一个基本示例:

<p>This is an <strong>example</strong> paragraph.</p>

Saved as a file on a computer, such as index.html, this document could be accessed via a URL such as http://example.org/index.html.

该文档index.html为计算机上的文件(例如index.html ,可以通过诸如http://example.org/index.html的URL进行访问

As sites grew larger, and documents ever more complicated, various schemes were introduced to improve ‘modularity’; without this, documents (i.e. pages) with the same content — such as a footer — duplicate that content again and again. This is problematic if such content needs to be updated, in which case the same exact edit needs to be made many times over. If an individual page is overlooked, its content becomes outdated.

随着站点的扩大和文档的日益复杂,引入了各种方案来改善“模块化”。 否则,具有相同内容(如页脚)的文档(即页面)会一次又一次地复制该内容。 如果需要更新这样的内容,这是有问题的,在这种情况下,相同的精确编辑需要进行多次。 如果忽略单个页面,则其内容将过时。

One early scheme known as ‘Server Side Includes’ allowed a simple instruction — much like the <strong> in the previous example — to indicate that external content should be ‘inserted’ at that point. All the page-specific content was still present, but common content resided in additional files.

一种早期的方案称为“服务器端包含”,它允许一条简单的指令(类似于上一个示例中的<strong> ,指示此时应“插入”外部内容。 所有特定于页面的内容仍然存在,但是常见内容位于其他文件中。

Such approaches become ever more complex until, eventually, more and more content was help separately, away from the page files themselves. Some was generated dynamically via individual programs that ran on the web server — using this approach, for example, a page could include the current time of day or the number of times it had been read (‘hits’). This advanced to the stage at which intricate databases were used to fetch content related to the specific user viewing a page, and some sites — notably blogs — went a step further and even stored some of their content in a database.

这样的方法变得越来越复杂,直到最终,越来越多的内容被单独地帮助了,而不是页面文件本身。 其中一些是通过Web服务器上运行的各个程序动态生成的,例如,使用这种方法,页面可以包含一天中的当前时间或该页面的读取次数(“点击数”)。 这已经发展到使用复杂的数据库来获取与特定用户查看页面有关的内容的阶段,并且某些站点(尤其是博客)走得更远,甚至将其某些内容存储在数据库中。

That potted history is merely to introduce the concept of a scale with ‘static’ content at one end and ‘dynamic’ content at the other, where pages are self-contained at the former and made up of many collaborating parts at the latter.

那段历史只是为了介绍一种秤的概念,秤的一端为“静态”内容,另一端为“动态”内容,前者页面是独立的,后者则由许多协作部分组成。

今天 (Today)

In recent years, there has been a resurgence of static sites, for reasons we will get to shortly. However, these sites are often not as we used to know them; their pages are generated, not every time a visitor asks to read one, but during some ‘offline’ process, when they are first created or later updated. Such systems, in theory, take benefits from both ends of the spectrum.

近年来,由于我们不久将要访问的原因,静态站点重新兴起。 但是,这些站点通常不像我们以前所知道的那样。 它们的页面不是在每次访问者要求阅读时生成的,而是在某些“脱机”过程中(首次创建或后来更新)生成的。 从理论上讲,这样的系统将从频谱的两端受益。

I want to concentrate on sites that are truly static, rather than generated, and explain when they should be used, and what considerations should be made when evaluating alternatives.

我想专注于真正静态的站点,而不是生成站点,并解释何时应使用它们,以及在评估替代方案时应考虑哪些因素。

好处 (Advantages)

速度(Speed)

First, a static site (generated or hand-rolled) is faster — often much faster — than its dynamic cousin. In either case, the end result — an HTML page — is identical, but the dynamic version involves assembling separate parts, which always takes some time. If you’re familiar with an existing dynamic site and you then experience a static equivalent, the experience can be eye-opening: navigating from page-to-page is simply a smoother, more pleasant thing to do.

首先,静态站点(生成或手动滚动)比其动态表亲更快(通常更快)。 无论哪种情况,最终结果(HTML页面)都是相同的,但是动态版本涉及组装单独的零件,这总是要花费一些时间。 如果您熟悉现有的动态站点,然后体验到静态等效站点,那么这种体验可能会令人大开眼界:逐页导航只是一件更平滑,更愉快的事情。

透明度 (Transparency)

Eric Raymond describes the general concept of ‘transparency’ in his book, The Art of Unix Programming. In short, it refers to how easily something can be understood just by looking at it. Our static pages, being complete and self-contained, are far more transparent than the equivalent dynamic pages which, by definition, are composed of multiple parts whose actual locations are not always obvious.

埃里克·雷蒙德(Eric Raymond )在他的《 Unix编程的艺术一书中描述了“透明性”的一般概念 简而言之,它是指仅通过查看就可以轻松理解某些内容。 我们的静态页面完整且自包含,比等效的动态页面透明得多,等效的动态页面根据定义由多个部分组成,这些部分的实际位置并不总是很明显。

If you’ve ever worked on a dynamic site and struggled to find out just where you should ‘go’ to update the copyright year in the bottom of every page, you’ll recognise how valuable transparency can be.

如果您曾经在一个动态站点上工作,并且在每个页面的底部都很难找到应该“去”更新版权年份的位置,那么您将认识到透明度的价值。

更新 (Updates)

Somewhat related, if you need to update content, it couldn’t be simpler when you’re dealing with a static page: go update the file. You can do it in seconds. Even if you know exactly how to update a dynamic page, it’s almost always a more involved process: you might need to tweak a database value, you might need to run a script to reprocess certain templates, etc.

有点相关,如果您需要更新内容,那么在处理静态页面时再简单不过了:去更新文件。 您可以在几秒钟内完成。 即使您确切地知道如何更新动态页面,也几乎总是一个更复杂的过程:您可能需要调整数据库值,您可能需要运行脚本来重新处理某些模板,等等。

工装 (Tooling)

Finally, the tools available for managing a static website are generally more numerous and powerful; they are the same tools we use for editing any text files. On a Unix-like system, tools such as find, grep, wc, and hundreds of others are well-established and incredibly powerful in the right hands.

最后,用于管理静态网站的工具通常更多,功能更强大。 它们与我们用于编辑任何文本文件的工具相同。 在类似Unix的系统上,诸如findgrepwc以及数百种其他工具之类的工具在wc下已经建立并且非常强大。

We can even edit our static pages using the simplest of tools; most dynamic sites have far greater barriers to entry. In the blink of an eye, I can tell you which pages on a static site contain the phrase “© 2019”; in the case of a dynamic site, that is likely to be a far, far more complex task.

我们甚至可以使用最简单的工具来编辑静态页面。 大多数动态网站的进入壁垒更大。 眨眼间,我可以告诉您静态站点上的哪些页面包含短语“©2019”; 对于动态网站,这可能是一项远为复杂的任务。

缺点 (Disadvantages)

Of course, the prime disadvantage is a loss of some flexibility; if you just have a single static index.html file representing your home page, that’s the same page all your visitors will see. Personalisation is out the window.

当然,最主要的缺点是失去了一些灵活性。 如果您只有一个静态index.html文件代表您的主页,那么所有访问者都将看到同一页面。 个性化是不可能的。

Or, at least, server-side personalisation is. With the ever-increasing popularity of Javascript, options exist to ‘layer’ personalisation on top of a static page, once it has been delivered to the user’s browser. Javascript processing happens on the client-side, not on the server, so it doesn’t have quite the same performance hit at scale.

或者至少是服务器端的个性化。 随着Javascript的日益普及,一旦将其交付给用户的浏览器,就可以在静态页面的顶部进行“分层”个性化设置。 Javascript处理发生在客户端,而不是服务器,因此在规模上并没有完全相同的性能。

Maintaining a static-site can also be daunting: if you have that footer present in hundreds of files, and want to update it, how do you go about it? I refer you back to the section on ‘tooling’ up above; although we might need to do some work here to set a system up, we have the benefit of files that are very easy to work with, that we can use a whole host of tools to examine, and manipulate. Even a fully static site can be kept up-to-date by a novice programmer with very little effort.

维护静态站点也可能是艰巨的:如果您在数百个文件中都存在该页脚,并且想要对其进行更新,您将如何处理? 我请您回到上面有关“工具”的部分; 尽管我们可能需要在此处进行一些工作来建立系统,但我们拥有易于使用的文件的优点,可以使用大量工具进行检查和操作。 即使是一个完全静态的站点,新手程序员也可以毫不费力地保持最新。

可移植性 (Portability)

In conclusion, I’ll introduce a final benefit which has many positive repercussions: portability. A static site can be transported, in its entirety, from A to B on a USB stick with great ease. With minimal planning, it can be viewed in any location with almost no effort at all. A dynamic site, in contrast, requires much in the way of scaffolding and initial setup, just to be visible in the first place.

最后,我将介绍一个最终的好处,它具有许多积极的影响:可移植性。 静态站点可以通过USB记忆棒从A到B进行完整的整体传输。 通过最少的计划,几乎可以毫不费力地在任何位置查看它。 相比之下,动态站点需要大量的脚手架和初始设置,才能首先显示出来。

One of those positive side-effects is the issue of hosting. The immensely popular development site, GitHub, offers a free hosting service for static pages known as GitHub Pages. If your website is static, you can host it on GitHub Pages for absolutely zero cost.

这些积极的副作用之一是托管问题。 广受欢迎的开发站点GitHub为名为GitHub Pages的静态页面提供免费托管服务。 如果您的网站是静态的,则可以将其托管在GitHub Pages上,且费用绝对为零。

If nothing else can convince you to at least investigate the possibility of running your website as a static one, I hope the promise of saving 100% of your hosting costs is enough of an incentive!

如果没有其他方法可以说服您至少研究将您的网站作为静态网站运行的可能性,那么我希望节省100%托管费用的承诺就足够了!

翻译自: https://medium.com/@bobbyjack/the-power-of-static-20e4f7768c3d

静态块 静态方法 静态变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值