5

GOV.UK 拔掉網頁上的 jQuery

 2 years ago
source link: https://blog.gslin.org/archives/2022/05/20/10708/gov-uk-%e6%8b%94%e6%8e%89%e7%b6%b2%e9%a0%81%e4%b8%8a%e7%9a%84-jquery/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

GOV.UK 拔掉網頁上的 jQuery

英國政府的網站拔掉 jQuery 了:「GOV.UK drops jQuery from their front end.」,Hacker News 上的討論也可以看一下:「Gov.uk drops jQuery from their front end (web.dev)」。

當年會選擇用 jQuery 大概有幾個原因,第一個是當年 (很舊的 browser 版本) 對 DOM 的操作非常的混亂,像是:

而 jQuery 在那個年代就已經把這堆 DOM operation 都窮舉支援了 (可以直接看「Category: DOM Insertion, Around」、「Category: DOM Insertion, Inside」、「Category: DOM Insertion, Outside」這三個大分類),可以注意 jQuery 1.0 就已經把基本界面都弄出來了,而 jQuery 1.0 是 2006 年八月出的,另外 IE7 是在 2006 年十月出,也就是說在 IE6 的年代就提供一整套完整的方案。

另外 jQuery 幫忙處理了早期 IE 與 W3C 標準的不一致行為,像是經典的 attachEvent (出自 DOM events):

Microsoft Internet Explorer prior to version 8 does not follow the W3C model, as its own model was created prior to the ratification of the W3C standard. Internet Explorer 9 follows DOM level 3 events, and Internet Explorer 11 deletes its support for Microsoft-specific model.

就功能面上來說,jQuery 提供的 Sizzle engine 也提供了 CSS selector 的能力,這在早期還沒有 querySelectorAll() (IE9+) 的時候方便不少,而且就算有了 querySelectorAll(),Sizzle 支援的 CSS selector 更完整。

上面提到的解決 browser 早期的各種亂象,jQuery 其實也帶入了不少好用的 pattern,其中一個是 fluent interface 讓人寫起來很舒服:(這個範例只是要介紹 fluent interface,不要管實際上在亂搞什麼 XD)

$('#foo').html('<p>bar</p>').css('width: 100px;');

另外就是不需要對 null object 做太多處理:

$('#foo').css('width: 100px;');

與這樣比較:

let elem = document.querySelector('#foo');
if (elem) {
    // ...
}

不過在這些年,負面的部份已經大幅改善了,所以也陸陸續續可以看到很多人在討論要怎麼拔掉 jQuery。而這次英國的 GOV.UK 拔掉 jQuery 有看到一些效果:

  • Less front end processing time overall.
  • 11% less blocking time at the 75th percentile.
  • 10% less blocking time for users at the 95th percentile. These are users who experience seriously adverse network and device conditions, and every performance gain matters especially for them.

但說實話,~10% 左右的 performance 改變比預期中少很多耶?可以看出來 John Resig 當年在上面為了效能花了多少功夫...

這次的結果反倒是讓我在思考,如果可以用 jQuery 降低開發的瓶頸,我還蠻偏好就拿 jQuery 進來用...

Related

GitHub 的網站拿掉 jQuery 了...

在 Twitter 上看到 GitHub 把網站上的 jQuery 都拔乾淨了。因為現代瀏覽器大多都可以直接來,或是有其他方案可以用: We’re finally finished removing jQuery from https://t.co/r2QL2aHBfa frontend. What did we replace it with? No framework whatsoever:• querySelectorAll,• fetch for ajax,• delegated-events for event handling,• polyfills for standard DOM stuff,• CustomElements on the rise.— Mislav Marohnić (@mislav) July 25, 2018 querySelectorAll 是 IE9+…

July 26, 2018

In "Browser"

Trac 1.2 上惡搞 TracTicketReferencePlugin 讓他會動...

TracTicketReferencePlugin 是個 Trac 上設定 ticket 之間關聯性的 plugin,與子母票有比較強烈的關係不同,有些人喜歡把相關的資料都掛在這邊。先前用都是在 Trac 1.0 上用,也沒什麼問題,最近在 Trac 1.2 上跑發現直接有 js error... 原因是 TracTicketReferencePlugin 用到 jQuery.live(),而這個函式在 jQuery 1.7 被宣告 deprecated,在 jQuery 1.9 被正式拔掉了。而 Trac 1.0 用的是 jQuery 1.7,所以不會有問題;Trac 1.2 用的是 jQuery 1.12,於是就爛掉了... 剛好就是昨天,有人在作者的 repository 上發 issue 出來 (另外也提供了 patch):「t2y / trac.plugins.ticketref / issues / #9 - JavaScript…

April 18, 2018

In "Computer"

無名 Blog 匯出成 MT 格式

給不想看下面說明的人:備份服務的網址是 http://backup.hasname.com/blog/wretch/。 注意:這項服務還有一些小問題,有可能隨時都在改 code。 2006/10/05 20:54 更新:現在的版本會多開幾條連線平行化處理,下載的速度應該會快很多。 2006/10/05 17:30 更新:現在下載的檔名會是 backup-${username}.txt 了,這樣應該比較方便。 雖然在去年六月的時候為了幫 ashley 大姊姊 (a.k.a. 電視兒童) 從 無名 跳出來而用 Perl 寫了一個小程式,將 無名 Blog 上的文章匯出成 RSS 2.0 格式,再匯入 WordPress 裡。後來這個小程式就再加強一下,寫了一個網頁並公開出來 (參考 無名小站的 Blog 與 Album 備份及還原服務 這篇文章),並且希望 無名 提供更完整的匯出及匯入服務。 後來 養樂多 (Yam Roodo) 的 Blog 服務 提供 MT 格式 的匯出與匯入,而國外…

October 4, 2006

In "Blog"

a611ee8db44c8d03a20edf0bf5a71d80?s=49&d=identicon&r=gAuthor Gea-Suan LinPosted on May 20, 2022May 20, 2022Categories Browser, Computer, Library, Murmuring, Network, Software, WWWTags browser, end, front, frontend, gov, javascript, jquery, js, kingdom, performance, uk, united, web

One thought on “GOV.UK 拔掉網頁上的 jQuery”

  1. 5ea6397c979cd9b12d332867f6d26efa?s=42&d=identicon&r=gChan says:

    個人覺得 jQuery 還是相當好用....尤其是處理 ajax 的部分

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK