diff --git a/README.md b/README.md index 88bb57d9c12..ada51dd9136 100644 --- a/README.md +++ b/README.md @@ -1798,11 +1798,11 @@ Route Functions give you full programmatic flexibility to define your routing lo export default ({ url, contextProps }) { // Route Functions allow us to implement advanced routing such as route guards. if (! contextProps.user.isAdmin) { - return { match: false } + return false } // We can use RegExp and any JavaScript tool we want. if (! /\/film\/[0-9]+\/admin/.test(url)) { - return { match: false } + return { match: false } // equivalent to `return false` } filmId = url.split('/')[2] return { @@ -1813,8 +1813,8 @@ export default ({ url, contextProps }) { } ``` -The `match` value can be a (negative) number which enables you to resolve route conflicts. -The higher the number, the higher the priority. +The `match` value can be a (negative) number which enables you to resolve route conflicts; +the higher the number, the higher the priority.

diff --git a/examples/vue/pages/hello/index.page.route.ts b/examples/vue/pages/hello/index.page.route.ts index 3544f13fa13..6a210b9bd09 100644 --- a/examples/vue/pages/hello/index.page.route.ts +++ b/examples/vue/pages/hello/index.page.route.ts @@ -2,7 +2,7 @@ // This is a route similar to `/hello/:name` but with details impossible to achieve with a route string. export default ({ url }: { url: string }) => { if (!url.startsWith('/hello')) { - return { match: false } + return false } const name = url.split('/')[2] || 'anonymous' return { match: true, contextProps: { name } } diff --git a/src/route.shared.ts b/src/route.shared.ts index 7f34a5690f8..6e671f38f69 100644 --- a/src/route.shared.ts +++ b/src/route.shared.ts @@ -203,12 +203,18 @@ function resolveRouteFunction( matchValue: boolean | number routeParams: Record } { - const result = routeFunction({ url: urlPathname, contextProps }) + let result = routeFunction({ url: urlPathname, contextProps }) + if ([true, false].includes(result)) { + result = { match: result } + } assertUsage( typeof result === 'object' && result !== null && result.constructor === Object, - `The Route Function ${routeFilePath} should return a plain JavaScript object, e.g. \`{ match: true }\`.` + `The Route Function ${routeFilePath} should return a boolean or a plain JavaScript object, e.g. \`{ match: true }\`.` ) - assertUsage(hasProp(result, 'match'), `The Route Function ${routeFilePath} should return a \`{ match }\` value.`) + if (!hasProp(result, 'match')) { + result.match = true + } + assert(hasProp(result, 'match')) assertUsage( typeof result.match === 'boolean' || typeof result.match === 'number', `The \`match\` value returned by the Route Function ${routeFilePath} should be a boolean or a number.`