crelte / routing / Request
Class: Request
A Request is a Route with some extra options you get a Request from the onRequest event or in a loadGlobal function.
Extends
Constructors
Constructor
new Request(
url,
site,
opts): Request;Create a new Request
Parameters
url
string | URL
site
opts
RequestOptions = {}
Returns
Request
Overrides
Properties
disableLoadData
disableLoadData: boolean;Disable loading data
Default
falsedisableScroll
disableScroll: boolean;Disable scrolling for this request
Default
falseentry
entry: Entry | null;The entry of the request once loaded
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> | null;The loaded data of the request
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
statusCode
statusCode: number | null;The Status code that should be returned for a redirect
template
template: TemplateModule | null;The template module of the request once loaded
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
cancelled
Get Signature
get cancelled(): boolean;Returns
boolean
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
cancel()
cancel(): void;Cancel this request
Returns
void
canGoBack()
canGoBack(): boolean;Checks if there are previous routes which would allow it to go back
Returns
boolean
Inherited from
clone()
clone(): Request;Create a copy of the request
without including the entry, template, loadedData or cancelled state
Is this what we wan't, maybe it should copy everything
Returns
Request
Overrides
delayRender()
delayRender(): DelayRender;With delayRender you can make sure that the render waits until you are ready. This is useful for building page transitions.
Important
If you call delayRender you need to call ready/remove or the render will never happen
Example
import { onRequest } from 'crelte';
import { animate } from 'motion';
onRequest(async req => {
if (req.origin !== 'click' && req.origin !== 'manual') return;
const delay = req.delayRender();
await animate(plane, { x: '0%' });
// wait until the new page is ready to be rendered
// if the render was cancelled we return
if (await delay.ready()) return;
await animate(plane, { x: '100%' });
});Returns
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
Inherited from
toRoute()
toRoute(): Route;Create a Route from the Request
Throws
if the entry, template or loadedData is missing or the request was cancelled
Returns
fromRoute()
static fromRoute(route, opts): Request;Create a Request from a Route
Clears the entry, template, and loadedData todo should it?
Parameters
route
opts
RequestOptions = {}
Returns
Request