-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: tweak fetch patch restoration timing during HMR to allow for us…
…erland fetch patching (#68193)
- Loading branch information
1 parent
bfa7df4
commit 15aeb92
Showing
11 changed files
with
158 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react' | ||
import { ReactNode } from 'react' | ||
|
||
const magicNumber = Math.random() | ||
const originalFetch = globalThis.fetch | ||
|
||
if (originalFetch.name === 'monkeyPatchedFetch') { | ||
throw new Error( | ||
'Patching over already patched fetch. This creates a memory leak.' | ||
) | ||
} | ||
|
||
globalThis.fetch = async function monkeyPatchedFetch( | ||
resource: URL | RequestInfo, | ||
options?: RequestInit | ||
) { | ||
const request = new Request(resource) | ||
|
||
if (request.url === 'http://fake.url/secret') { | ||
return new Response('monkey patching is fun') | ||
} | ||
|
||
if (request.url === 'http://fake.url/magic-number') { | ||
return new Response(magicNumber.toString()) | ||
} | ||
|
||
return originalFetch(resource, options) | ||
} | ||
|
||
export default function Root({ children }: { children: ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default async function Page() { | ||
const secret = (await fetch('http://fake.url/secret').then((res) => | ||
res.text() | ||
)) as any | ||
const magicNumber = (await fetch('http://fake.url/magic-number').then((res) => | ||
res.text() | ||
)) as any | ||
|
||
return ( | ||
<> | ||
<div id="update">touch to trigger HMR</div> | ||
<div id="secret">{secret}</div> | ||
<div id="magic-number">{magicNumber}</div> | ||
</> | ||
) | ||
} |
40 changes: 40 additions & 0 deletions
40
test/development/app-dir/dev-fetch-hmr/dev-fetch-hmr.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { retry } from 'next-test-utils' | ||
|
||
import cheerio from 'cheerio' | ||
|
||
describe('dev-fetch-hmr', () => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
it('should retain module level fetch patching', async () => { | ||
const html = await next.render('/') | ||
expect(html).toContain('monkey patching is fun') | ||
|
||
const magicNumber = cheerio.load(html)('#magic-number').text() | ||
|
||
const html2 = await next.render('/') | ||
expect(html2).toContain('monkey patching is fun') | ||
const magicNumber2 = cheerio.load(html2)('#magic-number').text() | ||
// Module was not re-evaluated | ||
expect(magicNumber2).toBe(magicNumber) | ||
const update = cheerio.load(html2)('#update').text() | ||
expect(update).toBe('touch to trigger HMR') | ||
|
||
// trigger HMR | ||
await next.patchFile('app/page.tsx', (content) => | ||
content.replace('touch to trigger HMR', 'touch to trigger HMR 2') | ||
) | ||
|
||
await retry(async () => { | ||
const html3 = await next.render('/') | ||
const update2 = cheerio.load(html3)('#update').text() | ||
expect(update2).toBe('touch to trigger HMR 2') | ||
const magicNumber3 = cheerio.load(html3)('#magic-number').text() | ||
expect(html3).toContain('monkey patching is fun') | ||
// Module was re-evaluated | ||
expect(magicNumber3).not.toEqual(magicNumber) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = {} | ||
|
||
module.exports = nextConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"noEmit": true, | ||
"incremental": true, | ||
"module": "esnext", | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
] | ||
}, | ||
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |