@@ -73,9 +73,18 @@ interface Component {
73
73
preload : ( data : any ) => any | Promise < any >
74
74
}
75
75
76
+ const IGNORE = '__SAPPER__IGNORE__' ;
77
+ function toIgnore ( uri : string , val : any ) {
78
+ if ( Array . isArray ( val ) ) return val . some ( x => toIgnore ( uri , x ) ) ;
79
+ if ( val instanceof RegExp ) return val . test ( uri ) ;
80
+ if ( typeof val === 'function' ) return val ( uri ) ;
81
+ return uri . startsWith ( val . charCodeAt ( 0 ) === 47 ? val : `/${ val } ` ) ;
82
+ }
83
+
76
84
export default function middleware ( opts : {
77
85
manifest : Manifest ,
78
86
store : ( req : Req ) => Store ,
87
+ ignore ?: any ,
79
88
routes ?: any // legacy
80
89
} ) {
81
90
if ( opts . routes ) {
@@ -84,12 +93,19 @@ export default function middleware(opts: {
84
93
85
94
const output = locations . dest ( ) ;
86
95
87
- const { manifest, store } = opts ;
96
+ const { manifest, store, ignore } = opts ;
88
97
89
98
let emitted_basepath = false ;
90
99
91
100
const middleware = compose_handlers ( [
101
+ ignore && ( ( req : Req , res : ServerResponse , next : ( ) => void ) => {
102
+ req [ IGNORE ] = toIgnore ( req . path , ignore ) ;
103
+ next ( ) ;
104
+ } ) ,
105
+
92
106
( req : Req , res : ServerResponse , next : ( ) => void ) => {
107
+ if ( req [ IGNORE ] ) return next ( ) ;
108
+
93
109
if ( req . baseUrl === undefined ) {
94
110
let { originalUrl } = req ;
95
111
if ( req . url === '/' && originalUrl [ originalUrl . length - 1 ] !== '/' ) {
@@ -163,6 +179,8 @@ function serve({ prefix, pathname, cache_control }: {
163
179
: ( file : string ) => ( cache . has ( file ) ? cache : cache . set ( file , fs . readFileSync ( path . resolve ( output , file ) ) ) ) . get ( file )
164
180
165
181
return ( req : Req , res : ServerResponse , next : ( ) => void ) => {
182
+ if ( req [ IGNORE ] ) return next ( ) ;
183
+
166
184
if ( filter ( req ) ) {
167
185
const type = lookup ( req . path ) ;
168
186
@@ -245,6 +263,8 @@ function get_server_route_handler(routes: ServerRoute[]) {
245
263
}
246
264
247
265
return function find_route ( req : Req , res : ServerResponse , next : ( ) => void ) {
266
+ if ( req [ IGNORE ] ) return next ( ) ;
267
+
248
268
for ( const route of routes ) {
249
269
if ( route . pattern . test ( req . path ) ) {
250
270
handle_route ( route , req , res , next ) ;
@@ -494,7 +514,9 @@ function get_page_handler(manifest: Manifest, store_getter: (req: Req) => Store)
494
514
} ) ;
495
515
}
496
516
497
- return function find_route ( req : Req , res : ServerResponse ) {
517
+ return function find_route ( req : Req , res : ServerResponse , next : ( ) => void ) {
518
+ if ( req [ IGNORE ] ) return next ( ) ;
519
+
498
520
if ( ! server_routes . some ( route => route . pattern . test ( req . path ) ) ) {
499
521
for ( const page of pages ) {
500
522
if ( page . pattern . test ( req . path ) ) {
0 commit comments