File size: 5,791 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import glob from 'glob'
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import {
  killApp,
  findPort,
  renderViaHTTP,
  initNextServerScript,
  fetchViaHTTP,
} from 'next-test-utils'

describe('minimal-mode-response-cache', () => {
  let next: NextInstance
  let server
  let port
  let appPort
  let output = ''

  beforeAll(async () => {
    // test build against environment with next support
    process.env.NOW_BUILDER = '1'
    process.env.NEXT_PRIVATE_TEST_HEADERS = '1'

    next = await createNext({
      files: new FileRef(join(__dirname, 'app')),
    })
    await next.stop()

    await fs.move(
      join(next.testDir, '.next/standalone'),
      join(next.testDir, 'standalone')
    )
    for (const file of await fs.readdir(next.testDir)) {
      if (file !== 'standalone') {
        await fs.remove(join(next.testDir, file))
        console.log('removed', file)
      }
    }
    const files = glob.sync('**/*', {
      cwd: join(next.testDir, 'standalone/.next/server'),
      nodir: true,
      dot: true,
    })

    for (const file of files) {
      if (file.match(/(pages|app)[/\\]/) && !file.endsWith('.js')) {
        await fs.remove(join(next.testDir, 'standalone/.next/server', file))
        console.log(
          'removing',
          join(next.testDir, 'standalone/.next/server', file)
        )
      }
    }

    const testServer = join(next.testDir, 'standalone/server.js')
    await fs.writeFile(
      testServer,
      (await fs.readFile(testServer, 'utf8'))
        .replace('console.error(err)', `console.error('top-level', err)`)
        .replace('port:', 'minimalMode: true,port:')
    )
    port = await findPort()
    server = await initNextServerScript(
      testServer,
      /- Local:/,
      {
        ...process.env,
        HOSTNAME: '',
        PORT: port.toString(),
      },
      undefined,
      {
        cwd: next.testDir,
        onStdout(msg) {
          output += msg
        },
        onStderr(msg) {
          output += msg
        },
      }
    )
    appPort = `http://127.0.0.1:${port}`
  })
  afterAll(async () => {
    delete process.env.NEXT_PRIVATE_TEST_HEADERS
    await next.destroy()
    if (server) await killApp(server)
  })

  it('app router revalidate should work with previous response cache dynamic', async () => {
    const headers = {
      vary: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch',
      'x-now-route-matches': '1=compare&rsc=1',
      'x-matched-path': '/app-blog/compare.rsc',
      'x-vercel-id': '1',
      rsc: '1',
    }
    const res1 = await fetchViaHTTP(
      appPort,
      '/app-blog/compare.rsc',
      undefined,
      {
        headers,
      }
    )
    const content1 = await res1.text()
    expect(content1).not.toContain('<html')
    expect(content1).toContain('app-blog')
    expect(res1.headers.get('content-type')).toContain('text/x-component')

    const res2 = await fetchViaHTTP(appPort, '/app-blog/compare', undefined, {
      headers,
    })
    const content2 = await res2.text()
    expect(content2).toContain('<html')
    expect(content2).toContain('app-blog')
    expect(res2.headers.get('content-type')).toContain('text/html')
  })

  it('app router revalidate should work with previous response cache', async () => {
    const headers = {
      vary: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch',
      'x-now-route-matches': '1=app-another&rsc=1',
      'x-matched-path': '/app-another.rsc',
      'x-vercel-id': '1',
      rsc: '1',
    }
    const res1 = await fetchViaHTTP(appPort, '/app-another.rsc', undefined, {
      headers,
    })
    const content1 = await res1.text()
    expect(res1.headers.get('content-type')).toContain('text/x-component')
    expect(content1).not.toContain('<html')
    expect(content1).toContain('app-another')

    const res2 = await fetchViaHTTP(appPort, '/app-another', undefined, {
      headers,
    })
    const content2 = await res2.text()
    expect(res2.headers.get('content-type')).toContain('text/html')
    expect(content2).toContain('<html')
    expect(content2).toContain('app-another')
  })

  it('should have correct "Started server on" log', async () => {
    expect(output).toContain(`- Local:`)
    expect(output).toContain(`http://localhost:${port}`)
  })

  it('should have correct responses', async () => {
    const html = await renderViaHTTP(appPort, '/')
    expect(html.length).toBeTruthy()

    for (const { path, matchedPath, query, asPath, pathname } of [
      { path: '/', asPath: '/' },
      { path: '/', matchedPath: '/index', asPath: '/' },
      { path: '/', matchedPath: '/index/', asPath: '/' },
      { path: '/', matchedPath: '/', asPath: '/' },
      {
        path: '/news/',
        matchedPath: '/news/',
        asPath: '/news/',
        pathname: '/news',
      },
      {
        path: '/news/',
        matchedPath: '/news',
        asPath: '/news/',
        pathname: '/news',
      },
      {
        path: '/blog/first/',
        matchedPath: '/blog/first/',
        pathname: '/blog/[slug]',
        asPath: '/blog/first/',
        query: { slug: 'first' },
      },
      {
        path: '/blog/second/',
        matchedPath: '/blog/[slug]/',
        pathname: '/blog/[slug]',
        asPath: '/blog/second/',
        query: { slug: 'second' },
      },
    ]) {
      const html = await renderViaHTTP(appPort, path, undefined, {
        headers: {
          'x-matched-path': matchedPath || path,
        },
      })
      const $ = cheerio.load(html)
      expect($('#asPath').text()).toBe(asPath)
      expect($('#pathname').text()).toBe(pathname || path)
      expect(JSON.parse($('#query').text())).toEqual(query || {})
    }
  })
})