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

Add ignore option #326

Merged
merged 4 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,18 @@ interface Component {
preload: (data: any) => any | Promise<any>
}

const IGNORE = '__SAPPER__IGNORE__';
function toIgnore(uri: string, val: any) {
if (Array.isArray(val)) return val.some(x => toIgnore(uri, x));
if (val instanceof RegExp) return val.test(uri);
if (typeof val === 'function') return val(uri);
return uri.startsWith(val.charCodeAt(0) === 47 ? val : `/${val}`);
}

export default function middleware(opts: {
manifest: Manifest,
store: (req: Req) => Store,
ignore?: any,
routes?: any // legacy
}) {
if (opts.routes) {
Expand All @@ -84,12 +93,19 @@ export default function middleware(opts: {

const output = locations.dest();

const { manifest, store } = opts;
const { manifest, store, ignore } = opts;

let emitted_basepath = false;

const middleware = compose_handlers([
ignore && ((req: Req, res: ServerResponse, next: () => void) => {
req[IGNORE] = toIgnore(req.path, ignore);
next();
}),

(req: Req, res: ServerResponse, next: () => void) => {
if (req[IGNORE]) return next();

if (req.baseUrl === undefined) {
let { originalUrl } = req;
if (req.url === '/' && originalUrl[originalUrl.length - 1] !== '/') {
Expand Down Expand Up @@ -163,6 +179,8 @@ function serve({ prefix, pathname, cache_control }: {
: (file: string) => (cache.has(file) ? cache : cache.set(file, fs.readFileSync(path.resolve(output, file)))).get(file)

return (req: Req, res: ServerResponse, next: () => void) => {
if (req[IGNORE]) return next();

if (filter(req)) {
const type = lookup(req.path);

Expand Down Expand Up @@ -245,6 +263,8 @@ function get_server_route_handler(routes: ServerRoute[]) {
}

return function find_route(req: Req, res: ServerResponse, next: () => void) {
if (req[IGNORE]) return next();

for (const route of routes) {
if (route.pattern.test(req.path)) {
handle_route(route, req, res, next);
Expand Down Expand Up @@ -494,7 +514,9 @@ function get_page_handler(manifest: Manifest, store_getter: (req: Req) => Store)
});
}

return function find_route(req: Req, res: ServerResponse) {
return function find_route(req: Req, res: ServerResponse, next: () => void) {
if (req[IGNORE]) return next();

if (!server_routes.some(route => route.pattern.test(req.path))) {
for (const page of pages) {
if (page.pattern.test(req.path)) {
Expand Down
16 changes: 13 additions & 3 deletions test/app/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ const middlewares = [
return new Store({
title: 'Stored title'
});
}
})
},
ignore: [
/foobar/i,
'/buzz',
'fizz',
x => x === '/hello'
]
}),
];

if (BASEPATH) {
Expand All @@ -101,4 +107,8 @@ if (BASEPATH) {
app.use(...middlewares);
}

app.listen(PORT);
['foobar', 'buzz', 'fizzer', 'hello'].forEach(uri => {
app.get('/'+uri, (req, res) => res.end(uri));
});

app.listen(PORT);
36 changes: 36 additions & 0 deletions test/common/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,42 @@ function run({ mode, basepath = '' }) {
});
});

// Ignores are meant for top-level escape.
// ~> Sapper **should** own the entire {basepath} when designated.
if (!basepath) {
it('respects `options.ignore` values (RegExp)', () => {
return nightmare.goto(`${base}/foobar`)
.evaluate(() => document.documentElement.textContent)
.then(text => {
assert.equal(text, 'foobar');
});
});

it('respects `options.ignore` values (String #1)', () => {
return nightmare.goto(`${base}/buzz`)
.evaluate(() => document.documentElement.textContent)
.then(text => {
assert.equal(text, 'buzz');
});
});

it('respects `options.ignore` values (String #2)', () => {
return nightmare.goto(`${base}/fizzer`)
.evaluate(() => document.documentElement.textContent)
.then(text => {
assert.equal(text, 'fizzer');
});
});

it('respects `options.ignore` values (Function)', () => {
return nightmare.goto(`${base}/hello`)
.evaluate(() => document.documentElement.textContent)
.then(text => {
assert.equal(text, 'hello');
});
});
}

it('does not attempt client-side navigation to server routes', () => {
return nightmare.goto(`${base}/blog/how-is-sapper-different-from-next`)
.init()
Expand Down