(メモ) Next.js の middleware と edge 環境
Published: 2023/8/24
Next.js は Vercel が主導して開発していて、彼等のサービスは Edge 環境に力を入れている。
Edge Functions - Vercel
Edge middleware and functions give you all the benefits of static with the power of dynamic. Personalize and experiment without sacrificing page speed or site performance.
vercel.com

Edge 環境とは、 v8 エンジンの isolation 機能を利用して、ワーカーとして利用しているような環境。 サービスの具体例としては、 Cloudflare Workers が有名。 WebWorker 的な仕組みの流用のため、基本的にはブラウザ的な API しかランタイムに用意されておらず、 node.js と比較して実行できることに制限がかかる。
現時点(2023/08/24)では、 Next.js の middleware の runtime は必ず edge に設定される。
(edge 前提の webpack bundling が行われる。)
そのため、 NextAuth.js などで getServerSession
などを行おうとしても、できなかったりする。
middleware 機能を node 環境で動かすことは、主たる開発元(Vercel)の関心事から外れるため、この機能は中々 Next.js に入らなさそう、という備忘録。
サマリー FAQ
Q. Next.js の middleware でできることは?
Q. edge 環境とは何ですか?
Tags: next.js
関連記事
NextJs にブログを移行した
2021/5/3