@umituz/web-seo 2.6.0 → 2.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/domains/seo/components/JsonLd.js +1 -1
- package/dist/domains/seo/hooks/useJsonLd.js +1 -1
- package/dist/domains/seo/ssr/renderer.js +1 -1
- package/dist/domains/seo/vite/html-rewriter.js +14 -1
- package/dist/domains/seo/vite/html-rewriter.test.d.ts +1 -0
- package/dist/domains/seo/vite/html-rewriter.test.js +41 -0
- package/dist/domains/seo/vite/types.d.ts +6 -0
- package/package.json +3 -2
|
@@ -18,7 +18,7 @@ const JsonLdComponent = ({ data, strategy = "afterInteractive", id }) => {
|
|
|
18
18
|
const script = document.createElement("script");
|
|
19
19
|
script.type = SCRIPT_TYPE;
|
|
20
20
|
script.textContent = JSON.stringify(items[index]);
|
|
21
|
-
script.setAttribute("data-seo-id", dataIds[index]);
|
|
21
|
+
script.setAttribute("data-seo-id", dataIds[index] ?? "");
|
|
22
22
|
script.setAttribute("data-seo-strategy", strategy);
|
|
23
23
|
document.head.appendChild(script);
|
|
24
24
|
if (index === 0)
|
|
@@ -21,7 +21,7 @@ export const useJsonLd = (data, options) => {
|
|
|
21
21
|
const script = document.createElement("script");
|
|
22
22
|
script.type = SCRIPT_TYPE;
|
|
23
23
|
script.textContent = JSON.stringify(item);
|
|
24
|
-
script.setAttribute("data-seo-id", ids[index]);
|
|
24
|
+
script.setAttribute("data-seo-id", ids[index] ?? "");
|
|
25
25
|
script.setAttribute("data-seo-strategy", strategy);
|
|
26
26
|
document.head.appendChild(script);
|
|
27
27
|
});
|
|
@@ -10,7 +10,7 @@ const joinTags = (ctx) => ({
|
|
|
10
10
|
});
|
|
11
11
|
const findTitle = (meta) => {
|
|
12
12
|
const match = meta.match(/<title>([\s\S]*?)<\/title>/);
|
|
13
|
-
return match ? match[1] : "";
|
|
13
|
+
return match ? match[1] ?? "" : "";
|
|
14
14
|
};
|
|
15
15
|
const stripTitle = (meta) => meta.replace(/<title>[\s\S]*?<\/title>\n?/, "");
|
|
16
16
|
export const renderSEO = (options) => {
|
|
@@ -57,7 +57,7 @@ export const injectRouteMeta = (htmlTemplate, meta, siteUrl, routePath) => {
|
|
|
57
57
|
const htmlTagRegex = /<html([^>]*)>/i;
|
|
58
58
|
const match = html.match(htmlTagRegex);
|
|
59
59
|
if (match) {
|
|
60
|
-
let attrs = match[1];
|
|
60
|
+
let attrs = match[1] ?? "";
|
|
61
61
|
if (/lang=["'][^"']*["']/i.test(attrs)) {
|
|
62
62
|
attrs = attrs.replace(/lang=["'][^"']*["']/i, `lang="${escapeHtml(meta.lang)}"`);
|
|
63
63
|
}
|
|
@@ -89,6 +89,16 @@ export const injectRouteMeta = (htmlTemplate, meta, siteUrl, routePath) => {
|
|
|
89
89
|
const canonicalRegex = /<link\s+[^>]*?rel=["']canonical["'][^>]*?\/?>/i;
|
|
90
90
|
const newCanonical = `<link rel="canonical" href="${escapeHtml(canonicalUrl)}" />`;
|
|
91
91
|
html = upsertTag(html, canonicalRegex, newCanonical);
|
|
92
|
+
if (meta.alternates && meta.alternates.length > 0) {
|
|
93
|
+
html = html.replace(/<link\s+[^>]*?rel=["']alternate["'][^>]*?hreflang=[^>]*?\/?>/gi, "");
|
|
94
|
+
const alternateTags = meta.alternates
|
|
95
|
+
.map((entry) => `<link rel="alternate" hreflang="${escapeHtml(entry.lang)}" href="${escapeHtml(entry.href)}" />`)
|
|
96
|
+
.join("\n ");
|
|
97
|
+
const headClosingIndex = html.toLowerCase().indexOf("</head>");
|
|
98
|
+
if (headClosingIndex !== -1) {
|
|
99
|
+
html = `${html.slice(0, headClosingIndex)} ${alternateTags}\n ${html.slice(headClosingIndex)}`;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
92
102
|
const ogTitle = meta.og?.title ?? meta.title;
|
|
93
103
|
if (ogTitle)
|
|
94
104
|
html = upsertMetaTag(html, "property", "og:title", ogTitle);
|
|
@@ -104,6 +114,9 @@ export const injectRouteMeta = (htmlTemplate, meta, siteUrl, routePath) => {
|
|
|
104
114
|
if (meta.og?.siteName) {
|
|
105
115
|
html = upsertMetaTag(html, "property", "og:site_name", meta.og.siteName);
|
|
106
116
|
}
|
|
117
|
+
if (meta.og?.locale) {
|
|
118
|
+
html = upsertMetaTag(html, "property", "og:locale", meta.og.locale);
|
|
119
|
+
}
|
|
107
120
|
if (meta.og?.image) {
|
|
108
121
|
html = upsertMetaTag(html, "property", "og:image", meta.og.image);
|
|
109
122
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { injectRouteMeta } from "./html-rewriter.js";
|
|
3
|
+
const template = `<!doctype html>
|
|
4
|
+
<html lang="en">
|
|
5
|
+
<head>
|
|
6
|
+
<meta charset="UTF-8" />
|
|
7
|
+
<title>Fallback</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body><div id="root"></div></body>
|
|
10
|
+
</html>`;
|
|
11
|
+
describe("injectRouteMeta alternates", () => {
|
|
12
|
+
it("hreflang kümesini ve og:locale yazar", () => {
|
|
13
|
+
const out = injectRouteMeta(template, {
|
|
14
|
+
title: "Start",
|
|
15
|
+
lang: "tr",
|
|
16
|
+
alternates: [
|
|
17
|
+
{ lang: "tr", href: "https://x.test/tr" },
|
|
18
|
+
{ lang: "en", href: "https://x.test/en" },
|
|
19
|
+
{ lang: "x-default", href: "https://x.test/en" },
|
|
20
|
+
],
|
|
21
|
+
og: { locale: "tr_TR" },
|
|
22
|
+
}, "https://x.test", "/tr");
|
|
23
|
+
expect(out).toContain('<link rel="alternate" hreflang="tr" href="https://x.test/tr" />');
|
|
24
|
+
expect(out).toContain('<link rel="alternate" hreflang="en" href="https://x.test/en" />');
|
|
25
|
+
expect(out).toContain('<link rel="alternate" hreflang="x-default" href="https://x.test/en" />');
|
|
26
|
+
expect(out).toContain('<meta property="og:locale" content="tr_TR" />');
|
|
27
|
+
expect(out).toContain('<html lang="tr">');
|
|
28
|
+
expect(out).toContain('<link rel="canonical" href="https://x.test/tr" />');
|
|
29
|
+
});
|
|
30
|
+
it("alternates yoksa link eklemez", () => {
|
|
31
|
+
const out = injectRouteMeta(template, { title: "A" }, "https://x.test", "/a");
|
|
32
|
+
expect(out).not.toContain('hreflang=');
|
|
33
|
+
expect(out).not.toContain('og:locale');
|
|
34
|
+
});
|
|
35
|
+
it("mevcut hreflang linklerini değiştirir (idempotent)", () => {
|
|
36
|
+
const first = injectRouteMeta(template, { title: "A", alternates: [{ lang: "de", href: "https://x.test/de" }] }, "https://x.test", "/de");
|
|
37
|
+
const second = injectRouteMeta(first, { title: "A", alternates: [{ lang: "fr", href: "https://x.test/fr" }] }, "https://x.test", "/fr");
|
|
38
|
+
expect(second).toContain('hreflang="fr"');
|
|
39
|
+
expect(second.match(/hreflang="de"/g)).toBeNull();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -5,6 +5,10 @@ export interface SitemapRouteItem {
|
|
|
5
5
|
changefreq?: SitemapChangeFreq;
|
|
6
6
|
lastmod?: string;
|
|
7
7
|
}
|
|
8
|
+
export interface HreflangAlternate {
|
|
9
|
+
lang: string;
|
|
10
|
+
href: string;
|
|
11
|
+
}
|
|
8
12
|
export interface RouteMeta {
|
|
9
13
|
title?: string;
|
|
10
14
|
description?: string;
|
|
@@ -12,6 +16,7 @@ export interface RouteMeta {
|
|
|
12
16
|
canonical?: string;
|
|
13
17
|
robots?: string;
|
|
14
18
|
lang?: string;
|
|
19
|
+
alternates?: HreflangAlternate[];
|
|
15
20
|
og?: {
|
|
16
21
|
title?: string;
|
|
17
22
|
description?: string;
|
|
@@ -19,6 +24,7 @@ export interface RouteMeta {
|
|
|
19
24
|
type?: string;
|
|
20
25
|
url?: string;
|
|
21
26
|
siteName?: string;
|
|
27
|
+
locale?: string;
|
|
22
28
|
};
|
|
23
29
|
twitter?: {
|
|
24
30
|
card?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-seo",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.1",
|
|
4
4
|
"description": "Comprehensive SEO solution for React Vite applications with structured data, performance hints, and build-time optimization",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"lint": "eslint .",
|
|
12
12
|
"typecheck": "tsc --noEmit",
|
|
13
13
|
"prepublishOnly": "npm run build",
|
|
14
|
-
"test": "vitest run"
|
|
14
|
+
"test": "vitest run",
|
|
15
|
+
"version:minor": "npm version minor -m 'chore: release v%s'"
|
|
15
16
|
},
|
|
16
17
|
"exports": {
|
|
17
18
|
".": {
|