Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
e868f77e
Unverified
Commit
e868f77e
authored
Jan 25, 2023
by
JJ Kasper
Committed by
GitHub
Jan 25, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update app cache handling in development (#45248)
parent
2725a379
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
12 deletions
+76
-12
app-render.tsx
packages/next/src/server/app-render.tsx
+2
-4
index.ts
packages/next/src/server/lib/incremental-cache/index.ts
+13
-6
app-static.test.ts
test/e2e/app-dir/app-static/app-static.test.ts
+61
-2
No files found.
packages/next/src/server/app-render.tsx
View file @
e868f77e
...
...
@@ -257,13 +257,10 @@ function createErrorHandler(
}
}
let
isFetchPatched
=
false
// we patch fetch to collect cache information used for
// determining if a page is static or not
function
patchFetch
(
ComponentMod
:
any
)
{
if
(
isFetchPatched
)
return
isFetchPatched
=
true
if
((
globalThis
.
fetch
as
any
).
patched
)
return
const
{
DynamicServerError
}
=
ComponentMod
.
serverHooks
as
typeof
import
(
'../client/components/hooks-server-context'
)
...
...
@@ -448,6 +445,7 @@ function patchFetch(ComponentMod: any) {
return
doOriginalFetch
()
}
;(
globalThis
.
fetch
as
any
).
patched
=
true
}
interface
FlightResponseRef
{
...
...
packages/next/src/server/lib/incremental-cache/index.ts
View file @
e868f77e
...
...
@@ -100,7 +100,7 @@ export class IncrementalCache {
this
.
cacheHandler
=
new
(
cacheHandlerMod
as
typeof
CacheHandler
)({
dev
,
fs
,
flushToDisk
,
flushToDisk
:
flushToDisk
&&
!
dev
,
serverDistDir
,
maxMemoryCacheSize
,
_appDir
:
!!
appDir
,
...
...
@@ -110,11 +110,12 @@ export class IncrementalCache {
private
calculateRevalidate
(
pathname
:
string
,
fromTime
:
number
fromTime
:
number
,
dev
?:
boolean
):
number
|
false
{
// in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging
if
(
this
.
dev
)
return
new
Date
().
getTime
()
-
1000
if
(
dev
)
return
new
Date
().
getTime
()
-
1000
// if an entry isn't present in routes we fallback to a default
// of revalidating after 1 second
...
...
@@ -174,7 +175,12 @@ export class IncrementalCache {
):
Promise
<
IncrementalCacheEntry
|
null
>
{
// we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging
if
(
this
.
dev
)
return
null
if
(
this
.
dev
&&
(
!
fetchCache
||
this
.
requestHeaders
[
'cache-control'
]
===
'no-cache'
)
)
{
return
null
}
pathname
=
this
.
_getPathname
(
pathname
,
fetchCache
)
let
entry
:
IncrementalCacheEntry
|
null
=
null
...
...
@@ -205,7 +211,8 @@ export class IncrementalCache {
this
.
prerenderManifest
.
routes
[
toRoute
(
pathname
)]?.
initialRevalidateSeconds
const
revalidateAfter
=
this
.
calculateRevalidate
(
pathname
,
cacheData
?.
lastModified
||
Date
.
now
()
cacheData
?.
lastModified
||
Date
.
now
(),
this
.
dev
&&
!
fetchCache
)
const
isStale
=
revalidateAfter
!==
false
&&
revalidateAfter
<
Date
.
now
()
...
...
@@ -248,7 +255,7 @@ export class IncrementalCache {
revalidateSeconds
?:
number
|
false
,
fetchCache
?:
boolean
)
{
if
(
this
.
dev
)
return
if
(
this
.
dev
&&
!
fetchCache
)
return
pathname
=
this
.
_getPathname
(
pathname
,
fetchCache
)
try
{
...
...
test/e2e/app-dir/app-static/app-static.test.ts
View file @
e868f77e
...
...
@@ -3,7 +3,7 @@ import cheerio from 'cheerio'
import
{
promisify
}
from
'util'
import
{
join
}
from
'path'
import
{
createNextDescribe
}
from
'e2e-utils'
import
{
check
,
normalizeRegEx
,
waitFor
}
from
'next-test-utils'
import
{
check
,
fetchViaHTTP
,
normalizeRegEx
,
waitFor
}
from
'next-test-utils'
import
stripAnsi
from
'strip-ansi'
const
glob
=
promisify
(
globOrig
)
...
...
@@ -289,7 +289,38 @@ createNextDescribe(
})
}
if
(
!
isDev
)
{
if
(
isDev
)
{
it
(
'should bypass fetch cache with cache-control: no-cache'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/variable-revalidate/revalidate-3'
)
expect
(
res
.
status
).
toBe
(
200
)
const
html
=
await
res
.
text
()
const
$
=
cheerio
.
load
(
html
)
const
layoutData
=
$
(
'#layout-data'
).
text
()
const
pageData
=
$
(
'#page-data'
).
text
()
const
res2
=
await
fetchViaHTTP
(
next
.
url
,
'/variable-revalidate/revalidate-3'
,
undefined
,
{
headers
:
{
'cache-control'
:
'no-cache'
,
},
}
)
expect
(
res2
.
status
).
toBe
(
200
)
const
html2
=
await
res2
.
text
()
const
$2
=
cheerio
.
load
(
html2
)
expect
(
$2
(
'#layout-data'
).
text
()).
not
.
toBe
(
layoutData
)
expect
(
$2
(
'#page-data'
).
text
()).
not
.
toBe
(
pageData
)
})
}
else
{
it
(
'should properly error when static page switches to dynamic at runtime'
,
async
()
=>
{
const
res
=
await
next
.
fetch
(
'/static-to-dynamic-error/static-bailout-1'
...
...
@@ -324,6 +355,34 @@ createNextDescribe(
})
}
it
(
'should honor fetch cache correctly'
,
async
()
=>
{
await
fetchViaHTTP
(
next
.
url
,
'/variable-revalidate/revalidate-3'
)
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/variable-revalidate/revalidate-3'
)
expect
(
res
.
status
).
toBe
(
200
)
const
html
=
await
res
.
text
()
const
$
=
cheerio
.
load
(
html
)
const
layoutData
=
$
(
'#layout-data'
).
text
()
const
pageData
=
$
(
'#page-data'
).
text
()
for
(
let
i
=
0
;
i
<
3
;
i
++
)
{
const
res2
=
await
fetchViaHTTP
(
next
.
url
,
'/variable-revalidate/revalidate-3'
)
expect
(
res2
.
status
).
toBe
(
200
)
const
html2
=
await
res2
.
text
()
const
$2
=
cheerio
.
load
(
html2
)
expect
(
$2
(
'#layout-data'
).
text
()).
toBe
(
layoutData
)
expect
(
$2
(
'#page-data'
).
text
()).
toBe
(
pageData
)
}
})
it
(
'Should not throw Dynamic Server Usage error when using generateStaticParams with previewData'
,
async
()
=>
{
const
browserOnIndexPage
=
await
next
.
browser
(
'/ssg-preview'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment