Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 47a6d6f

Browse files
authored
Merge pull request #326 from lukeed/feat/ignore
Add `ignore` option
2 parents fc855f3 + 4b2b644 commit 47a6d6f

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

src/middleware.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,18 @@ interface Component {
7373
preload: (data: any) => any | Promise<any>
7474
}
7575

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+
7684
export default function middleware(opts: {
7785
manifest: Manifest,
7886
store: (req: Req) => Store,
87+
ignore?: any,
7988
routes?: any // legacy
8089
}) {
8190
if (opts.routes) {
@@ -84,12 +93,19 @@ export default function middleware(opts: {
8493

8594
const output = locations.dest();
8695

87-
const { manifest, store } = opts;
96+
const { manifest, store, ignore } = opts;
8897

8998
let emitted_basepath = false;
9099

91100
const middleware = compose_handlers([
101+
ignore && ((req: Req, res: ServerResponse, next: () => void) => {
102+
req[IGNORE] = toIgnore(req.path, ignore);
103+
next();
104+
}),
105+
92106
(req: Req, res: ServerResponse, next: () => void) => {
107+
if (req[IGNORE]) return next();
108+
93109
if (req.baseUrl === undefined) {
94110
let { originalUrl } = req;
95111
if (req.url === '/' && originalUrl[originalUrl.length - 1] !== '/') {
@@ -163,6 +179,8 @@ function serve({ prefix, pathname, cache_control }: {
163179
: (file: string) => (cache.has(file) ? cache : cache.set(file, fs.readFileSync(path.resolve(output, file)))).get(file)
164180

165181
return (req: Req, res: ServerResponse, next: () => void) => {
182+
if (req[IGNORE]) return next();
183+
166184
if (filter(req)) {
167185
const type = lookup(req.path);
168186

@@ -245,6 +263,8 @@ function get_server_route_handler(routes: ServerRoute[]) {
245263
}
246264

247265
return function find_route(req: Req, res: ServerResponse, next: () => void) {
266+
if (req[IGNORE]) return next();
267+
248268
for (const route of routes) {
249269
if (route.pattern.test(req.path)) {
250270
handle_route(route, req, res, next);
@@ -494,7 +514,9 @@ function get_page_handler(manifest: Manifest, store_getter: (req: Req) => Store)
494514
});
495515
}
496516

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+
498520
if (!server_routes.some(route => route.pattern.test(req.path))) {
499521
for (const page of pages) {
500522
if (page.pattern.test(req.path)) {

test/app/app/server.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,14 @@ const middlewares = [
9191
return new Store({
9292
title: 'Stored title'
9393
});
94-
}
95-
})
94+
},
95+
ignore: [
96+
/foobar/i,
97+
'/buzz',
98+
'fizz',
99+
x => x === '/hello'
100+
]
101+
}),
96102
];
97103

98104
if (BASEPATH) {
@@ -101,4 +107,8 @@ if (BASEPATH) {
101107
app.use(...middlewares);
102108
}
103109

104-
app.listen(PORT);
110+
['foobar', 'buzz', 'fizzer', 'hello'].forEach(uri => {
111+
app.get('/'+uri, (req, res) => res.end(uri));
112+
});
113+
114+
app.listen(PORT);

test/common/test.js

+36
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,42 @@ function run({ mode, basepath = '' }) {
477477
});
478478
});
479479

480+
// Ignores are meant for top-level escape.
481+
// ~> Sapper **should** own the entire {basepath} when designated.
482+
if (!basepath) {
483+
it('respects `options.ignore` values (RegExp)', () => {
484+
return nightmare.goto(`${base}/foobar`)
485+
.evaluate(() => document.documentElement.textContent)
486+
.then(text => {
487+
assert.equal(text, 'foobar');
488+
});
489+
});
490+
491+
it('respects `options.ignore` values (String #1)', () => {
492+
return nightmare.goto(`${base}/buzz`)
493+
.evaluate(() => document.documentElement.textContent)
494+
.then(text => {
495+
assert.equal(text, 'buzz');
496+
});
497+
});
498+
499+
it('respects `options.ignore` values (String #2)', () => {
500+
return nightmare.goto(`${base}/fizzer`)
501+
.evaluate(() => document.documentElement.textContent)
502+
.then(text => {
503+
assert.equal(text, 'fizzer');
504+
});
505+
});
506+
507+
it('respects `options.ignore` values (Function)', () => {
508+
return nightmare.goto(`${base}/hello`)
509+
.evaluate(() => document.documentElement.textContent)
510+
.then(text => {
511+
assert.equal(text, 'hello');
512+
});
513+
});
514+
}
515+
480516
it('does not attempt client-side navigation to server routes', () => {
481517
return nightmare.goto(`${base}/blog/how-is-sapper-different-from-next`)
482518
.init()

0 commit comments

Comments
 (0)