テーマ: javascript
-
ブラウザーストレージ:cookie、Web Storage、IndexedDB の比較
Cookie はリクエストごとに送られ、仕様上の容量は小さい。localStorage と sessionStorage はオリジンごとに数 MiB の同期的な文字列ストアで、IndexedDB は非同期かつトランザクション対応で、オリジンの大きな共通枠を使う。永続化が認められない限り、ブラウザーの保存はベストエフォートであり、オリジンごとに区切られる。
-
Using fetch with timeouts and AbortController
A fetch promise rejects only on network failure, not on HTTP error status, and it has no timeout by itself. Pass an AbortSignal combined from AbortSignal.timeout and a caller's controller, check response.ok, and tell TimeoutError, AbortError, network errors and HTTP errors apart in the catch.
-
ES module builds with declared side effects shrink consumer bundles more than CommonJS builds
Hypothesis: for a library of many independent functions, a consumer that imports a few of them gets a smaller bundle when the library ships ES modules with a sideEffects declaration than when it ships CommonJS, because tree shaking depends on static import/export structure; a fixture-based comparison is proposed.
-
Web Components:カスタム要素、Shadow DOM とカプセル化の境界
カスタム要素はハイフンを含むタグ名でクラスを登録し、ライフサイクルのコールバックを持ちます。Shadow DOM はスタイルが両方向に漏れないスコープ付きの部分木を与え、テンプレートと宣言的シャドウルートがマークアップを提供します。parts やカスタムプロパティーでの装飾から、ElementInternals でのフォーム参加、ラベルの関連付けまで、境界を越える機能は意図的に開く必要があります。
-
When does client-side routing still pay off now that browsers offer bfcache, prerendering and cross-document view transitions?
Open question: in-page routers were adopted to avoid full page loads, at the cost of shell serving, 404 handling, scroll and focus restoration and a bundle that must arrive first; the back/forward cache, the Speculation Rules API and cross-document view transitions now address the original motivations in multi-page sites. For which sites and interaction patterns does an in-page router still measurably win?
-
ロケールに対応する UI:Intl の複数形規則、日付、右から左のレイアウト
文字列を手作業で操作せず、ブラウザーの Intl オブジェクトで数値、日付、相対時刻を整形します。n == 1 ではなく Intl.PluralRules のカテゴリーで複数形を選び、CSS 論理プロパティーで dir=rtl の文書が反転するように配置し、翻訳で長くなるテキストの余地を確保します。
-
Node.js における ES モジュールと CommonJS
CommonJS は同期的な require と module.exports を使い、ES モジュールは静的な import/export、トップレベル await、import.meta を使う。Node は拡張子と最も近い package.json の type フィールドからファイルごとに形式を決める。ES モジュールでは完全なファイル拡張子が必要で、__dirname と require はないが、文書化された代替手段がある。
-
JavaScript のテキストオフセット:UTF-16 コード単位、コードポイント、見える文字を区別する
エディターや API の各境界でオフセットの単位を明示し、テキストを切り出す前に意識して変換する。
-
Rolling out a service worker safely: scope, versioned caches, the waiting worker and a kill switch
A service worker that caches HTML can pin users to old code after a deploy. Register it with an explicit scope, fetch the script fresh on every update check, version caches per build and delete old ones on activate, choose per-resource strategies, decide how the waiting worker takes over, and ship a tested kill switch before the first release.
-
Keyboard navigation in composite widgets: roving tabindex, arrow keys and Escape
Tab and Shift+Tab move between components; arrow keys move inside a component such as a tab list, toolbar, menu or radio group. Implement this with a roving tabindex (one item at tabindex=0, the rest at -1) or aria-activedescendant, close transient surfaces with Escape, and never trap focus without an advertised way out.
-
Focus management in single-page interactions: dialogs, route changes and removed elements
When a page changes without a full load, keyboard focus must be moved on purpose: into a dialog when it opens and back to its trigger when it closes, to the new view's heading after a client-side route change, and to a sensible neighbour when the focused element is deleted. Otherwise focus falls to the document body and screen-reader users are sent back to the top.
-
Promise と async/await のエラーを処理する
async 関数は throw を拒否に変え、await はそのエラーを再び送出する。すべての拒否には処理責任を持つ箇所が必要である。復旧できる処理だけを囲み、Promise を放置せず、部分的な失敗を許容できる場合は allSettled を使い、最後の備えとして unhandledrejection ハンドラーを登録する。
-
The JavaScript event loop: tasks, microtasks and rendering
JavaScript runs one job at a time; the HTML event loop takes a task, then drains the whole microtask queue (promise reactions, queueMicrotask) before the next task or a rendering opportunity. Long synchronous work and endless microtask chains block input and painting.
-
JSON での BigInt:replacer を追加する前に情報を失わないフィールド契約を定める
大きな整数を明示的なスキーマで符号化し、暗黙に Number を経由せずに復元します。
-
ビット操作の基礎:フラグ、マスク、シフトを正しく使う
フラグは二の累乗で、OR で組み合わせ、AND で調べ、AND NOT で消します。マスクとシフトでフィールドを取り出します。注意点は演算子の優先順位、符号付き右シフト、JavaScript の固定的な 32 ビット変換、Python の上限のない整数です。各フラグに名前を付け、判定には必ず括弧を使います。
機械可読: JSON