Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Patched fetch did not work
Search
Yosuke Kurami
September 18, 2024
Programming
800
6
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Patched fetch did not work
Yosuke Kurami
September 18, 2024
More Decks by Yosuke Kurami
See All by Yosuke Kurami
TypeScript LSP の今までとこれから
quramy
1
2.1k
フロントエンドテストの育て方
quramy
12
3.9k
App Router 悲喜交々
quramy
8
750
上手に付き合うコンポーネントテスト
quramy
6
2.4k
GraphQL あるいは React における自律的なデータ取得について
quramy
18
5.9k
Next.js App Router
quramy
15
3.9k
Fragment Composition of GraphQL
quramy
17
4.9k
reg-viz VRT tools
quramy
4
1.7k
NoInfer
quramy
0
400
Other Decks in Programming
See All in Programming
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
550
Foundation Models frameworkで画像分析
ryodeveloper
1
510
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
200
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
140
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.8k
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
0
140
What's New in Android 2026
veronikapj
0
250
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
620
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
170
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
150
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
170
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
200
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
450
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
480
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
920
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.8k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
270
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6k
First, design no harm
axbom
PRO
2
1.2k
Crafting Experiences
bethany
1
240
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
480
Abbi's Birthday
coloredviolet
3
9.1k
Transcript
PATCH didn't work @Quramy 2024.9.18
About me - id: @Quramy (GitHub, X) - ৬छ: Web
ϑϩϯτΤϯυΤϯδχΞ - Next.js ͱͷؔΘΓ: - gIP, gSSP, gSP (ISR), RSC ͷҰ௨Γܦݧࡁ - 20236݄ࠒ͔Β App Router ར༻Ҋ݅ͷ։ൃʹܞΘΔ
App Router Ͱຯʹࠔͬͨ͜ͱ - Next.js ͷ Cache ͷڍಈ - Datadog
APM ࿈ܞ - msw ࿈ܞ
App Router Ͱຯʹࠔͬͨ͜ͱ - Next.js ͷ Cache ͷڍಈ - Datadog
APM ࿈ܞ - msw ࿈ܞ ͜ΕΒʹڞ௨͍ͯ͠Δ͕͋Δ
Զͷ fetch ԚΕ͍ͯΔ
ઌʹஅ͓͖ͬͯ·͢ ࠓͷʹಛʹ༗ӹͳ݁͋Γ·ͤΜ
fetch API ਓؾऀ ҎԼશͯ globalThis.fetch ʹϞϯΩʔύονΛ͍ͯͯΔ - Next.js - Datadog
SDK - msw/node ࠓ fetch ύον͕িಥͨ͠Λ͠·͢
Next.js ͷ fetch ύον ͓͞Β͍
Request Memoization - App Router ʹ 4 छྨͷΩϟογϡ͕͋Δ - Request
Memoize ͱݺΕΔΩϟογϡ React ͕ fetch API ʹύον Λద༻͢Δ͜ͱͰ࣮ݱ͞Ε͍ͯΔ - ͨͩ͠ React 19 Ͱ֘ύονίʔυআ͞Εͨ ΘΓʹ Next.js ͕ fetch ʹಉ༷ͷύονΛద༻͢ΔΑ͏ʹͳ͍ͬͯΔ https://github.com/vercel/next.js/pull/65058/ fi les#diff- 9389e7475a89a826ddfd492b54b3314fc14d53b375f806f3197ed9a69 9e2b3a6
https://nextjs.org/docs/app/building-your-application/caching#request-memoization
Data Cache - App Router Ωϟογϡ ࢛ఱԦͷҰப, Data Cache -
ෳͷ Next.js ͷϦΫΤετؒͰڞ༗͞ΕΔΩϟογϡ (Request Memoize ϦΫΤετείʔϓ) - Data Cache fetch Λύον͢Δ͜ͱͰ࣮ݱ͞Ε͍ͯΔ const res = await fetch('https://dummyjson.com/products/1', { next: { revalidate: 3600, tags: ['product', '1'], }, }) const data = await res.json()
https://nextjs.org/docs/app/building-your-application/caching#data-cache
ΦϓτΞτͰ͖Δʁ - Request Memoize: جຊతʹΦϓτΞτෆՄೳ - Data Cache: fetch Φϓγϣϯʹ
cache-control: no-store Λ༩͢Δ͜ͱ ͰΦϓτΞτՄೳ ※1 Next.js v15 Ҏ߱Ͱ no-store ͕σϑΥϧτͱͳΔݟࠐΈ ※2 Data Cache ͷΦϓτΞτํ๏ଞʹଘࡏ͢Δׂ͕Ѫ - ʮNext.js ͕ fetch Λύον͢Δʯ͜ͱͦΕࣗମෆՄආ
ࠔͬͨ͜ͱᶃ Datadog APM
APM ͱࢄτϨʔε - Datadog APM ʹݶΒͣɺҰൠతʹࢄτϨʔεͰ Outgoing ͳ HTTP Request
ʹ HTTP Header ΛࠐΉ͜ͱͰαʔϏεؒͷ Span Λඥ͚ͮΔ - Next.js App Router Ͱ instrumentation.ts ͰܭΛॳظԽ͢Δ͜ͱ͕ ਪ͞Ε͍ͯΔ Next.js request GET / fetch GET http://my-service/api/hoge fetch GET http://my-service/api/fuga my-service request GET /api/hoge my-service request GET /api/fuga
Datadog APM https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation
Datadog APM - ͕ɺdd-trace(= Datadog APM SDK) ͕ instrumentation.ts
Ͱಈ͔ͳ͍ - https://github.com/DataDog/dd-trace-js/issues/3457 - ্هͷ issue ෳͷ͕ࠞࡏ͍ͯ͠Δ͕ʮNext.js - όοΫΤϯυ αʔϏε ؒͷ Span ͕ඥ͔ͮͳ͍ = ࢄτϨʔε͕࠾Εͳ͍ʯͱ͍͏ ࣄ͕ى͖ͨ - Next.js v14.1.0 Ͱमਖ਼ͣΈ
Datadog APM - fetch API ͷύονͷڝ߹͕ݪҼͩͬͨ - 1. Next.js
͕αʔόʔىಈ࣌ʹ fetch API ΛهԱ - 2. instrumentation.ts Ͱ dd-trace ͕ fetch Λύον - 3. Next.js 1. ͷ fetch ʹύονΛͯͯར༻ (2. ͷύον͕ແ͔ͬͨ ͜ͱʹ͞ΕΔ)
ࠔͬͨ͜ͱᶄ msw ͕͑ͳ͍
msw - msw ͱ - https://github.com/mswjs/msw - HTTP ௨৴ΛϞοΫ͢ΔͨΊͷϥΠϒϥϦ
Next.js request GET / fetch GET http://my-service/api/hoge fetch GET http://my-service/api/fuga
msw - msw ͲͷΑ͏ʹ fetch ΛΠϯλʔηϓτ͍ͯ͠Δͷ͔: - ϒϥβ: Service
Worker Ͱ HTTP ௨৴ΛΠϯλʔηϓτ - Node.js : fetch API ʹύονΛͯͯ HTTP ௨৴ΛΠϯλʔηϓτ
msw - React Server ଆͰಈ࡞ͤ͞ΔϞδϡʔϧ (e.g. Server Component
Server Action) ʹͯɺ msw/node ͕ར༻Ͱ͖ͳ͍ - https://github.com/mswjs/msw/issues/1644
IUUQTHJUIVCDPNNTXKTNTXJTTVFT
msw ҎԼίʔυύοτݟಈ࡞͢Δ͕ "next dev" ڥͰෆՄղͳڍಈΛࣔ͢ (Ϧϩʔυ͢Δͱϋϯυϥ͕ۭৼΔ) import { http,
HttpResponse } from 'msw' import { setupServer } from 'msw/node' if (process.env.NODE_ENV !== 'production') { const server = setupServer() server.use( http.get('https://dummyjson.com/products/1', async () => { return HttpResponse.json({ id: '1', name: 'Hi', price: 100, }) }), ) server.listen() } export default async function Home() { const res = await fetch('https://dummyjson.com/products/1') const data = await res.json() return <pre>{JSON.stringify(data, null, 2)}</pre> }
msw - https://github.com/vercel/next.js/pull/68193 Ͱमਖ਼͞ΕΔ ※ 20249݄ݱࡏ Next.js ҆ఆ൛ͱͯ͠ະϦϦʔε -
PR λΠτϧͷ "tweak fetch patch restoration timing during HMR to allow for userland fetch patching" ͔Β͕ͭ͘͠௨ΓɺNext.js ଆͷ Hot Module Replacement ͱ msw/node ͷ fetch ύον͕িಥ͍ͯͨ͠
HMR Cache https://nextjs.org/docs/app/api-reference/next-con fi g-js/serverComponentsHmrCache
(ิ) msw ͱ instrumentation.ts - Next.js v14.1~ Ͱ͋Εɺinstrumentation.ts Λ͏͜ͱͰ msw/node
ͷར༻ࣗମҰԠՄೳ - instrumentation.ts ʹؚ·ΕΔίʔυ dev-server ىಈޙʹมߋͯ͠ ࠶࣮ߦ͞ΕͣɺىಈதʹϋϯυϥͷมߋΛޮ͔ͤΔͨΊʹผͷϋοΫ ͕ඞཁͱͳΔ - msw ͷϢʔεέʔεΛߟ͑Δͱ instrumentation.ts ෆ͖
Next.js ύονΛΊΔͷ͔ʁ - ͦͦ Next.js ͕ fetch Λύον͠ͳ͚Εɺdd-trace msw
Λར༻͢ΔোนʹͳΒͳ͔ͬͨͣ - Next.js ଆʮfetch ͷ֦ுΛΊΔͭΓʯͱߘ͍ͯͨ͠ - https://twitter.com/leeerob/status/1733154383410684148 - v15 Ͱ fetch ͷσϑΥϧτڍಈมߋ͞ΕΔݟࠐΈ - ͔͠͠ɺReact ͷ fetch ύονऔࠐ HMR ΩϟογϡΛߟ͑Δ ͱɺʮfetch API ͷύονʯࣗମແ͘ͳΒͳ͍ͷͰ
Ͳ͏͠Α͏ͳ͍ͱ͖ - fetch Λύον͢ΔϥΠϒϥϦ͕ Next.js ্ ͱিಥͨ͠߹ͷϫʔΫΞϥ ϯυ: - NODE_OPTIONS
ͷ --import (·ͨ --require) Λར༻͢Δ (ΧελϜαʔόʔͰಉͷޮՌ͕ಘΒΕΔͣ) - Next.js ͕ globalThis.fetch Λೝࣝ͢Δલʹɺࣗͷ͍ͯͨύονΛ ͓ྃͤͯ͘͞ - ֖ͳ͍ํ๏Ͱ͋Δ͕ɺNext.js v14 Ͱ͋Ε͜Ε͕Ұ൪Ϛγ
·ͱΊ - Next.js fetch ʹύονΛ͍ͯͯΔ (Request Memoize, Data Cache,
HMR Cache) - Next.js v15 Ͱʮfetch ʹύον͕ͨΔ͜ͱʯࣗମ݈ࡏ - msw/node dd-trace ͷΑ͏ͳʮfetch ʹύονΛͯΔʯྨͷι Ϧϡʔγϣϯͱিಥ͢Δ͜ͱ͕͋Γɺಛʹ Next.js v14.x ෆ҆ఆͳڍಈ Λ͕ࣔͪ͠ - ബණͷ্ͰΪϦΓཱ͍ͬͯΔ
ࢀߟࢿྉ - Next.js ͷ fetch ֦ுͱΩϟογϡػߏͷҧ͍Λཧղ͢Δ https://speakerdeck.com/ryo_manba/fetch-kuo-zhang-tokiyatusiyuji- gou-nowei-iwoli-jie-suru - Why
Patching Globals Is Harmful https://kettanaito.com/blog/why-patching-globals-is-harmful
Thank you!