crelte / routing / Route
Class: Route
A Route contains information about the current page for example the url, the site and its entry.
Extends
Constructors
Constructor
new Route(
url,
site,
entry,
template,
loadedData,
opts): Route;Create a new Route
Note
This should only be created by crelte
Parameters
url
string | URL
site
entry
template
loadedData
Record<string, any>
opts
RouteOptions = {}
Returns
Route
Overrides
Properties
entry
entry: Entry;The entry of the route
entryChanged
entryChanged: boolean;Wether the entry changed since the last Route change
index
index: number;the position in the browser history of this route this allows to find out if we can go back
Inherited from
loadedData
loadedData: Record<string, any>;The loaded data of the route
origin
origin: RouteOrigin;The origin of this route, See RouteOrigin
Inherited from
scrollY
scrollY: number | null;The scroll position of the current route
Note
This does not have to represent the current scroll position should more be used internally.
It might be useful for a new request to specify the wanted scroll position
Inherited from
site
site: Site;The Site of the route
Note
In the beforeRequest event the site might not match the url and could be the default site or one that matches the users language.
If that is important call route.siteMatches() to verify
Inherited from
template
template: TemplateModule;The template module of the route
url
url: URL;The url of the route
Inherited from
Accessors
baseUrl
Get Signature
get baseUrl(): string;Returns the base url of the route
Never ends with a slash
Example
const site = _; // site with url https://example.com/foo
const route = new Route('https://example.com/foo/bar/', null);
console.log(route.baseUrl); // 'https://example.com/foo'
const site2 = _; // site with url https://example.com/other
const route2 = new Route('https://example.com/foo/bar/', site2);
console.log(route2.baseUrl); // 'https://example.com'Returns
string
Inherited from
hash
Get Signature
get hash(): string;Returns the hash of the route
Example
const route = new Route('https://example.com/foo/bar/#hash', null);
console.log(route.hash); // '#hash'Returns
string
Set Signature
set hash(hash): void;Set the hash of the route
Example
const route = new Route('https://example.com/foo/bar/', null);
route.hash = '#hash';
console.log(route.url.href); // 'https://example.com/foo/bar/#hash'Parameters
hash
string
Returns
void
Inherited from
search
Get Signature
get search(): URLSearchParams;Returns the search params
Note
You might also have a look at getSearchParam and setSearchParam
Example
const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
console.log(route.search.get('a')); // '1'Returns
URLSearchParams
Inherited from
uri
Get Signature
get uri(): string;Returns the uri of the route
Never ends with a slash
Example
const site = _; // site with url https://example.com/fo
const route = new Route('https://example.com/foo/bar/', site);
console.log(route.uri); // '/bar'
const site2 = _; // site with url https://example.com/other
const route2 = new Route('https://example.com/foo/bar/?a=1', site2);
console.log(route2.uri); // '/foo/bar'Returns
string
Inherited from
Methods
canGoBack()
canGoBack(): boolean;Checks if there are previous routes which would allow it to go back
Returns
boolean
Inherited from
clone()
clone(): Route;Create a copy of the EntryRoute
Note
This does not make a copy of the entry, template or loadedData.
Returns
Route
Overrides
eq()
eq(route): boolean | null;Checks if the route is equal to another route
This checks the url but search params do not have to be in the same order
Note
This only check the url, not site or anything else.
Parameters
route
BaseRoute | null
Returns
boolean | null
Inherited from
eqHash()
eqHash(route): boolean | null;Checks if the hash is equal to another route
Parameters
route
BaseRoute | null
Returns
boolean | null
Inherited from
eqSearch()
eqSearch(route): boolean | null;Checks if the search params are equal to another route
Parameters
route
BaseRoute | null
Returns
boolean | null
Inherited from
eqUrl()
eqUrl(route): boolean | null;Checks if the route is equal to another route
This does not check the search params or hash
Parameters
route
BaseRoute | null
Returns
boolean | null
Inherited from
getContext()
getContext<T>(key): T | null;Returns a context value if it exists.
Type Parameters
T
T = any
Parameters
key
string
Returns
T | null
Inherited from
getSearchParam()
getSearchParam(key): string | null;Gets the search param
Example
const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
console.log(route.getSearchParam('a')); // '1'Parameters
key
string
Returns
string | null
Inherited from
getState()
getState<T>(key): T | null;Returns a state value if it exists.
Type Parameters
T
T = any
Parameters
key
string
Returns
T | null
Inherited from
inLivePreview()
inLivePreview(): boolean;Returns true if the route is in live preview mode
Returns
boolean
Inherited from
setContext()
setContext<T>(key, value): void;Sets a context value. If the value is null or undefined, the key will be removed.
When to use context
Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls. This context is not persistent and should be considered valid only for the current request/route. The context is not cloned in the clone call and will be the same object.
Type Parameters
T
T
Parameters
key
string
value
T | null | undefined
Returns
void
Inherited from
setSearchParam()
setSearchParam(key, value?): void;Sets the search param or removes it if the value is null, undefined or an empty string
Example
const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
route.setSearchParam('a', '3');
console.log(route.url.href); // 'https://example.com/foo/bar/?a=3&b=2'
route.setSearchParam('a', null);
console.log(route.url.href); // 'https://example.com/foo/bar/?b=2'Parameters
key
string
value?
string | number | null
Returns
void
Inherited from
setState()
setState<T>(key, value): void;Sets a state value. If the value is null or undefined, the key will be removed.
When to use state
State is used to store additional information that persists across route changes. The State is only available in the client code since it is stored using window.history.
Consider using setSearchParam instead to enable server side rendering.
Type Parameters
T
T
Parameters
key
string
value
T | null | undefined
Returns
void
Inherited from
siteMatches()
siteMatches(): boolean;Returns if the site matches the url
Returns
boolean