Skip to main content

@shuvi/runtime

Loader

type Loader<T extends {} = {}> = (
loaderContenxt: RouteLoaderContext
) => Promise<T | Response | void | undefined> | T | Response | void | undefined;

RouteLoaderContext

loaderContenxt is an object containing several useful properties and methods.

ctx.pathname

Current url path

Type

string

Example

If current url is /xx/yy?hello=world&test=123,
pathname will be /xx/yy

ctx.query

The query object of current url.

Type

interface Query {
[x: string]: any;
}

Example

If current url is /xx/yy?hello=world&test=123,
query will be

{
hello: 'world',
test: '123'
}

ctx.params

The params of current url.

If matched routes contains dynamic route, the params of dynamic route will be parsed as this params object.

Type

interface Params {
[x: string]: any;
}

Example

If the route is set as /:foo/:bar and the current url is /xx/yy?hello=world&test=123 params will be

{
foo: 'xx',
bar: 'yy'
}

ctx.redirect

make a redirect to another path.

Type

function redirect(toPath: string, status?: number = 302): Response;

Example

import { Loader } from "@shuvi/runtime";
export const loader: Loader = async ({ redirect }) => {
return redirect("/target", 301);
};

ctx.error

Make a redirect to error page.

Type

(body?: string, statusCode?: number = 500) => Response;

Example

import { Loader } from "@shuvi/runtime";
export const loader: Loader = async ({ redirect }) => {
return error("not found", 404);
};

ctx.appContext

Application context object, which is accessiable during the entire lifecycle of application

ctx.req

Current request information. Only available on server side.

AppContext

interface AppContext {
store: any;
pageData: any;
[x: string]: any;
}