Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | 2x 2x 2x 19x 19x 19x 12x 12x 12x 12x 12x 12x 12x 87x 87x 12x 2x 10x 9x 9x 9x 9x 9x 9x 1x 9x 9x 18x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 9x 9x 3x 3x 3x 3x | /* * Next Avenues * * (c) Samuel Joos * * Based on the work of * (c) Harminder Virk <virk@adonisjs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import Route from './Route'; import RouteGroup from './Group'; import RouteStore from './Store'; import { parse } from 'url'; import { errorMessage } from './utils'; import NextRouter from 'next/router'; /** * @description * Router is the public interface used to define * routes, groups and Link components. * * @singleton * * @class Router */ class Router { constructor() { this.getRequestHandler = this.getRequestHandler.bind(this); this.NextRouter = NextRouter; this._initialize(); } /** * @description * Creates a new route which resolves to a Next.js page component. * @function add * @param {string} route The route name. * @param {string} page The Next.js page component. * @return {Route} */ add(route, page) { const routeInstance = new Route(this, route, page); RouteStore.add(routeInstance); return routeInstance; } /** * @description * Resolves and returns the route that matches given **url** and **host** * * **Note:** The first matching route will be used. So make * sure the generic routes are created after the * static routes. * @function match * @param {string} url Url string to find a match for. * @param {string} host Domain to find a match for. * @returns {Object} */ match(url, host) { const protocol = `${this.protocol}://`; const parsedHost = parse(protocol + host); const domain = parsedHost.hostname; const parsedUrl = parse(url, true); const { pathname, query } = parsedUrl; let resolvedUrl = {}; /* * Find the first matching route. */ const matchingRoute = RouteStore.list().find(route => { resolvedUrl = route.resolve(pathname, domain); return resolvedUrl; }); /* * Return null when unable to find a route. */ if (!matchingRoute) { return {}; } return { ...matchingRoute.toJSON(), port: parsedHost.port, parsedUrl, pathname, query: { ...query, ...resolvedUrl.subdomains }, data: matchingRoute.data, params: resolvedUrl.params, domain }; } /** * @description * Create a group of routes. * Also see [Group](https://github.com/samueljoos/next-avenues/blob/master/docs/group.md). * * @function group * * @param {Function} callback Callback which should only contain calls to [Router.add](https://github.com/samueljoos/next-avenues/blob/master/docs/router.md#routeraddroute-page). * @param {string} [name] Same as using [Route.as](https://github.com/samueljoos/next-avenues/blob/master/docs/group.md#asname). * @returns {Group} * * @example * routes.group(() => { * routes.add('/', 'dashboard') * // the route name will be admin.dashboard * }, 'admin') */ group(callback, name) { this._validateGroupClosure(callback); this._validateNestedGroups(); RouteStore.breakpoint(name); callback(); /* * Create a new group and pass all the routes * to the group. */ const group = new RouteGroup(RouteStore.breakpointRoutes()); if (name) { group.as(name); } RouteStore.releaseBreakpoint(); return group; } /** * @description * Returns an array of all the registered routes * * @function list * * @returns {Array} */ list() { return RouteStore.list(); } /** * @description * Return the current active route. * This is usualy called inside the getInitialProps a Next.js page component. * * @function getCurrentRoute * @returns {Object} */ getCurrentRoute() { let path; let domain; Eif (typeof window !== 'undefined') { path = this.path || document.location.href; domain = document.location.host; } else { path = this.currentUrl; domain = this.currentHost; } return this.match(path, domain); } /** * @description * Middleware function for your nextjs server setup. * * @function getRequestHandler * @param {Next.App} app The value of Next.js next(). * @param {Function} [customHandler] Callback to customise the renderHandler parameters. * * @returns {Function} */ getRequestHandler(app, customHandler) { const nextHandler = app.getRequestHandler(); return (req, res) => { const route = this.match( req.url, req.headers.host ); this.currentUrl = req.url; this.currentHost = req.headers.host; this.port = route.port; this.domain = route.domain; this.protocol = req.headers.referer && req.headers.referer.split('://')[0] || 'http'; Eif (route.pathname) { Iif (customHandler) { customHandler({ req, res, route, query: route.query }); } else { app.render(req, res, '/' + route.page, route.query); } } else { nextHandler(req, res, route.parsedUrl); } }; } /** * @description * Push State helper for navigating to a route. * **note:** This doesn't work serverside. * * @param {string} name The route name. * @param {Object} [params] The route parameters. * @param {Object} [query] The route query parameters. * * @example * router.add('/post/:slug','blog-post').as('blog-post'); * router.pushRoute('blog-post', {slug:'post-slug'}, {order:'1'}); * // resolves to /post/post-slug?order=1 */ pushRoute(name, params, query) { this._browserHistoryApply('push', name, params, query); } /** * @description * Replace State helper for navigating to a route. * **note:** This doesn't work serverside. * * @param {string} name The route name. * @param {Object} [params] The route parameters. * @param {Object} [query] The route query parameters. * * @example * router.add('/post/:slug','blog-post').as('blog-post'); * router.pushRoute('blog-post', {slug:'post-slug'}, {order:'1'}); * // resolves to /post/post-slug?order=1 */ replaceRoute(name, params, query) { this._browserHistoryApply('replace', name, params, query); } /** * @description * Prefetch the route. * **note:** This doesn't work serverside. * * @param {string} name The route name. * @param {Object} [params] The route parameters. * @param {Object} [query] The route query parameters. * * @example * router.add('/post/:slug','blog-post').as('blog-post'); * router.pushRoute('blog-post', {slug:'post-slug'}, {order:'1'}); * // resolves to /post/post-slug?order=1 */ prefetchRoute(name, params, query) { this._browserHistoryApply('prefetch', name, params, query); } ////// // Private functions ////// /** * @description * Initialize the client url location data * * @function _validateGroupClosure * * @returns {void} * * @private */ _initialize() { Eif (typeof window !== 'undefined') { this.port = document.location.port; this.domain = document.location.host.split(':')[0]; this.protocol = document.location.protocol.split(':')[0]; this.NextRouter.events.on('routeChangeStart', (url) => { this.path = url; }); } } /** * @description * Validates the group closure to make sure * it is a function * * @function _validateGroupClosure * * @param {Function} callback * * @private */ _validateGroupClosure(callback) { Iif (typeof callback !== 'function') { throw errorMessage('Route.group expects a callback', callback); } } /** * @description * Validates that nested groups are not created. * * @function _validateNestedGroups * * @private */ _validateNestedGroups() { Iif (RouteStore.hasBreakpoint()) { RouteStore.releaseBreakpoint(); throw errorMessage( 'Nested route groups are not allowed', 500, 'E_NESTED_ROUTE_GROUPS' ); } } /** * @description * Helper function for browser history methods. * Used by [router.pushRoute](https://github.com/samueljoos/next-avenues/blob/master/docs/router.md#pushroutenameparamsquery) [router.replaceRoute](https://github.com/samueljoos/next-avenues/blob/master/docs/router.md#replaceroutenameparamsquery) [router.prefetchRoute](https://github.com/samueljoos/next-avenues/blob/master/docs/router.md#prefetchroutenameparamsquery) * * @param {string} method Should be one of push, replace or prefetch. * @param {string} name The route name. * @param {Object} params The route Params. * @param {Object} query The route query. */ _browserHistoryApply(method, name, params, query) { Iif (typeof window === 'undefined') { throw errorMessage('Pushing routes is only available in the client', 'router.push'); } const route = RouteStore.find(name, this.domain); const nextLinkProps = route.getNextLinkProps(params, { domain: this.domain, protocol: this.protocol, query }); this.NextRouter[method](nextLinkProps.href, nextLinkProps.as); } } export default new Router(); |