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
810
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.2k
フロントエンドテストの育て方
quramy
12
3.9k
App Router 悲喜交々
quramy
8
760
上手に付き合うコンポーネントテスト
quramy
6
2.4k
GraphQL あるいは React における自律的なデータ取得について
quramy
18
5.9k
Next.js App Router
quramy
15
4k
Fragment Composition of GraphQL
quramy
17
4.9k
reg-viz VRT tools
quramy
4
1.8k
NoInfer
quramy
0
400
Other Decks in Programming
See All in Programming
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
120
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.4k
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
130
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.8k
Pythonの実行はどこまで賢くなったのか? CPythonとPyPyから見る最適化のしくみ
curekoshimizu
4
2.3k
Deep dive into the select statement (GopherCon UK)
jespino
0
160
Cloudflare is Agents
chimame
0
190
AIに既存システムを理解させる技術 ~レガシーを見捨てないハーネスエンジニアリング入門~
ochtum
0
180
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
840
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
4k
高専キャリア LT 発表内容
crysta1221
6
5.2k
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
290
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
310
How to build a perfect <img>
jonoalderson
1
5.9k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
510
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
450
WCS-LA-2024
lcolladotor
0
810
4 Signs Your Business is Dying
shpigford
187
23k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
400
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
310
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Ruling the World: When Life Gets Gamed
codingconduct
0
310
Build your cross-platform service in a week with App Engine
jlugia
234
19k
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!