Seo poprawki

This commit is contained in:
dm
2025-11-22 20:56:05 +01:00
parent bdf6c40755
commit 617de866e2
6 changed files with 100 additions and 32 deletions

View File

@@ -2,41 +2,43 @@
import yaml from "js-yaml";
import fs from "fs";
const { seo } = Astro.props;
// SEO z YAML (np. import seo from "../content/seo/home.yaml")
const seo = Astro.props.seo ?? {};
// Load global SEO config
// Global SEO (site + company)
const globalSeo = yaml.load(
fs.readFileSync("./src/content/seo/home.yaml", "utf8")
);
const { site, company } = globalSeo;
const {
title = site.name,
description = site.description,
url = "/",
image = site.logo,
keywords = [],
schema = {}
} = seo ?? {};
// Page SEO (sekcja "page" w YAML)
const page = seo.page ?? {};
const canonical = site.url + url;
// FINAL VALUES
const title = page.title ?? site.name;
const description = page.description ?? site.description;
const image = page.image ?? site.logo;
const canonical = site.url + (page.url ?? "/");
const keywords = page.keywords ?? [];
/* PREPARE JSON-LD AS SAFE STRINGS */
// Extra structured data (optional)
const extraSchema = page.schema ?? null;
const schemaWebsite = JSON.stringify({
// JSON-LD objects
const schemaWebsite = {
"@context": "https://schema.org",
"@type": "WebSite",
"url": site.url,
"name": site.name,
"potentialAction": {
"@type": "SearchAction",
"target": site.url + "/wyszukiwarka?query={search_term_string}",
"target": `${site.url}/wyszukiwarka?query={search_term_string}`,
"query-input": "required name=search_term_string"
}
});
};
const schemaLocalBusiness = JSON.stringify({
const schemaLocalBusiness = {
"@context": "https://schema.org",
"@type": "LocalBusiness",
"name": company.name,
@@ -56,13 +58,14 @@ const schemaLocalBusiness = JSON.stringify({
"longitude": company.lon
},
"url": site.url
});
};
/* Extra schema if passed from page */
const schemaExtra = Object.keys(schema).length > 0
? JSON.stringify(schema)
: null;
// JSON strings
const jsonWebsite = JSON.stringify(schemaWebsite);
const jsonBusiness = JSON.stringify(schemaLocalBusiness);
const jsonExtra = extraSchema ? JSON.stringify(extraSchema) : null;
---
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
@@ -76,7 +79,7 @@ const schemaExtra = Object.keys(schema).length > 0
<link rel="canonical" href={canonical} />
<!-- OG -->
<!-- OpenGraph -->
<meta property="og:type" content="website" />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
@@ -90,15 +93,15 @@ const schemaExtra = Object.keys(schema).length > 0
<meta name="twitter:description" content={description} />
<meta name="twitter:image" content={image} />
<!-- JSON-LD (Website) -->
<script type="application/ld+json">{schemaWebsite}</script>
<!-- JSON-LD: Website -->
<script type="application/ld+json" set:html={jsonWebsite}></script>
<!-- JSON-LD (LocalBusiness) -->
<script type="application/ld+json">{schemaLocalBusiness}</script>
<!-- JSON-LD: LocalBusiness -->
<script type="application/ld+json" set:html={jsonBusiness}></script>
<!-- Optional per-page schema -->
{schemaExtra && (
<script type="application/ld+json">{schemaExtra}</script>
<!-- JSON-LD: Extra schema -->
{jsonExtra && (
<script type="application/ld+json" set:html={jsonExtra}></script>
)}
<slot />