Commit 9b66f6e8 authored by Luke Sandberg's avatar Luke Sandberg

safari fix

parent d0f6ad53
......@@ -384,7 +384,7 @@ export class Head extends React.Component<HeadProps> {
getCssLinks(files: DocumentFiles): JSX.Element[] | null {
const {
assetPrefix,
assetQueryString,
cssAssetQueryString,
dynamicImports,
dynamicCssManifest,
crossOrigin,
......@@ -422,7 +422,7 @@ export class Head extends React.Component<HeadProps> {
rel="preload"
href={`${assetPrefix}/_next/${encodeURIPath(
file
)}${assetQueryString}`}
)}${cssAssetQueryString}`}
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
)}${assetQueryString}`}
)}${cssAssetQueryString}`}
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,
assetQueryString,
cssAssetQueryString,
} = this.context
const disableRuntimeJS = unstable_runtimeJS === false
......@@ -659,7 +659,7 @@ export class Head extends React.Component<HeadProps> {
nextFontManifest,
dangerousAsPath,
assetPrefix,
assetQueryString
cssAssetQueryString
)
const tracingMetadata = getTracedMetadata(
......
......@@ -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,
......
......@@ -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[]
......
......@@ -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=/)
})
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment