Skip to content

Commit

Permalink
turning off prefetch for pages that not in the routes list
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentImp committed Feb 12, 2020
1 parent 973fff4 commit 98efda5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ describe('UrlPrettifier getPrettyUrl', (): void => {
.toEqual({href: '/pageName?id=1', as: '/page-pretty-url-1'});
});
it('should return only href if the route does not exist', (): void => {
it('should return href if the route does not exist', (): void => {
expect(router.getPrettyUrl('unknownPage', {id: 1}))
.toEqual({href: '/unknownPage?id=1'});
});
it('should return href and prefetch equal to false if the route does not exist', (): void => {
expect(router.getPrettyUrl('unknownPage', {id: 1, preventPrefetchIfNoRoute: true}))
.toEqual({href: '/unknownPage?id=1', prefetch: false});
});
});
describe('UrlPrettifier getPrettyUrlPatterns (DEPRECATED)', (): void => {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export default class UrlPrettifier<T: string> {
;
}

getPrettyUrl(pageName: T, params: ParamType): RouteLinkParamsType {
getPrettyUrl(pageName: T, {preventPrefetchIfNoRoute = false, ...params}: ParamType): RouteLinkParamsType {
const route: ?RouteType<T> = this.routes.filter((currentRoute: RouteType<T>): boolean =>
currentRoute.page === pageName)[0];
return {
href: `/${pageName}${this.paramsToQueryString(params)}`,
...(preventPrefetchIfNoRoute && !route && {prefetch: false}),
...(route && route.prettyUrl
? {as: typeof route.prettyUrl === 'string' ? route.prettyUrl : route.prettyUrl(params)}
: {}
Expand Down

0 comments on commit 98efda5

Please sign in to comment.