Client-side router
Quickly add a description, title, open graph, etc. in your page’s document
head.
This add-on will take care of the nitty-gritty.
Caution
Experimental. This is not well tested, nor it is customizable enough.
Installation
npm i @gracile-labs/client-router
Activate page premises:
📄 ./vite.config.ts
import { defineConfigfunction defineConfig(config: UserConfig): UserConfig (+5 overloads)Type helper to make it easier to use vite.config.ts
accepts a direct
{@link
UserConfig
}
object, or a function that returns it.
The function receives a
{@link
ConfigEnv
}
object.
, type PluginOptiontype PluginOption = Plugin<any> | {
name: string;
} | FalsyPlugin | PluginOption[] | Promise<Plugin<any> | {
name: string;
} | FalsyPlugin | PluginOption[]>
} from 'vite';
import { gracileimport gracile } from '@gracile/gracile/plugin';
export default defineConfigfunction defineConfig(config: UserConfig): UserConfig (+5 overloads)Type helper to make it easier to use vite.config.ts
accepts a direct
{@link
UserConfig
}
object, or a function that returns it.
The function receives a
{@link
ConfigEnv
}
object.
({
// ...
pluginsUserConfig.plugins?: PluginOption[] | undefinedArray of vite plugins to use.
: [
gracileimport gracile ({
pagespages: {
premises: {
expose: boolean;
};
}
: { premisespremises: {
expose: boolean;
}
: { exposeexpose: boolean : true } },
}),
// ...
],
});
Usage
After installing the @gracile-labs/client-router, you can create a client
router instance and add it to your document client scripts:
📄 ./src/client-router.ts
import { createRouterimport createRouter } from '@gracile-labs/client-router/create';
As an event target, you can listen to events or navigate programmatically from anywhere.
export const routerconst router: any = createRouterimport createRouter ();
📄 ./src/document.client.ts
import './client-router.js';
📄 ./src/routes/(home).ts
import { routerconst router: any } from '../client-router.js';
// ...
if (!isServer) {
// ...
Trigger as soon as a new URL is requested.
routerconst router: any .addEventListener('route-changed', () => {
this.isSearchBoxVisible = false;
});
Trigger when the route template is fully rendered and displayed.
routerconst router: any .addEventListener('route-rendered', () =>
requestIdleCallbackfunction requestIdleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): numberThe window.requestIdleCallback() method queues a function to be called during a browser's idle periods. This enables developers to perform background and low priority work on the main thread, without impacting latency-critical events such as animation and input response. Functions are generally called in first-in-first-out order; however, callbacks which have a timeout specified may be called out-of-order if necessary in order to run them before the timeout elapses.
(() => initCardsHover()),
);
setTimeoutfunction setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number (() => {
routerconst router: any .navigate('/docs/');
}, 2000);
}