Unverified Commit 447cba0f authored by Shu Ding's avatar Shu Ding Committed by GitHub

Remove the timestamp query for CSS resources (#43185)

As discussed [here](https://vercel.slack.com/archives/C035J346QQL/p1668425692084449), we don't need the timestamp for CSS resources in the app dir for now as we are not preloading them. Manually tested with Safari and added corresponding e2e tests.

Closes #42862.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 21d7d224
...@@ -999,13 +999,17 @@ export async function renderToHTMLOrFlight( ...@@ -999,13 +999,17 @@ export async function renderToHTMLOrFlight(
filePath, filePath,
serverCSSForEntries serverCSSForEntries
) )
const cacheBustingUrlSuffix = dev ? `?ts=${Date.now()}` : ''
const styles = cssHrefs const styles = cssHrefs
? cssHrefs.map((href, index) => ( ? cssHrefs.map((href, index) => (
<link <link
rel="stylesheet" rel="stylesheet"
href={`${assetPrefix}/_next/${href}${cacheBustingUrlSuffix}`} // In dev, Safari will wrongly cache the resource if you preload it:
// - https://github.com/vercel/next.js/issues/5860
// - https://bugs.webkit.org/show_bug.cgi?id=187726
// We used to add a `?ts=` query for resources in `pages` to bypass it,
// but in this case it is fine as we don't need to preload the styles.
href={`${assetPrefix}/_next/${href}`}
// @ts-ignore // @ts-ignore
precedence={shouldPreload ? 'high' : undefined} precedence={shouldPreload ? 'high' : undefined}
key={index} key={index}
...@@ -1324,10 +1328,6 @@ export async function renderToHTMLOrFlight( ...@@ -1324,10 +1328,6 @@ export async function renderToHTMLOrFlight(
return { return {
Component: () => { Component: () => {
// Add extra cache busting (DEV only) for https://github.com/vercel/next.js/issues/5860
// See also https://bugs.webkit.org/show_bug.cgi?id=187726
const cacheBustingUrlSuffix = dev ? `?ts=${Date.now()}` : ''
return ( return (
<> <>
{preloadedFontFiles.map((fontFile) => { {preloadedFontFiles.map((fontFile) => {
...@@ -1347,7 +1347,12 @@ export async function renderToHTMLOrFlight( ...@@ -1347,7 +1347,12 @@ export async function renderToHTMLOrFlight(
? stylesheets.map((href, index) => ( ? stylesheets.map((href, index) => (
<link <link
rel="stylesheet" rel="stylesheet"
href={`${assetPrefix}/_next/${href}${cacheBustingUrlSuffix}`} // In dev, Safari will wrongly cache the resource if you preload it:
// - https://github.com/vercel/next.js/issues/5860
// - https://bugs.webkit.org/show_bug.cgi?id=187726
// We used to add a `?ts=` query for resources in `pages` to bypass it,
// but in this case it is fine as we don't need to preload the styles.
href={`${assetPrefix}/_next/${href}`}
// `Precedence` is an opt-in signal for React to handle // `Precedence` is an opt-in signal for React to handle
// resource loading and deduplication, etc: // resource loading and deduplication, etc:
// https://github.com/facebook/react/pull/25060 // https://github.com/facebook/react/pull/25060
......
...@@ -299,20 +299,6 @@ describe('app dir', () => { ...@@ -299,20 +299,6 @@ describe('app dir', () => {
expect(html).toContain('hello from app/partial-match-[id]. ID is: 123') expect(html).toContain('hello from app/partial-match-[id]. ID is: 123')
}) })
// 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('should use cache busting when loading css (dev only)', async () => {
const html = await renderViaHTTP(next.url, '/')
const $ = cheerio.load(html)
const links = $('link[rel=stylesheet]')
links.each((_, link) => {
const href = $(link).attr('href')
isDev
? expect(href).toMatch(/\?ts=/)
: expect(href).not.toMatch(/\?ts=/)
})
})
describe('rewrites', () => { describe('rewrites', () => {
// TODO-APP: rewrite url is broken // TODO-APP: rewrite url is broken
it('should support rewrites on initial load', async () => { it('should support rewrites on initial load', async () => {
...@@ -1514,7 +1500,7 @@ describe('app dir', () => { ...@@ -1514,7 +1500,7 @@ describe('app dir', () => {
const html = await renderViaHTTP(next.url, '/loading-bug/hi') const html = await renderViaHTTP(next.url, '/loading-bug/hi')
// The link tag should be included together with loading // The link tag should be included together with loading
expect(html).toMatch( expect(html).toMatch(
/<link rel="stylesheet" href="(.+)\.css(\?ts=\d+)?"\/><h2>Loading...<\/h2>/ /<link rel="stylesheet" href="(.+)\.css"\/><h2>Loading...<\/h2>/
) )
}) })
...@@ -1574,6 +1560,66 @@ describe('app dir', () => { ...@@ -1574,6 +1560,66 @@ describe('app dir', () => {
).toBe('50px') ).toBe('50px')
}) })
}) })
if (isDev) {
describe('HMR', () => {
it('should support HMR for CSS imports in server components', async () => {
const filePath = 'app/css/css-page/style.css'
const origContent = await next.readFile(filePath)
// h1 should be red
const browser = await webdriver(next.url, '/css/css-page')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
try {
await next.patchFile(filePath, origContent.replace('red', 'blue'))
// Wait for HMR to trigger
await new Promise((resolve) => setTimeout(resolve, 2000))
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(0, 0, 255)')
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should support HMR for CSS imports in client components', async () => {
const filePath = 'app/css/css-client/client-page.css'
const origContent = await next.readFile(filePath)
// h1 should be red
const browser = await webdriver(next.url, '/css/css-client')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(255, 0, 0)')
try {
await next.patchFile(filePath, origContent.replace('red', 'blue'))
// Wait for HMR to trigger
await new Promise((resolve) => setTimeout(resolve, 2000))
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).color`
)
).toBe('rgb(0, 0, 255)')
} finally {
await next.patchFile(filePath, origContent)
}
})
})
}
}) })
describe('searchParams prop', () => { describe('searchParams prop', () => {
......
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