34
loading...
This website collects cookies to deliver better user experience
{
"@context": "http://schema.org/",
"@type": "Person",
"name": "Jane Doe",
"jobTitle": "Professor",
"telephone": "(425) 123-4567",
"url": "http://www.janedoe.com"
}
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Person",
"name": "Jane Doe",
"jobTitle": "Professor",
"telephone": "(425) 123-4567",
"url": "http://www.janedoe.com"
}
</script>
script
tag, mark it as a type of application/ld+json
and you're all set. There's way more to the different kinds of schemas you may want to include - check out the JSON-LD docs for a much better walk through of all the details than I would ever fit into a single blog post.script
tag for all the JavaScript, style
tag for the component's CSS, and one or more HTML elements for the actual DOM elements.application/ld+json
script in your components.script
tag in a component. A Google search will lead you down a rabbit hole of different combinations of Svelte's @html expressions and string literals to get it to compile.script
tags in your Svelte component. This is where many will reach for some combination of {$html}
and string literals..svelte
files.export function serializeSchema(thing) {
return `<script type="application/ld+json">${JSON.stringify(thing)}</script>`;
}
ld+json
script tag for it.import type { Thing, WithContext } from "schema-dts";
export type Schema = Thing | WithContext<Thing>;
export function serializeSchema(thing: Schema) {
return `<script type="application/ld+json">${JSON.stringify(
thing,
null,
2
)}</script>`;
}
schema-dts
defines types for all of the different schema objects, see below for a more detailed example with an Organization
object. This is a huge win, it's easy to accidentally structure the JSON wrong or have a typo in one of the property names. Setting it up to use TypeScript definitions we can make sure that our JSON objects are validated at build.Organization
object used by this very site.import site from `$data/site.json`;
export const organizationSchema: WithContext<Organization> = {
"@context": "https://schema.org",
"@type": "Organization",
"@id": `${site.url}#organization`,
url: site.url,
name: site.company.name,
description: site.description,
sameAs: [`https://twitter.com/${site.social.twitter}`],
logo: `${site.url}/favicon.svg`,
};
Organization
object is defined in TypeScript, verified at build-time, and can be unit tested if you want to make sure the site.json
data is hooked up properly.WithContext
doing? That's a clever setup from schema-dts
, the top-level JSON+LD object should have a @context
property. You can nest objects though, and they even have support for using an object graph format for multiple objects. Any nested objects, or every object in the graph, doesn't need @context
. WithContext
is a TypeScript wrapper for another type, in the example above I could have removed @context
from the object and it would be a valid Organization
type.LDTag.svelte
component similar to<script lang="ts">
import { serializeSchema } from "$utils/json-ld";
import type { Schema } from "$utils/json-ld";
export let schema: Schema;
</script>
<svelte:head>
{@html serializeSchema(schema)}
</svelte:head>