Skip to content

Commit

Permalink
version 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
bacher committed Mar 26, 2019
1 parent d9342e7 commit 17d3ceb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-links",
"version": "0.1.1",
"version": "0.1.2",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
30 changes: 23 additions & 7 deletions src/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
<Link {...newProps} href={href} as={as + (hash ? `#${hash}` : '')} />
);
return <Link {...newProps} href={href} as={as} />;
};
}

Expand Down

0 comments on commit 17d3ceb

Please sign in to comment.