File tree Expand file tree Collapse file tree 3 files changed +140
-1
lines changed
packages/gatsby/src/bootstrap Expand file tree Collapse file tree 3 files changed +140
-1
lines changed Original file line number Diff line number Diff line change 1
1
// Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
3
+ exports [` requires-writer matchPath have static pages first and prefer more specific matchPaths 1` ] = `
4
+ Array [
5
+ Object {
6
+ " matchPath" : " /mp1/mp2/hello" ,
7
+ " path" : " /mp1/mp2/hello" ,
8
+ } ,
9
+ Object {
10
+ " matchPath" : " /mp1/mp2" ,
11
+ " path" : " /mp1/mp2" ,
12
+ } ,
13
+ Object {
14
+ " matchPath" : " /some-page" ,
15
+ " path" : " /some-page" ,
16
+ } ,
17
+ Object {
18
+ " matchPath" : " /" ,
19
+ " path" : " /" ,
20
+ } ,
21
+ Object {
22
+ " matchPath" : " /mp1/mp2/mp3/mp4/*" ,
23
+ " path" : " /mp4" ,
24
+ } ,
25
+ Object {
26
+ " matchPath" : " /mp1/mp2/mp3/*" ,
27
+ " path" : " /mp3" ,
28
+ } ,
29
+ Object {
30
+ " matchPath" : " /mp1/mp2/*" ,
31
+ " path" : " /mp2" ,
32
+ } ,
33
+ Object {
34
+ " matchPath" : " /mp1/*" ,
35
+ " path" : " /mp1" ,
36
+ } ,
37
+ Object {
38
+ " matchPath" : " /*" ,
39
+ " path" : " /custom-404" ,
40
+ } ,
41
+ ]
42
+ ` ;
43
+
3
44
exports [` requires-writer matchPath should be sorted by specificity 1` ] = `
4
45
Array [
5
46
Object {
@@ -21,6 +62,19 @@ Array [
21
62
]
22
63
` ;
23
64
65
+ exports [` requires-writer matchPath should have index pages with higher priority than matchPaths 1` ] = `
66
+ Array [
67
+ Object {
68
+ " matchPath" : " /" ,
69
+ " path" : " /" ,
70
+ } ,
71
+ Object {
72
+ " matchPath" : " /*" ,
73
+ " path" : " /custom-404" ,
74
+ } ,
75
+ ]
76
+ ` ;
77
+
24
78
exports [` requires-writer matchPath should have static pages that live inside a matchPath 1` ] = `
25
79
Array [
26
80
Object {
Original file line number Diff line number Diff line change @@ -145,5 +145,80 @@ describe(`requires-writer`, () => {
145
145
expect ( matchPaths [ 0 ] . path ) . toBe ( pages . get ( `/app/clients/static` ) . path )
146
146
expect ( matchPaths ) . toMatchSnapshot ( )
147
147
} )
148
+
149
+ it ( `should have index pages with higher priority than matchPaths` , async ( ) => {
150
+ const pages = generatePagesState ( [
151
+ {
152
+ path : `/` ,
153
+ } ,
154
+ {
155
+ path : `/custom-404` ,
156
+ matchPath : `/*` ,
157
+ } ,
158
+ ] )
159
+
160
+ await requiresWriter . writeAll ( {
161
+ pages,
162
+ program,
163
+ } )
164
+
165
+ expect ( matchPaths [ 0 ] . path ) . toBe ( pages . get ( `/` ) . path )
166
+ expect ( matchPaths ) . toMatchSnapshot ( )
167
+ } )
168
+
169
+ it ( `have static pages first and prefer more specific matchPaths` , async ( ) => {
170
+ const pages = generatePagesState ( [
171
+ {
172
+ path : `/` ,
173
+ } ,
174
+ {
175
+ path : `/custom-404` ,
176
+ matchPath : `/*` ,
177
+ } ,
178
+ {
179
+ path : `/mp4` ,
180
+ matchPath : `/mp1/mp2/mp3/mp4/*` ,
181
+ } ,
182
+ {
183
+ path : `/some-page` ,
184
+ } ,
185
+ {
186
+ path : `/mp1/mp2` ,
187
+ } ,
188
+ {
189
+ path : `/mp1/mp2/hello` ,
190
+ } ,
191
+ {
192
+ path : `/mp1` ,
193
+ matchPath : `/mp1/*` ,
194
+ } ,
195
+ {
196
+ path : `/mp2` ,
197
+ matchPath : `/mp1/mp2/*` ,
198
+ } ,
199
+ {
200
+ path : `/mp3` ,
201
+ matchPath : `/mp1/mp2/mp3/*` ,
202
+ } ,
203
+ ] )
204
+
205
+ await requiresWriter . writeAll ( {
206
+ pages,
207
+ program,
208
+ } )
209
+
210
+ expect ( matchPaths . map ( p => p . path ) ) . toEqual ( [
211
+ `/mp1/mp2/hello` ,
212
+ `/mp1/mp2` ,
213
+ `/some-page` ,
214
+ `/` ,
215
+ `/mp4` ,
216
+ `/mp3` ,
217
+ `/mp2` ,
218
+ `/mp1` ,
219
+ `/custom-404` ,
220
+ ] )
221
+ expect ( matchPaths ) . toMatchSnapshot ( )
222
+ } )
148
223
} )
149
224
} )
Original file line number Diff line number Diff line change @@ -27,15 +27,19 @@ const getComponents = pages =>
27
27
*/
28
28
const getMatchPaths = pages => {
29
29
const createMatchPathEntry = ( page , index ) => {
30
- let score = page . matchPath . replace ( / \/ $ / , `` ) . split ( `/` ) . length
30
+ let score = page . matchPath . replace ( / [ / ] [ * ] ? $ / , `` ) . split ( `/` ) . length
31
+ let wildcard = 0
32
+
31
33
if ( ! page . matchPath . includes ( `*` ) ) {
34
+ wildcard = 1
32
35
score += 1
33
36
}
34
37
35
38
return {
36
39
...page ,
37
40
index,
38
41
score,
42
+ wildcard,
39
43
}
40
44
}
41
45
@@ -74,6 +78,12 @@ const getMatchPaths = pages => {
74
78
75
79
return matchPathPages
76
80
. sort ( ( a , b ) => {
81
+ // Paths with wildcards should appear after those without.
82
+ const wildcardOrder = b . wildcard - a . wildcard
83
+ if ( wildcardOrder !== 0 ) {
84
+ return wildcardOrder
85
+ }
86
+
77
87
// The higher the score, the higher the specificity of our matchPath
78
88
const order = b . score - a . score
79
89
if ( order !== 0 ) {
You can’t perform that action at this time.
0 commit comments