-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: Virtual Routes Support #1799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ba998e8
add first test
illBeRoy 7c92837
new VirtualRoutes mixin that handles routes. fetch tries to use Virtu…
illBeRoy 7a2d0d0
cover all basic use cases
illBeRoy 3f359ed
regex matching in routes
illBeRoy 53c507f
covered all virtual routes tests
illBeRoy 9d816bc
added hack to fix config test on firefox
illBeRoy fb084f8
removed formatting regex matches into string routes
illBeRoy 1866fa2
added support for "next" function
illBeRoy 8bf6890
added docs
illBeRoy a022b3d
navigate now supports both hash and history routerModes
illBeRoy 1447898
waiting for networkidle in navigateToRoute helper
illBeRoy e8a12c0
promiseless implementation
illBeRoy e81429d
remove firefox workaround from catchPluginErrors test, since we no lo…
illBeRoy f0be4ca
updated docs
illBeRoy dbf45d4
updated docs for "alias" as well
illBeRoy dfbf77f
minor rephrasing
illBeRoy a245a2b
removed non-legacy code from exact-match; updated navigateToRoute hel…
illBeRoy 4277cc5
moved endsWith from router utils to general utils; added startsWith u…
illBeRoy 4bd7ac8
updated docs per feedback
illBeRoy ebfc235
moved navigateToRoute helper into the virtual-routes test file
illBeRoy bea7308
moved navigateToRoute to top of file
illBeRoy c748b33
updated docs per pr comments
illBeRoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Adds beginning of input (^) and end of input ($) assertions if needed into a regex string | ||
* @param {string} matcher the string to match | ||
* @returns {string} | ||
*/ | ||
export function makeExactMatcher(matcher) { | ||
const matcherWithBeginningOfInput = matcher.startsWith('^') | ||
jhildenbiddle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? matcher | ||
: `^${matcher}`; | ||
|
||
const matcherWithBeginningAndEndOfInput = | ||
matcherWithBeginningOfInput.endsWith('$') | ||
jhildenbiddle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? matcherWithBeginningOfInput | ||
: `${matcherWithBeginningOfInput}$`; | ||
|
||
return matcherWithBeginningAndEndOfInput; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { makeExactMatcher } from './exact-match'; | ||
import { createNextFunction } from './next'; | ||
|
||
/** @typedef {import('../Docsify').Constructor} Constructor */ | ||
|
||
/** @typedef {Record<string, string | VirtualRouteHandler>} VirtualRoutesMap */ | ||
/** @typedef {(route: string, match: RegExpMatchArray | null) => string | void | Promise<string | void> } VirtualRouteHandler */ | ||
|
||
/** | ||
* @template {!Constructor} T | ||
* @param {T} Base - The class to extend | ||
*/ | ||
export function VirtualRoutes(Base) { | ||
return class VirtualRoutes extends Base { | ||
/** | ||
* Gets the Routes object from the configuration | ||
* @returns {VirtualRoutesMap} | ||
*/ | ||
routes() { | ||
return this.config.routes || {}; | ||
} | ||
|
||
/** | ||
* Attempts to match the given path with a virtual route. | ||
* @param {string} path | ||
* @returns {Promise<string | null>} resolves to string if route was matched, otherwise null | ||
*/ | ||
matchVirtualRoute(path) { | ||
const virtualRoutes = this.routes(); | ||
|
||
const virtualRoutePaths = Object.keys(virtualRoutes); | ||
|
||
/** | ||
* This is a tail recursion that resolves to the first properly matched route, to itself or to null. | ||
* Used because async\await is not supported, so for loops over promises are out of the question... | ||
* @returns {Promise<string | null>} | ||
*/ | ||
function asyncMatchNextRoute() { | ||
const virtualRoutePath = virtualRoutePaths.shift(); | ||
if (!virtualRoutePath) { | ||
return Promise.resolve(null); | ||
} | ||
|
||
const matcher = makeExactMatcher(virtualRoutePath); | ||
const matched = path.match(matcher); | ||
|
||
if (!matched) { | ||
return Promise.resolve().then(asyncMatchNextRoute); | ||
} | ||
|
||
const virtualRouteContentOrFn = virtualRoutes[virtualRoutePath]; | ||
|
||
if (typeof virtualRouteContentOrFn === 'string') { | ||
return Promise.resolve(virtualRouteContentOrFn); | ||
} else if (typeof virtualRouteContentOrFn === 'function') { | ||
return Promise.resolve() | ||
.then(() => { | ||
if (virtualRouteContentOrFn.length <= 2) { | ||
return virtualRouteContentOrFn(path, matched); | ||
} else { | ||
const [resultPromise, next] = createNextFunction(); | ||
virtualRouteContentOrFn(path, matched, next); | ||
return resultPromise; | ||
} | ||
}) | ||
.then(contents => { | ||
if (typeof contents === 'string') { | ||
return contents; | ||
} else if (contents === false) { | ||
return null; | ||
} else { | ||
return asyncMatchNextRoute(); | ||
} | ||
}); | ||
} else { | ||
return Promise.resolve().then(asyncMatchNextRoute); | ||
} | ||
} | ||
|
||
return asyncMatchNextRoute(); | ||
} | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** @typedef {(value: any) => void} NextFunction */ | ||
|
||
/** | ||
* Creates a pair of a function and a promise. | ||
* When the function is called, the promise is resolved with the value that was passed to the function. | ||
* @returns {[Promise, NextFunction]} | ||
*/ | ||
export function createNextFunction() { | ||
let resolvePromise; | ||
const promise = new Promise(res => (resolvePromise = res)); | ||
|
||
function next(value) { | ||
resolvePromise(value); | ||
} | ||
|
||
return [promise, next]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.