Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thedahoom/sveltekit-seo
Sveltekit SEO component to save your time
https://github.com/thedahoom/sveltekit-seo
component dependency-free jsonld lightweight no-dependencies seo seo-optimization svelte sveltekit
Last synced: 1 day ago
JSON representation
Sveltekit SEO component to save your time
- Host: GitHub
- URL: https://github.com/thedahoom/sveltekit-seo
- Owner: TheDahoom
- License: mit
- Created: 2024-04-05T19:53:26.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-01-10T20:25:12.000Z (23 days ago)
- Last Synced: 2025-01-10T21:28:55.311Z (23 days ago)
- Topics: component, dependency-free, jsonld, lightweight, no-dependencies, seo, seo-optimization, svelte, sveltekit
- Language: Svelte
- Homepage: https://skseo.dev
- Size: 105 KB
- Stars: 130
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A dead simple, no dependencies, svelte component that automates a lot of the annoying SEO parts for you.
Also optionally includes functionality for social media preview support[![Minzip size](https://img.shields.io/bundlephobia/minzip/sk-seo?style=for-the-badge)](https://img.shields.io/bundlephobia/minzip/sk-seo)
[![Installs](https://img.shields.io/npm/d18m/sk-seo?style=for-the-badge)](https://img.shields.io/npm/d18m/sk-seo)
[![GitHub Repo stars](https://img.shields.io/github/stars/TheDahoom/sveltekit-seo?style=for-the-badge&label=STAR)](https://github.com/TheDahoom/Sveltekit-seo)
[![GitHub Forks](https://img.shields.io/github/forks/TheDahoom/sveltekit-seo?style=for-the-badge&logo=github&label=fork)](https://github.com/TheDahoom/Sveltekit-seo/fork)## Installation
```bash
npm i -D sk-seo
```
If you're using @adapter-static, make sure to follow this
## Usage
Add the component to your layout file (eg: `+layout.svelte`).
```svelteimport Seo from 'sk-seo';
```
Add a `+layout.js` file alongside your `+layout.svelte` with a load function and return data with the
SEO/Meta that you need:
> [!TIP]
> For type support follow this [guide](https://github.com/TheDahoom/Sveltekit-seo/wiki/v0.5--types)
```js
// +layout.js
export const load = async ({ url }) => {
// OPTIONAL: You can use url.origin to get the base URL,
// or even url.href to get the full URL.
// (For example, to get URLs of images in your /static folder), like this:
// imageURL: `${url.origin}/image.jpg`
return {
title: 'Dahoom - Official',
description: 'The official website of Dahoom',
keywords: 'dahoom, official, website'
// ... and more
}
}
```> [!TIP]
> You can override your `+layout.js` meta from any `+page.js`.Make sure to add a `+page.js` with a load function to all your pages with the SEO/Meta that you need:
```js
// contacts/+page.js
export const load = async ({ url }) => {
// Title, description and keywords set here will replace the title set in +layout.js while visiting /contacts
return {
title: 'Contacts',
description: 'Where to contact Dahoom AlShaya, whether for business needs or general inquiries',
keywords: 'Contact, business, inquiries',
}
}
```> [!TIP]
> This also works with `+layout.server.js` and `+page.server.js`.## Component method (Not recommended, duplicates meta tags)
Put a `` tag in each page you want to have SEO for.
> [!WARNING]
> This's fine as long as you're making a single-page website (such as, just a homepage). But if you're making a
> multi-page website, you should use the previous method!
```svelteimport Seo from 'sk-seo';
```
> [!CAUTION]
> Using `` on each page will duplicate meta tags. This's why we recommend using the first method
> (`load` functions and `` only in your `+layout.svelte`).### Conflicting stores/load return values
You could even combine stores (with any name that you want, just make sure to use the same name that you return from
your `+layout.js/+page.js` load function) and manual input if you really have to:
```svelteimport Seo from 'sk-seo';
import { page } from '$app/stores';description="Where to contact Dahoom AlShaya, whether for business needs or general inquiries"
keywords="Contact, business, inquiries"
/>
```## Standard Parameters
| 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 |# Advanced
All these choices are optional
| 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 (used by search engines for names, contact information, [etc](https://json-ld.org)) | object `{}` | ~ |
| `imageURL`| The URL of the image to be used for preview (twitter, discord image preview when your url is shared) | 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](https://schema.org/docs/full.html)) | string | website |## How it works
The component uses `` to place meta tags that are filled with sveltekit $page and inputted variables, this means that a lot of the tags are automatically filled for you for each page in your website. An example of this is `og:url`, which requires the url of the current page:
```svelte```
Since v0.5 the component also makes use of `$page.data` **stores**. These will be automatically picked up by the component and used to fill in the meta tags## Extendable
If you want to use an unusual meta tag or use your own custom one (eg: google site verification). It's easy as:
```svelte
```
## Why
A lot of SEO is repeated boilerplate for twitter, open graph and schemaOrg. This component's sole purpose is to do away with all the annoyances and just help you focus on your content without having to spend hours making sure all the meta tags are correctly set on each and every page.I initially made this for my personal website and decided to open source it to so that no one has to go through the headache I did to make sure everything is functional.
## Prerendering
If you are using adapter-static, you need to add your base URL in `svelte.config.js`, otherwise `$page.url` will default to `http://sveltekit-prerender`.
```js
// svelte.config.js
const config = {
kit: {
prerender: {
origin: 'https://mossberg.dev' // Replace with your URL.
}
}
}
```## Duplicated Meta?
If you're behind `Cloudflare` and find yourself with duplicated meta tags, then you should disable auto-minify!`Speed -> Optimization -> Content Optimization -> Auto Minify -> UNCHECK HTML`
> [!WARNING]
> Still having duplicated meta? Make sure that you're not using `` in each page.## Credits
- Thanks to [GABRYCA](https://github.com/GABRYCA) for implementing a new cool new approach, svelte 5 and the multitude of additions that came with them!
- Thanks to [ArchangelGCA](https://github.com/ArchangelGCA) for the multiple quality of life imporvements and fixes!
- Thanks to [RodneyLab](https://github.com/rodneylab) for his [blog](https://rodneylab.com/adding-schema-org-markup-to-sveltekit-site/) post which taught me about jsonLd and for suggesting an interesting snippet of code to render jsonLd
## License
[MIT License](https://github.com/TheDahoom/Sveltekit-seo/blob/main/LICENSE)## As seen on
[Svelte Blog](https://svelte.dev/blog/whats-new-in-svelte-may-2024)
[The Guardian](https://youtu.be/iik25wqIuFo)
[New York Times](https://youtu.be/iik25wqIuFo)