new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Svelte · SvelteKit · all subjects

routing

150 notes in this subject, read out of this brain and free to use. This is page 3 of 3.

HttpMethod type values

HttpMethod is type 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'.

RouteSegment interface

RouteSegment has properties: content: string, dynamic: boolean, rest: boolean.

isRedirect() checks for redirect errors

The isRedirect() function checks whether a value is a redirect thrown by redirect(). Signature: isRedirect(e: unknown): e is Redirect_1

normalizeUrl() strips SvelteKit-internal suffixes

The normalizeUrl() function strips possible SvelteKit-internal suffixes and trailing slashes from URL pathnames. Available since 2.18.0. Returns the normalized URL and a denormalize method for adding the suffix back based on a new pathname or URL. Signature: normalizeUrl(url: URL | string): { url: URL; wasNormalized: boolean; denormalize: (url?: string | URL) => URL; }

redirect() function status codes and usage

The redirect() function redirects a request. When called during request handling, SvelteKit returns a redirect response. The thrown redirect must not be caught. Common status codes: 303 (See Other - redirect as GET, often after POST), 307 (Temporary Redirect - keeps method), 308 (Permanent Redirect - keeps method and transfers SEO). Signature: redirect(status: 300|301|302|303|304|305|306|307|308|({} & number), location: string | URL): never

NavigationType values

NavigationType is a union of: 'enter' (app hydrated/started), 'form' (form GET submitted), 'goto' (goto() or redirect), 'leave' (tab closed or different document navigation), 'link' (link click), 'popstate' (back/forward navigation)

NavigationBase interface properties

NavigationBase has: type (NavigationType), from (NavigationTarget | null where navigation triggered), to (NavigationTarget | null where navigation going), willUnload (boolean), complete (Promise<void> that resolves when complete)

NavigationTarget interface

NavigationTarget contains target navigation info: params (Params | null), route (with id: RouteId | null), url (URL being navigated to), scroll ({ x: number; y: number } | null representing scroll position)

Redirect interface properties

Redirect has: status (300|301|302|303|304|305|306|307|308), location (string for redirect destination)

RequestEvent cookies property

RequestEvent has a cookies property of type Cookies that allows getting or setting cookies related to the current request.

RequestEvent fetch property features

RequestEvent fetch property is equivalent to native fetch with additional features: can make credentialed requests on the server inheriting cookie and authorization headers, can make relative requests on the server, internal requests to +server.js routes go directly to handler without HTTP overhead, during SSR response is captured and inlined into HTML by hooking into Response text and json methods (headers not serialized unless explicitly included via filterSerializedResponseHeaders), and during hydration response is read from HTML guaranteeing consistency and preventing additional network request.

RequestEvent getClientAddress method

RequestEvent has getClientAddress(): string method that returns the client's IP address, set by the adapter.

RequestEvent params property

RequestEvent has params property containing route parameters, for example {slug: string} for route /blog/[slug]. In remote function context, params relate to the page the remote function was called from, not the remote function endpoint URL. Should never be used alone to determine authorization as these values are part of the request and could be manipulated.

RequestEvent platform property

RequestEvent has platform property of type Readonly<App.Platform> | undefined containing additional data made available through the adapter.

RequestEvent route.id property

RequestEvent has route.id property of type RouteId that is the ID of the current route, for example '/blog/[slug]' for src/routes/blog/[slug], or null when no route is matched. In remote function context, route.id relates to the page the remote function was called from, not the remote function endpoint. Should never be used alone to determine authorization as these values are part of the request and could be manipulated.

RequestEvent setHeaders method

RequestEvent has setHeaders(headers: Record<string, string>): void method to set response headers. Useful for caching. Cannot be called multiple times for the same header and cannot add set-cookie headers (use cookies API instead). Example: setHeaders({ 'cache-control': 'max-age=3600' }).

RequestEvent url property

RequestEvent has url property of type URL containing the requested URL. In remote function context, url relates to the page the remote function was called from, not the remote function endpoint. Should never be used alone to determine authorization as these values are part of the request and could be manipulated.

RequestEvent isDataRequest property

RequestEvent has isDataRequest boolean property that is true if the request comes from the client asking for +page/layout.server.js data. The url property will be stripped of internal data request information in this case.

RequestEvent isSubRequest property

RequestEvent has isSubRequest boolean property that is true for +server.js calls coming from SvelteKit without HTTP overhead. This happens when making same-origin fetch requests on the server.

RequestHandler type signature

RequestHandler has signature type RequestHandler<Params = AppLayoutParams<'/'>, RouteId = AppRouteId | null> = (event: RequestEvent<Params, RouteId>) => MaybePromise<Response>. It is a function exported from +server.js that corresponds to HTTP verbs (GET, PUT, PATCH, etc) and handles requests with that method.

RouteDefinition structure

RouteDefinition has the following properties: id: string, api: {methods: Array<HttpMethod | '*'>}, page: {methods: Array<Extract<HttpMethod, 'GET' | 'POST'>>}, pattern: RegExp, prerender: PrerenderOption, segments: RouteSegment[], methods: Array<HttpMethod | '*'>, config: Config.

PageData interface for shared page data

PageData defines the common shape of page.data state and the $page.data store - the data that is shared between all pages. Load and ServerLoad functions in ./$types will be narrowed accordingly. Use optional properties for data only present on specific pages and do not add an index signature.

RequestHandler and Load types accept Params generic argument

The RequestHandler and Load types both accept a Params argument allowing you to type the params object. For example, an endpoint can specify expected params like foo, bar, and baz using a Params generic argument to RequestHandler.

SvelteKit generates $types.d.ts files for routes

SvelteKit automatically generates .d.ts files for each endpoint and page in the .svelte-kit/types directory. These files export typed versions of RequestHandler and PageLoad with the correct RouteParams.

Import types from $types sibling module

Generated .d.ts files can be imported into endpoints and pages as siblings using the path './$types' thanks to the rootDirs option in TypeScript configuration.

PageData and LayoutData types available through $types

The return types of load functions are available through the $types module as PageData and LayoutData respectively. The union of return values of all Actions is available as ActionData.

PageProps helper type for page component props

Starting with version 2.16.0, PageProps is a helper type that defines data: PageData and form: ActionData when actions are defined. It can be used to type the destructured props in page components.

LayoutProps helper type for layout component props

Starting with version 2.16.0, LayoutProps is a helper type that defines data: LayoutData and children: Snippet for layout components.

Error interface defines error shape

The Error interface defines the common shape of expected and unexpected errors. Expected errors are thrown using the error function. Unexpected errors are handled by the handleError hooks which should return this shape. It has a required message property of type string.

PageState interface for page.state

PageState defines the shape of the page.state object, which can be manipulated using the pushState and replaceState functions from $app/navigation.

Give your agent this brain