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
9b66f6e8
Commit
9b66f6e8
authored
Apr 09, 2026
by
Luke Sandberg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
safari fix
parent
d0f6ad53
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
28 deletions
+68
-28
_document.tsx
packages/next/src/pages/_document.tsx
+5
-5
render.tsx
packages/next/src/server/render.tsx
+29
-18
html-context.shared-runtime.ts
packages/next/src/shared/lib/html-context.shared-runtime.ts
+13
-0
rendering.test.ts
test/e2e/app-document/rendering.test.ts
+21
-5
No files found.
packages/next/src/pages/_document.tsx
View file @
9b66f6e8
...
...
@@ -384,7 +384,7 @@ export class Head extends React.Component<HeadProps> {
getCssLinks
(
files
:
DocumentFiles
):
JSX
.
Element
[]
|
null
{
const
{
assetPrefix
,
a
ssetQueryString
,
cssA
ssetQueryString
,
dynamicImports
,
dynamicCssManifest
,
crossOrigin
,
...
...
@@ -422,7 +422,7 @@ export class Head extends React.Component<HeadProps> {
rel=
"preload"
href=
{
`${assetPrefix}/_next/${encodeURIPath(
file
)}${
a
ssetQueryString}`
}
)}${
cssA
ssetQueryString}`
}
as=
"style"
crossOrigin=
{
this
.
props
.
crossOrigin
||
crossOrigin
}
/>
...
...
@@ -436,7 +436,7 @@ export class Head extends React.Component<HeadProps> {
rel=
"stylesheet"
href=
{
`${assetPrefix}/_next/${encodeURIPath(
file
)}${
a
ssetQueryString}`
}
)}${
cssA
ssetQueryString}`
}
crossOrigin=
{
this
.
props
.
crossOrigin
||
crossOrigin
}
data
-
n
-
g=
{
isUnmanagedFile
?
undefined
:
isSharedFile
?
''
:
undefined
}
data
-
n
-
p=
{
...
...
@@ -589,7 +589,7 @@ export class Head extends React.Component<HeadProps> {
optimizeCss
,
assetPrefix
,
nextFontManifest
,
a
ssetQueryString
,
cssA
ssetQueryString
,
}
=
this
.
context
const
disableRuntimeJS
=
unstable_runtimeJS
===
false
...
...
@@ -659,7 +659,7 @@ export class Head extends React.Component<HeadProps> {
nextFontManifest
,
dangerousAsPath
,
assetPrefix
,
a
ssetQueryString
cssA
ssetQueryString
)
const
tracingMetadata
=
getTracedMetadata
(
...
...
packages/next/src/server/render.tsx
View file @
9b66f6e8
...
...
@@ -458,34 +458,44 @@ export async function renderToHTMLImpl(
// Adds support for reading `cookies` in `getServerSideProps` when SSR.
setLazyProp
({
req
:
req
as
any
},
'cookies'
,
getCookieParser
(
req
.
headers
))
let
baseAssetQueryString
=
// cacheBuster is a workaround for a Safari bug
// (https://bugs.webkit.org/show_bug.cgi?id=187726) where preloaded CSS
// resources are cached and not re-fetched on HMR. It must only be applied
// to CSS and font assets — not to script tags — because the Turbopack
// runtime infers ASSET_SUFFIX from the executing script's query string and
// leaks it onto all static asset URLs (including images), causing
// next/image validation errors.
// See https://github.com/vercel/next.js/issues/92118.
let
cacheBuster
=
(
process
.
env
.
__NEXT_DEV_SERVER
&&
renderOpts
.
assetQueryString
)
||
''
if
(
process
.
env
.
__NEXT_DEV_SERVER
&&
!
baseAssetQueryString
)
{
if
(
process
.
env
.
__NEXT_DEV_SERVER
&&
!
cacheBuster
)
{
const
userAgent
=
(
req
.
headers
[
'user-agent'
]
||
''
).
toLowerCase
()
if
(
userAgent
.
includes
(
'safari'
)
&&
!
userAgent
.
includes
(
'chrome'
))
{
// In dev we invalidate the cache by appending a timestamp to the resource URL.
// This is a workaround to fix https://github.com/vercel/next.js/issues/5860
// TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed.
// Note: The workaround breaks breakpoints on reload since the script url always changes,
// so we only apply it to Safari.
baseAssetQueryString
=
`?ts=
${
Date
.
now
()}
`
cacheBuster
=
`?ts=
${
Date
.
now
()}
`
}
}
const
mutableAssetQueryString
=
baseAssetQueryString
+
(
sharedContext
.
deploymentId
?
`
${
baseAssetQueryString
?
'&'
:
'?'
}
dpl=
${
sharedContext
.
deploymentId
}
`
:
''
)
const
assetQueryString
=
baseAssetQueryString
+
const
mutableAssetQueryString
=
sharedContext
.
deploymentId
?
`?dpl=
${
sharedContext
.
deploymentId
}
`
:
''
const
assetQueryString
=
sharedContext
.
clientAssetToken
?
`?dpl=
${
sharedContext
.
clientAssetToken
}
`
:
''
// cssAssetQueryString is assetQueryString with the cacheBuster prepended.
// Use this for CSS and font URLs; use assetQueryString for script URLs.
const
cssAssetQueryString
=
cacheBuster
+
(
sharedContext
.
clientAssetToken
?
`
${
baseAssetQueryString
?
'&'
:
'?'
}
dpl=
${
sharedContext
.
clientAssetToken
}
`
?
`
${
cacheBuster
?
'&'
:
'?'
}
dpl=
${
sharedContext
.
clientAssetToken
}
`
:
''
)
const
metadata
:
PagesRenderResultMetadata
=
{
assetQueryString
,
mutableAssetQueryString
,
assetQueryString
:
cssAssetQueryString
,
mutableAssetQueryString
:
cacheBuster
+
(
sharedContext
.
deploymentId
?
`
${
cacheBuster
?
'&'
:
'?'
}
dpl=
${
sharedContext
.
deploymentId
}
`
:
''
),
}
// don't modify original query object
...
...
@@ -1531,6 +1541,7 @@ export async function renderToHTMLImpl(
:
undefined
,
unstable_JsPreload
:
pageConfig
.
unstable_JsPreload
,
assetQueryString
:
assetQueryString
||
''
,
cssAssetQueryString
:
cssAssetQueryString
||
''
,
mutableAssetQueryString
:
mutableAssetQueryString
||
''
,
scriptLoader
,
locale
,
...
...
packages/next/src/shared/lib/html-context.shared-runtime.ts
View file @
9b66f6e8
...
...
@@ -31,6 +31,19 @@ export type HtmlProps = {
unstable_JsPreload
?:
false
assetQueryString
:
string
mutableAssetQueryString
:
string
/**
* Asset query string for CSS and font assets. Includes both the deployment
* token (if any) and a cache-busting parameter for Safari
* (https://bugs.webkit.org/show_bug.cgi?id=187726) in dev. Use this for
* `<link rel="stylesheet">`, `<link rel="preload" as="style">`, and font
* preload tags.
*
* Must NOT be used for script tags — the Turbopack runtime infers
* ASSET_SUFFIX from the executing script's query string and leaks it onto
* all static asset URLs, causing next/image validation errors.
* See https://github.com/vercel/next.js/issues/92118.
*/
cssAssetQueryString
:
string
scriptLoader
:
{
afterInteractive
?:
string
[]
beforeInteractive
?:
any
[]
...
...
test/e2e/app-document/rendering.test.ts
View file @
9b66f6e8
...
...
@@ -81,19 +81,35 @@ describe('Document and App - Rendering via HTTP', () => {
})
if
(
isNextDev
)
{
// This is a workaround to fix https://github.com/vercel/next.js/issues/5860
// TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed.
it
(
'adds a timestamp to link tags with preload attribute to invalidate the cache in dev'
,
async
()
=>
{
// The ?ts= timestamp is a workaround for a Safari preload cache bug:
// https://github.com/vercel/next.js/issues/5860
// https://bugs.webkit.org/show_bug.cgi?id=187726
// It must only appear on CSS/font resources, not on script tags, because
// the Turbopack runtime reads ASSET_SUFFIX from the executing script's
// query string and would leak it onto image URLs.
it
(
'adds a timestamp only to CSS/font link tags to invalidate the cache in dev'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/'
,
undefined
,
{
headers
:
{
'user-agent'
:
'Safari'
},
})
$
(
'link[rel=preload]'
).
each
((
index
,
element
)
=>
{
// CSS preload links must have ?ts= for Safari cache busting
$
(
'link[rel=preload][as=style]'
).
each
((
index
,
element
)
=>
{
const
href
=
$
(
element
).
attr
(
'href'
)
expect
(
href
).
toMatch
(
/^
[^
?
]
+
\?
ts=
\d
+$/
)
})
// Font preload links must have ?ts= for Safari cache busting
$
(
'link[rel=preload][as=font]'
).
each
((
index
,
element
)
=>
{
const
href
=
$
(
element
).
attr
(
'href'
)
expect
(
href
).
toMatch
(
/^
[^
?
]
+
\?
ts=
\d
+$/
)
})
// Script preload links must NOT have ?ts= (Turbopack ASSET_SUFFIX bug)
$
(
'link[rel=preload][as=script]'
).
each
((
index
,
element
)
=>
{
const
src
=
$
(
element
).
attr
(
'href'
)
expect
(
src
).
not
.
toMatch
(
/
[
?&
]
ts=/
)
})
// Script tags must NOT have ?ts= (Turbopack ASSET_SUFFIX bug)
$
(
'script[src]'
).
each
((
index
,
element
)
=>
{
const
src
=
$
(
element
).
attr
(
'src'
)
expect
(
src
).
toMatch
(
/^
[^
?
]
+
\?
ts=
\d
+$
/
)
expect
(
src
).
not
.
toMatch
(
/
[
?&
]
ts=
/
)
})
})
}
...
...
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