build(aio): test Service Worker "routing" configuration (#21763)

PR Close #21763
This commit is contained in:
Pete Bacon Darwin
2018-01-25 20:19:32 +00:00
committed by Alex Rickabaugh
parent bf29936af9
commit 240aed29e0
3 changed files with 54 additions and 1 deletions

View File

@ -27,3 +27,23 @@ export function loadLegacyUrls() {
const urls = readFileSync(pathToLegacyUrls, 'utf8').split('\n').map(line => line.split('\t'));
return urls;
}
export function loadSWRoutes() {
const pathToSWManifest = path.resolve(__dirname, '../../ngsw-manifest.json');
const contents = cjson.load(pathToSWManifest);
const routes = contents.routing.routes;
return Object.keys(routes).map(route => {
const routeConfig = routes[route];
switch (routeConfig.match) {
case 'exact':
return (url) => url === route;
case 'prefix':
return (url) => url.startsWith(route);
case 'regex':
const regex = new RegExp(route);
return (url) => regex.test(url);
default:
throw new Error(`unknown route config: ${route} - ${routeConfig.match}`);
}
});
}