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
780
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
2k
フロントエンドテストの育て方
quramy
12
3.8k
App Router 悲喜交々
quramy
8
730
上手に付き合うコンポーネントテスト
quramy
6
2.4k
GraphQL あるいは React における自律的なデータ取得について
quramy
18
5.8k
Next.js App Router
quramy
15
3.9k
Fragment Composition of GraphQL
quramy
17
4.8k
reg-viz VRT tools
quramy
4
1.7k
NoInfer
quramy
0
380
Other Decks in Programming
See All in Programming
Agentic UI
manfredsteyer
PRO
0
160
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
790
Lessons from Spec-Driven Development
simas
PRO
0
200
AI時代のUIはどこへ行く?その2!
yusukebe
21
7.2k
ふつうのFeature Flag実践入門
irof
7
4k
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
550
CSC307 Lecture 17
javiergs
PRO
0
320
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
180
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
Contextとはなにか
chiroruxx
1
330
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
2
680
「エンジニアインターン、どうやって取った?」準備のリアルを語るLT会 Progate BAR
akiomatic
0
130
Featured
See All Featured
sira's awesome portfolio website redesign presentation
elsirapls
0
280
Code Reviewing Like a Champion
maltzj
528
40k
WCS-LA-2024
lcolladotor
0
640
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
How to Ace a Technical Interview
jacobian
281
24k
The Curious Case for Waylosing
cassininazir
1
390
My Coaching Mixtape
mlcsv
0
150
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
240
Documentation Writing (for coders)
carmenintech
77
5.4k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
410
The Spectacular Lies of Maps
axbom
PRO
1
810
Design in an AI World
tapps
1
240
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!