File size: 6,774 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
202
203
204
205
import { nextTestSetup } from 'e2e-utils'
import { check, retry } from 'next-test-utils'
import type { Request, Response } from 'playwright'

describe('app dir - basepath', () => {
  const { next, isNextDev } = nextTestSetup({
    files: __dirname,
    dependencies: {
      sass: 'latest',
    },
  })

  it('should successfully hard navigate from pages -> app', async () => {
    const browser = await next.browser('/base/pages-path')
    await browser.elementByCss('#to-another').click()
    await browser.waitForElementByCss('#page-2')
  })

  it('should support `basePath`', async () => {
    const html = await next.render('/base')
    expect(html).toContain('<h1>Test Page</h1>')
  })

  it('should support Link with basePath prefixed', async () => {
    const browser = await next.browser('/base')
    expect(
      await browser
        .elementByCss('a[href="/base/another"]')
        .click()
        .waitForElementByCss('#page-2')
        .text()
    ).toBe(`Page 2`)
  })

  it('should prefix segment metadata og image with basePath and pathname', async () => {
    const $ = await next.render$('/base/metadata')
    const ogImageHref = $('meta[property="og:image"]').attr('content')
    expect(ogImageHref).toContain('/base/metadata/opengraph-image.png')
  })

  it('should prefix manifest with basePath', async () => {
    const $ = await next.render$('/base/metadata')
    const manifestHref = $('link[rel="manifest"]').attr('href')
    expect(manifestHref).toContain('/base/manifest.webmanifest')
  })

  it('should prefix redirect() with basePath', async () => {
    const browser = await next.browser('/base/redirect')
    await retry(async () => {
      expect(await browser.url()).toBe(`${next.url}/base/another`)
    })
  })

  it('should render usePathname without the basePath', async () => {
    const pathnames = ['/use-pathname', '/use-pathname-another']
    const validatorPromises = pathnames.map(async (pathname) => {
      const $ = await next.render$('/base' + pathname)
      expect($('#pathname').data('pathname')).toBe(pathname)
    })
    await Promise.all(validatorPromises)
  })

  it('should handle redirect in dynamic in suspense boundary routes with basePath', async () => {
    const browser = await next.browser('/base/dynamic/source')
    await retry(async () => {
      // Check content is loaded first to avoid flakiness
      expect(await browser.elementByCss('p').text()).toBe(`id:dest`)
      expect(await browser.url()).toBe(`${next.url}/base/dynamic/dest`)
    })
  })

  it.each(['/base/refresh', '/base/refresh?foo=bar'])(
    `should only make a single RSC call to the current page (%s)`,
    async (path) => {
      let rscRequests = []
      const browser = await next.browser(path, {
        beforePageLoad(page) {
          page.on('request', (request) => {
            return request.allHeaders().then((headers) => {
              if (
                headers['RSC'.toLowerCase()] === '1' &&
                // Prefetches also include `RSC`
                headers['Next-Router-Prefetch'.toLowerCase()] !== '1'
              ) {
                rscRequests.push(request.url())
              }
            })
          })
        },
      })
      await browser.elementByCss('button').click()
      await retry(async () => {
        expect(rscRequests.length).toBe(1)
        expect(rscRequests[0]).toContain(`${next.url}${path}`)
      })
    }
  )

  it.each([
    { redirectType: 'relative', buttonId: 'redirect-relative' },
    { redirectType: 'absolute', buttonId: 'redirect-absolute-internal' },
  ])(
    `should properly stream an internal server action redirect() with a $redirectType URL`,
    async ({ buttonId }) => {
      const initialPagePath = '/base/client'
      const destinationPagePath = '/base/another'

      const requests: Request[] = []
      const responses: Response[] = []

      const browser = await next.browser(initialPagePath)

      browser.on('request', (req) => {
        const url = req.url()

        if (
          url.includes(initialPagePath) ||
          url.includes(destinationPagePath)
        ) {
          requests.push(req)
        }
      })

      browser.on('response', (res) => {
        const url = res.url()

        if (
          url.includes(initialPagePath) ||
          url.includes(destinationPagePath)
        ) {
          responses.push(res)
        }
      })

      await browser.elementById(buttonId).click()
      await check(() => browser.url(), /\/base\/another/)

      expect(await browser.waitForElementByCss('#page-2').text()).toBe(`Page 2`)

      // This verifies the redirect & server response happens in a single roundtrip,
      // if the redirect resource was static. In development, these responses are always
      // dynamically generated, so we only expect a single request for build/deploy.
      if (!isNextDev) {
        expect(requests).toHaveLength(1)
        expect(responses).toHaveLength(1)
      }

      const request = requests[0]
      const response = responses[0]

      expect(request.method()).toEqual('POST')
      expect(request.url()).toEqual(`${next.url}${initialPagePath}`)
      expect(response.status()).toEqual(303)
    }
  )

  it('should redirect externally when encountering absolute URLs on the same host outside the basePath', async () => {
    const initialPagePath = '/base/client'
    const destinationPagePath = '/outsideBasePath'

    const requests: Request[] = []
    const responses: Response[] = []

    const browser = await next.browser(initialPagePath)

    browser.on('request', (req) => {
      const url = req.url()

      if (!url.includes('_next')) {
        requests.push(req)
      }
    })

    browser.on('response', (res) => {
      const url = res.url()

      if (!url.includes('_next')) {
        responses.push(res)
      }
    })

    await browser.elementById('redirect-absolute-external').click()
    await check(() => browser.url(), /\/outsideBasePath/)

    // We expect to see two requests, first a POST invoking the server
    // action. And second a GET request resolving the redirect.
    expect(requests).toHaveLength(2)
    expect(responses).toHaveLength(2)

    const [firstRequest, secondRequest] = requests
    const [firstResponse, secondResponse] = responses

    expect(firstRequest.method()).toEqual('POST')
    expect(firstRequest.url()).toEqual(`${next.url}${initialPagePath}`)

    expect(secondRequest.method()).toEqual('GET')
    expect(secondRequest.url()).toEqual(`${next.url}${destinationPagePath}`)

    expect(firstResponse.status()).toEqual(303)
    // Since this is an external request to a resource outside of NextJS
    // we expect to see a separate request resolving the external URL.
    expect(secondResponse.status()).toEqual(200)
  })
})