File size: 5,465 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
import loadCustomRoutes from './load-custom-routes'

describe('loadCustomRoutes', () => {
  describe('rewrites', () => {
    it('missing rewrites should not throw', async () => {
      const customRoutes = await loadCustomRoutes({})
      expect(customRoutes.rewrites.beforeFiles).toEqual([])
      expect(customRoutes.rewrites.afterFiles).toEqual([])
      expect(customRoutes.rewrites.fallback).toEqual([])
    })

    it('array rewrites should be added to afterFiles', async () => {
      const customRoutes = await loadCustomRoutes({
        async rewrites() {
          return [
            {
              source: '/a',
              destination: '/b',
            },
          ]
        },
      })
      expect(customRoutes.rewrites.beforeFiles).toEqual([])
      expect(customRoutes.rewrites.afterFiles).toEqual([
        {
          destination: '/b',
          source: '/a',
        },
      ])
      expect(customRoutes.rewrites.fallback).toEqual([])
    })

    it('rewrites should be preserved correctly', async () => {
      const customRoutes = await loadCustomRoutes({
        async rewrites() {
          return {
            beforeFiles: [
              {
                source: '/beforeFiles/a',
                destination: '/beforeFiles/b',
              },
            ],
            afterFiles: [
              {
                source: '/afterFiles/a',
                destination: '/afterFiles/b',
              },
            ],
            fallback: [
              {
                source: '/fallback/a',
                destination: '/fallback/b',
              },
            ],
          }
        },
      })
      expect(customRoutes.rewrites.beforeFiles).toEqual([
        {
          destination: '/beforeFiles/b',
          source: '/beforeFiles/a',
        },
      ])
      expect(customRoutes.rewrites.afterFiles).toEqual([
        {
          destination: '/afterFiles/b',
          source: '/afterFiles/a',
        },
      ])
      expect(customRoutes.rewrites.fallback).toEqual([
        {
          destination: '/fallback/b',
          source: '/fallback/a',
        },
      ])
    })

    describe('assetPrefix', () => {
      it('automatically inserts assetPrefix rewrite for /_next/ paths', async () => {
        const customRoutes = await loadCustomRoutes({
          assetPrefix: '/custom-asset-prefix',
          async rewrites() {
            return [
              {
                source: '/a',
                destination: '/b',
              },
            ]
          },
        })
        expect(customRoutes.rewrites.beforeFiles).toEqual([
          {
            destination: '/_next/:path+',
            source: '/custom-asset-prefix/_next/:path+',
          },
        ])
        expect(customRoutes.rewrites.afterFiles).toEqual([
          {
            destination: '/b',
            source: '/a',
          },
        ])
      })

      it('automatically inserts assetPrefix rewrite for /_next/ paths when rewrites() is not present', async () => {
        const customRoutes = await loadCustomRoutes({
          assetPrefix: '/custom-asset-prefix',
        })
        expect(customRoutes.rewrites.beforeFiles).toEqual([
          {
            destination: '/_next/:path+',
            source: '/custom-asset-prefix/_next/:path+',
          },
        ])
        expect(customRoutes.rewrites.afterFiles).toEqual([])
      })

      it('automatically inserts assetPrefix rewrite for /_next/ paths for basePath', async () => {
        const customRoutes = await loadCustomRoutes({
          assetPrefix: '/custom-asset-prefix',
          basePath: '/custom-base-path',
          async rewrites() {
            return [
              {
                source: '/a',
                destination: '/b',
              },
            ]
          },
        })
        expect(customRoutes.rewrites.beforeFiles).toEqual([
          {
            destination: '/custom-base-path/_next/:path+',
            source: '/custom-asset-prefix/_next/:path+',
          },
        ])
        expect(customRoutes.rewrites.afterFiles).toEqual([
          {
            destination: '/custom-base-path/b',
            source: '/custom-base-path/a',
          },
        ])
      })

      it('does not insert assetPrefix rewrite for /_next/ paths when assetPrefix is absolute URL', async () => {
        const customRoutes = await loadCustomRoutes({
          assetPrefix: 'https://example.com',
        })
        expect(customRoutes.rewrites.beforeFiles).toEqual([])
        expect(customRoutes.rewrites.afterFiles).toEqual([])
      })

      it('automatically insert assetPrefix rewrite for /_next/ paths when assetPrefix is absolute URL with a path', async () => {
        const customRoutes = await loadCustomRoutes({
          assetPrefix: 'https://example.com/custom-asset-prefix',
        })
        expect(customRoutes.rewrites.beforeFiles).toEqual([
          {
            destination: '/_next/:path+',
            source: '/custom-asset-prefix/_next/:path+',
          },
        ])
        expect(customRoutes.rewrites.afterFiles).toEqual([])
      })

      it('does not add rewrite when assetPrefix === basePath', async () => {
        const customRoutes = await loadCustomRoutes({
          assetPrefix: '/base',
          basePath: '/base',
        })
        expect(customRoutes.rewrites.beforeFiles).toEqual([])
        expect(customRoutes.rewrites.afterFiles).toEqual([])
      })
    })
  })
})