sveltekit-canonicals Svelte Themes

Sveltekit Canonicals

Vite plugin for SvelteKit projects which replaces __CANONICAL_URL__ with the canonical URL of the page in +page.svelte files.

sveltekit-canonicals

Repo | NPM

Vite plugin for SvelteKit projects which replaces __CANONICAL_URL__ with the canonical URL of the page in +page.svelte files.

Usage

npm install --save-dev '@arjunsatarkar/sveltekit-canonicals'
npm install --save-dev '@types/node'

vite.config.ts

import { canonicals } from "@arjunsatarkar/sveltekit-canonicals"
import { defineConfig } from 'vite';
import { sveltekit } from '@sveltejs/kit/vite';
import path from "path";

export default defineConfig({
    plugins: [
        sveltekit(),
        canonicals({
            replace: "__CANONICAL_URL__", // Default, can omit
            routesDir: path.resolve(__dirname, "src/routes"),
            siteUrlRoot: "https://example.net/",
            trailingSlash: "always" // Or "never"; see https://svelte.dev/docs/kit/page-options#trailingSlash
        }),
    ],
});

src/app.d.ts

// See https://svelte.dev/docs/kit/types#app.d.ts
// for information about these interfaces
declare global {
    namespace App {}
    declare const __CANONICAL_URL__: string;
}

export { };

In +page.svelte files:

<svelte:head>
    <link rel="canonical" href={__CANONICAL_URL__} />
</svelte:head>

Copying

© Arjun Satarkar. MIT license; see LICENSE.txt.

Top categories

Loading Svelte Themes