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 (+3 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 = false | Plugin<any> | PluginOption[] | Promise<false | Plugin<any> | PluginOption[] | null | undefined> | null | undefined
} from 'vite';
import { gracileconst gracile: (config?: GracileConfig) => any[]
The main Vite plugin for loading the Gracile framework.
} from '@gracile/gracile/plugin';
export default defineConfigfunction defineConfig(config: UserConfig): UserConfig (+3 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[] | undefined
Array of vite plugins to use.
: [
gracilefunction gracile(config?: GracileConfig | undefined): any[]
The main Vite plugin for loading the Gracile framework.
({
pagesGracileConfig.pages?: {
premises?: {
expose?: boolean | undefined;
include?: string[] | undefined;
exclude?: string[] | undefined;
} | undefined;
} | undefined
Settings for pages in /src/routes
.
: { premisespremises?: {
expose?: boolean | undefined;
include?: string[] | undefined;
exclude?: string[] | undefined;
} | undefined
Premises are the document and the properties necessary for page template
rendering.
You can access them via:
.../_my-route/__index.props.json
.../_my-route/__index.doc.html
They are accessible with the dev/server handler and are outputted as
static files for the static output or for server pre-rendered pages.
They can be use for implementing client-side routing.
: { exposeexpose?: boolean | undefined
: 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 { createRouterfunction createRouter(config?: GracileRouterConfig): GracileRouter
} from '@gracile-labs/client-router/create';
As an event target, you can listen to events or navigate programmatically from anywhere.
export const routerconst router: GracileRouter
= createRouterfunction createRouter(config?: GracileRouterConfig): GracileRouter
();
📄 ./src/document.client.ts
import './client-router.js';
📄 ./src/routes/(home).ts
import { routerconst router: GracileRouter
} from '../client-router.js';
// ...
if (!isServer) {
// ...
Trigger as soon as a new URL is requested.
routerconst router: GracileRouter
.addEventListenerGracileRouter.addEventListener(type: EventType, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
('route-changed', () => {
this.isSearchBoxVisible = false;
});
Trigger when the route template is fully rendered and displayed.
routerconst router: GracileRouter
.addEventListenerGracileRouter.addEventListener(type: EventType, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
('route-rendered', () =>
requestIdleCallbackfunction requestIdleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): number
(() => initCardsHover()),
);
setTimeoutfunction setTimeout<[]>(callback: () => void, ms?: number): NodeJS.Timeout (+2 overloads)
Schedules execution of a one-time callback
after delay
milliseconds.
The callback
will likely not be invoked in precisely delay
milliseconds.
Node.js makes no guarantees about the exact timing of when callbacks will fire,
nor of their ordering. The callback will be called as close as possible to the
time specified.
When delay
is larger than 2147483647
or less than 1
, the delay
will be set to 1
. Non-integer delays are truncated to an integer.
If callback
is not a function, a TypeError
will be thrown.
This method has a custom variant for promises that is available using timersPromises.setTimeout()
.
(() => {
routerconst router: GracileRouter
.navigateGracileRouter.navigate(url: string | URL, options?: {
backNav?: boolean;
replace?: boolean;
}): Promise<void>
('/docs/');
}, 2000);
}