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
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
1.5k
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
170
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
220
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
250
What's New in Android 2026
veronikapj
0
260
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
320
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.5k
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
320
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
190
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
18k
数百円から始めるRuby電子工作
tarosay
0
140
Android CLI
fornewid
0
220
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
37
7.4k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Six Lessons from altMBA
skipperchong
29
4.4k
Testing 201, or: Great Expectations
jmmastey
46
8.2k
How to train your dragon (web standard)
notwaldorf
97
6.7k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Navigating Weather and Climate Data
rabernat
0
470
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
380
Rails Girls Zürich Keynote
gr2m
96
14k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
690
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!