From 17d3ceb547e639978f918b983c5111363e16c464 Mon Sep 17 00:00:00 2001 From: Ilya Lebedev Date: Tue, 26 Mar 2019 12:33:06 +0300 Subject: [PATCH] version 0.1.2 --- package.json | 2 +- src/Routes.js | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index d3c14c5..7661c0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-links", - "version": "0.1.1", + "version": "0.1.2", "description": "", "main": "dist/index.js", "scripts": { diff --git a/src/Routes.js b/src/Routes.js index 5774a13..0157433 100644 --- a/src/Routes.js +++ b/src/Routes.js @@ -6,10 +6,11 @@ import NextRouter from 'next/router'; import Route from './Route'; export default class Routes { - constructor({ Link = NextLink, Router = NextRouter } = {}) { + constructor({ Link = NextLink, Router = NextRouter, logger } = {}) { this.routes = []; this.Link = this.getLink(Link); this.Router = this.getRouter(Router); + this.logger = logger || console; } add(name, pattern, page) { @@ -68,7 +69,11 @@ export default class Routes { const route = this.findByName(nameOrUrl); if (route) { - return { route, urls: route.getUrls(params), byName: true }; + return { + route, + urls: route.getUrls(params), + byName: true, + }; } else { const { route, query } = this.match(nameOrUrl); const href = route ? route.getHref(query) : nameOrUrl; @@ -98,12 +103,23 @@ export default class Routes { getLink(Link) { return props => { const { route, params, to, hash, ...newProps } = props; + let href; + let as; + + try { + const urls = this.findAndGetUrls(route || to, params).urls; + + href = urls.href; + as = urls.as + (hash ? `#${hash}` : ''); + } catch (err) { + this.logger.error( + `Link url composing failed. route="${route || + to}", params: ${JSON.stringify(params)},`, + err + ); + } - const { href, as } = this.findAndGetUrls(route || to, params).urls; - - return ( - - ); + return ; }; }