File size: 1,479 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import { join } from 'path'
import { fetchViaHTTP, waitFor } from 'next-test-utils'

describe('app-dir-prevent-304-caching', () => {
  let next: NextInstance

  beforeAll(async () => {
    next = await createNext({
      files: {
        'next.config.js': new FileRef(join(__dirname, 'next.config.js')),
        app: new FileRef(join(__dirname, 'app')),
      },
    })
  })
  afterAll(() => next.destroy())

  // https://github.com/vercel/next.js/issues/56580
  it('should not cache 304 status', async () => {
    // Fresh call
    const rFresh = await fetchViaHTTP(next.url, '/')
    expect(rFresh.status).toBe(200)

    await waitFor(500)

    // Cache HIT but still 200
    const rHit = await fetchViaHTTP(
      next.url,
      '/',
      {},
      {
        headers: {
          'If-None-Match': rFresh.headers.get('etag'),
        },
      }
    )
    expect(rHit.status).toBe(200)
    await waitFor(500)

    // Here happens the race condition
    const r304 = await fetchViaHTTP(
      next.url,
      '/',
      {},
      {
        headers: {
          'If-None-Match': rHit.headers.get('etag'),
        },
      }
    )
    expect(r304.status).toBe(304)
    // ... Postponed but should not save 304 ...
    await waitFor(1000)

    // Now without cache headers should 200
    const rStillFresh = await fetchViaHTTP(next.url, '/')
    expect(rStillFresh.status).toBe(200)
  })
})