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
790
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.8k
App Router 悲喜交々
quramy
8
740
上手に付き合うコンポーネントテスト
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.8k
reg-viz VRT tools
quramy
4
1.7k
NoInfer
quramy
0
390
Other Decks in Programming
See All in Programming
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
190
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
150
【SRE NEXT 2026 Lunch Session】一人目専任SREの立ち上げを加速する ― AIと進めたオンボーディングで2分を0.04秒にした話
pkshadeck
PRO
0
2.3k
OS アップデート対応の取り組み方がもっと共有されてほしい
andpad
0
110
えっ!!コードを読まずに開発を!?
hananouchi
0
190
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.8k
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.1k
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
130
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
610
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
360
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
340
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
100
Featured
See All Featured
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
370
Building the Perfect Custom Keyboard
takai
2
810
Code Review Best Practice
trishagee
74
20k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
410
KATA
mclloyd
PRO
35
15k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
420
The Art of Programming - Codeland 2020
erikaheidi
57
14k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.3k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
490
The Invisible Side of Design
smashingmag
301
52k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
630
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
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!