File size: 2,156 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
import { nextTestSetup } from 'e2e-utils'

describe('app-dir assetPrefix handling', () => {
  const { next } = nextTestSetup({
    files: __dirname,
  })

  it('should redirect route when requesting it directly', async () => {
    const res = await next.fetch('/a/', {
      redirect: 'manual',
    })
    expect(res.status).toBe(308)
    expect(new URL(res.headers.get('location'), next.url).pathname).toBe('/a')
  })

  it('should render link', async () => {
    const $ = await next.render$('/')
    expect($('#to-a-trailing-slash').attr('href')).toBe('/a')
  })

  it('should redirect route when requesting it directly by browser', async () => {
    const browser = await next.browser('/a')
    expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
  })

  it('should redirect route when clicking link', async () => {
    const browser = await next.browser('/')
    await browser
      .elementByCss('#to-a-trailing-slash')
      .click()
      .waitForElementByCss('#a-page')
    expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
  })

  it('bundles should return 200 on served assetPrefix', async () => {
    const $ = await next.render$('/')

    let bundles = []
    for (const script of $('script').toArray()) {
      const { src } = script.attribs
      if (src?.includes('/custom-asset-prefix/_next/static')) {
        bundles.push(src)
      }
    }

    expect(bundles.length).toBeGreaterThan(0)

    bundles.forEach(async (src) => {
      const { status } = await next.fetch(decodeURI(src))

      expect(status).toBe(200)
    })
  })

  describe('rewrites', () => {
    it('rewrites that do not start with assetPrefix should still work', async () => {
      const res = await next.fetch('/not-custom-asset-prefix/api/test-json', {})
      expect(res.status).toBe(200)
      expect(await res.text()).toBe('{"message":"test"}')
    })

    it('should respect rewrites that start with assetPrefix', async () => {
      const res = await next.fetch('/custom-asset-prefix/api/test-json', {})
      expect(res.status).toBe(200)
      expect(await res.text()).toBe('{"message":"test"}')
    })
  })
})