sk-seo

repository·main·Indexed 40 days ago

https://github.com/thedahoom/sveltekit-seo

A lightweight, dependency-free SvelteKit SEO component (v0.5.4) that automates the generation of meta tags, Twitter cards, OpenGraph properties, and Schema.org JSON-LD structured data. It supports a hierarchical data pattern, allowing developers to define global SEO defaults in root layouts and override them on specific pages via SvelteKit load functions and the $page.data store.

Tokens
3.5K
Snippets
13
Records
14
Agent score
49%

What's inside sk-seo

  1. Configure origin for Prerendering (adapter-static)

    main

    If you are using @adapter-static, you must define your base URL in svelte.config.js. If you don't, $page.url will default to http://sveltekit-prerender, which will break your SEO links and canonical URLs.

    // svelte.config.js
    const config = {
      kit: {
        prerender: {
          origin: 'https://mossberg.dev' // Replace with your URL.
        }
      }
    }
  2. Recommended Usage: Automate SEO via Load Functions

    main

    The recommended way to use sk-seo in a multi-page application is to place the <Seo /> component once in your root +layout.svelte and provide SEO data through SvelteKit load functions.

    This approach allows you to define global SEO defaults in +layout.js and override them on specific pages using +page.js (or +page.server.js). The component automatically picks up data from the $page.data store.

    <!-- +layout.svelte -->
    <script>
      import Seo from 'sk-seo';
    </script>
    
    <Seo />
    // +layout.js
    export const load = async ({ url }) => {
        return {
            title: 'Dahoom - Official',
            description: 'The official website of Dahoom',
            keywords: 'dahoom, official, website'
        }
    }
    // contacts/+page.js
    export const load = async ({ url }) => {
        // This overrides the title/description/keywords from +layout.js for the /contacts route
        return {
            title: 'Contacts', 
            description: 'Where to contact Dahoom AlShaya, whether for business needs or general inquiries',
            keywords: 'Contact, business, inquiries',
        }
    }
  3. Generate JSON-LD Schema.org structured data

    main

    To enable Schema.org structured data, set the schemaOrg prop to true. The component automatically constructs a JSON-LD script using the following logic:

    1. Context: Always uses https://schema.org.
    2. Type: Uses the provided schemaType array. If the array has multiple values, it uses the array; otherwise, it uses the first element.
    3. Automatic Fields: It automatically includes name, url (current page origin), image (from imageURL), and a logo object (using the logo prop).
    4. Socials: The socials array is mapped to the sameAs property.
    5. Customization: Any properties provided in the jsonld prop are merged into the final object, allowing you to override or extend the default schema.

    Note: The script is only rendered if schemaOrg is true AND at least one of socials, logo, or name is provided.

    <SEO 
        schemaOrg={true} 
        name="Acme Corp" 
        schemaType={['Organization']} 
        jsonld={{ @context: 'https://schema.org', contactPoint: { telephone: '123-456' } }}
    />
  4. Use the SEO component for automated meta tags

    main

    The SEO.svelte component automates the generation of meta tags (title, description, keywords, author), OpenGraph properties, Twitter cards, and JSON-LD Schema.org structured data.

    It is designed to be used in SvelteKit layouts. It follows a hierarchical data pattern: it first attempts to pull SEO data from the SvelteKit $page.data object (populated via load functions in +page.ts or +layout.ts), and falls back to the props passed directly to the component. This allows you to define global SEO defaults in a layout and override them on specific pages.

    <script>
        import SEO from './SEO.svelte';
    </script>
    
    <SEO 
        title="My Page Title" 
        description="A great description for my page" 
        imageURL="https://example.com/image.png"
        socials={['https://twitter.com/example']}
    />
  5. Use the Seo component for single-page websites

    main

    If you are building a single-page website (e.g., just a homepage), you can pass SEO parameters directly to the <Seo /> component on that page.

    Warning: Do not use this method for multi-page websites, as placing <Seo /> on every page will cause duplicate meta tags.

    <!-- contacts/+page.svelte -->
    <script>
      import Seo from 'sk-seo';
    </script>
    
    <Seo 
      title="Contact"
      description="Where to contact Dahoom AlShaya, whether for business needs or general inquiries"
      keywords="Contact, business, inquiries"
    />
  6. Extend the Seo component with custom meta tags

    main

    You can pass custom meta tags as children to the <Seo /> component. This is useful for tags not explicitly supported by the component, such as Google site verification.

    <Seo title="abc" description="def">
      <meta name="google-site-verification" content="abcd123">
    </Seo>
  7. Combine SEO stores with manual input

    main

    You can combine data from SvelteKit's $page.data store with manual component props if you need to map custom property names from your load functions to the SEO component.

    <!-- +layout.svelte -->
    <script>
      import Seo from 'sk-seo';
      import { page } from '$app/stores';
    </script>
    
    <Seo 
      title={$page.data.customTitle ?? ''} 
      description="Where to contact Dahoom AlShaya, whether for business needs or general inquiries"
      keywords="Contact, business, inquiries"
    />
  8. Reference: Advanced SEO Parameters

    main

    These optional parameters allow for deeper configuration of social media tags and Schema.org structured data.

    | Parameter | Description | Type | Default |
    | ------------- | ------------- | ------- | ------- |
    | `siteName`| The name of the site | string | ~ |
    | `canonical`| Current URL of the page. For resolving duplicate pages with SEO | string | ~ |
    | `twitter`| Indicates whether Twitter meta tags should be generated | boolean | true |
    | `openGraph`| Indicates whether og / OpenGraph meta tags should be generated | boolean | true |
    | `schemaOrg`| Indicates whether jsonLd/SchemaOrg meta script should be generated | boolean | false |
    | `schemaType`| The type of jsonld schema (Person, Organisation, Create) @default Person,Organisation | string/Array | ['Person', 'Organisation'] |
    | `jsonld`| Appends contents to jsonld script | object `{}` | ~ |
    | `imageURL`| The URL of the image to be used for preview (twitter, discord) | string | ~ |
    | `logo`| The logo image URL for SchemaOrg | string | ~ |
    | `author`| Represents the author of the page | string | ~ |
    | `socials`| An array of social media links for SchemaOrg | Array | ~ |
    | `name`| The name to be used for SchemaOrg | string | ~ |
    | `type`| The type of the page (website, article, etc) | string | website |
  9. Reference: Standard SEO Parameters

    main

    These are the primary parameters available for the <Seo /> component.

    | Parameter | Description | Type | Default |
    | ------------- | ------------- | ------- | ------- |
    | `title`| The title of the page | string | ~ |
    | `description`| The description of the page | string | ~ |
    | `keywords`| The keywords to be used for search engine optimization or search | string | ~ |
    | `index`| Whether or not crawlers should crawl this page | boolean | true |
  10. Configure SEO component props

    main

    The SEO component accepts the following props to control meta tags and structured data. If these props are not provided, the component attempts to use values from $page.data.

    // Core Meta Tags
    export let title = "";             // Page title
    export let description = "";      // Meta description
    export let keywords = "";         // Meta keywords
    export let canonical = "";        // Canonical URL (defaults to current URL if empty)
    export let author = "";           // Meta author
    
    // Branding & Identity
    export let siteName = "";         // OpenGraph site name
    export let name = "";             // Schema.org name
    export let type = "website";      // OpenGraph type (e.g., 'website', 'article')
    export let logo = "";             // URL for logo (used in OG and Schema)
    export let imageURL = "";          // URL for preview image (used in OG and Twitter)
    
    // Feature Toggles
    export let index = true;           // Whether to allow search engine indexing (controls robots meta)
    export let twitter = true;         // Whether to include Twitter card meta tags
    export let openGraph = true;        // Whether to include OpenGraph meta tags
    export let schemaOrg = false;      // Whether to include JSON-LD Schema.org script
    
    // Structured Data & Socials
    export let schemaType = ['Person', 'Organization']; // Array of Schema types
    export let socials = [];           // Array of URLs for 'sameAs' in Schema.org
    export let jsonld = {};            // Custom object to merge into the generated JSON-LD