@umituz/web-seo 2.0.21 → 2.1.0
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.d.ts +7 -6
- package/dist/domains/seo/components/JsonLd.js +26 -77
- package/dist/domains/seo/components/SEO.d.ts +7 -0
- package/dist/domains/seo/components/SEO.js +10 -33
- package/dist/domains/seo/core/constants.d.ts +15 -0
- package/dist/domains/seo/core/constants.js +15 -0
- package/dist/domains/seo/core/deep-equal.d.ts +8 -0
- package/dist/domains/seo/core/deep-equal.js +28 -0
- package/dist/domains/seo/core/dom-queue.d.ts +10 -0
- package/dist/domains/seo/core/dom-queue.js +37 -0
- package/dist/domains/seo/core/escape.d.ts +12 -0
- package/dist/domains/seo/core/escape.js +41 -0
- package/dist/domains/seo/core/hash.d.ts +9 -0
- package/dist/domains/seo/core/hash.js +29 -0
- package/dist/domains/seo/core/init.d.ts +15 -2
- package/dist/domains/seo/core/init.js +38 -18
- package/dist/domains/seo/core/seo-strategies.d.ts +29 -0
- package/dist/domains/seo/core/seo-strategies.js +694 -0
- package/dist/domains/seo/core/seo-strategy.d.ts +37 -0
- package/dist/domains/seo/core/seo-strategy.js +14 -0
- package/dist/domains/seo/hooks/useJsonLd.d.ts +6 -7
- package/dist/domains/seo/hooks/useJsonLd.js +27 -61
- package/dist/domains/seo/hooks/useSEO.d.ts +6 -0
- package/dist/domains/seo/hooks/useSEO.js +7 -29
- package/dist/domains/seo/ssr/helmet.d.ts +25 -0
- package/dist/domains/seo/ssr/helmet.js +23 -0
- package/dist/domains/seo/ssr/index.d.ts +10 -0
- package/dist/domains/seo/ssr/index.js +8 -0
- package/dist/domains/seo/ssr/renderer.d.ts +11 -36
- package/dist/domains/seo/ssr/renderer.js +51 -285
- package/dist/domains/seo/ssr/serializer.d.ts +5 -60
- package/dist/domains/seo/ssr/serializer.js +26 -166
- package/dist/domains/seo/types/index.d.ts +0 -4
- package/dist/domains/seo/types/schema.d.ts +22 -1
- package/dist/domains/seo/utils/dom/dom-context.d.ts +13 -0
- package/dist/domains/seo/utils/dom/dom-context.js +62 -0
- package/dist/domains/seo/utils/dom/dom-mutator.d.ts +31 -0
- package/dist/domains/seo/utils/dom/dom-mutator.js +89 -0
- package/dist/domains/seo/utils/dom/index.d.ts +1 -0
- package/dist/domains/seo/utils/dom/index.js +1 -0
- package/dist/domains/seo/utils/index.d.ts +3 -4
- package/dist/domains/seo/utils/index.js +3 -5
- package/dist/domains/seo/utils/schema-generators/base.d.ts +25 -0
- package/dist/domains/seo/utils/schema-generators/base.js +50 -0
- package/dist/domains/seo/utils/schema-generators/factory.d.ts +27 -0
- package/dist/domains/seo/utils/schema-generators/factory.js +49 -0
- package/dist/domains/seo/utils/schema-generators/generators/article.d.ts +4 -0
- package/dist/domains/seo/utils/schema-generators/generators/article.js +39 -0
- package/dist/domains/seo/utils/schema-generators/generators/breadcrumb.d.ts +6 -0
- package/dist/domains/seo/utils/schema-generators/generators/breadcrumb.js +18 -0
- package/dist/domains/seo/utils/schema-generators/generators/course.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/course.js +70 -0
- package/dist/domains/seo/utils/schema-generators/generators/faq.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/faq.js +21 -0
- package/dist/domains/seo/utils/schema-generators/generators/local-business.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/local-business.js +45 -0
- package/dist/domains/seo/utils/schema-generators/generators/organization.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/organization.js +25 -0
- package/dist/domains/seo/utils/schema-generators/generators/person.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/person.js +27 -0
- package/dist/domains/seo/utils/schema-generators/generators/product.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/product.js +40 -0
- package/dist/domains/seo/utils/schema-generators/generators/video.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/video.js +23 -0
- package/dist/domains/seo/utils/schema-generators/generators/website.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/website.js +30 -0
- package/dist/domains/seo/utils/schema-generators/index.d.ts +15 -2
- package/dist/domains/seo/utils/schema-generators/index.js +13 -2
- package/dist/domains/seo/utils/schema-generators/types.d.ts +142 -0
- package/dist/domains/seo/utils/schema-generators/types.js +5 -0
- package/dist/domains/seo/utils/validation/hreflang-validator.d.ts +7 -0
- package/dist/domains/seo/utils/validation/hreflang-validator.js +13 -0
- package/dist/domains/seo/utils/validation/image-validator.d.ts +7 -0
- package/dist/domains/seo/utils/validation/image-validator.js +24 -0
- package/dist/domains/seo/utils/validation/index.d.ts +10 -2
- package/dist/domains/seo/utils/validation/index.js +10 -2
- package/dist/domains/seo/utils/validation/jsonld-validator.d.ts +9 -0
- package/dist/domains/seo/utils/validation/jsonld-validator.js +74 -0
- package/dist/domains/seo/utils/validation/language-code.d.ts +7 -0
- package/dist/domains/seo/utils/validation/language-code.js +8 -0
- package/dist/domains/seo/utils/validation/length-validator.d.ts +10 -0
- package/dist/domains/seo/utils/validation/length-validator.js +40 -0
- package/dist/domains/seo/utils/validation/seo-auditor.d.ts +17 -0
- package/dist/domains/seo/utils/validation/seo-auditor.js +144 -0
- package/dist/domains/seo/utils/validation/text-helpers.d.ts +7 -0
- package/dist/domains/seo/utils/validation/text-helpers.js +12 -0
- package/dist/domains/seo/utils/validation/url-validator.d.ts +8 -0
- package/dist/domains/seo/utils/validation/url-validator.js +26 -0
- package/dist/domains/seo/vite/index.d.ts +6 -4
- package/dist/domains/seo/vite/index.js +5 -3
- package/dist/domains/seo/vite/plugin.d.ts +3 -55
- package/dist/domains/seo/vite/plugin.js +28 -186
- package/dist/domains/seo/vite/presets.d.ts +14 -0
- package/dist/domains/seo/vite/presets.js +33 -0
- package/dist/domains/seo/vite/robots-generator.d.ts +9 -0
- package/dist/domains/seo/vite/robots-generator.js +36 -0
- package/dist/domains/seo/vite/sitemap-generator.d.ts +15 -0
- package/dist/domains/seo/vite/sitemap-generator.js +56 -0
- package/dist/domains/seo/vite/types.d.ts +29 -0
- package/dist/domains/seo/vite/types.js +4 -0
- package/dist/index.d.ts +8 -7
- package/dist/index.js +7 -6
- package/package.json +1 -1
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comprehensive SEO validation and audit.
|
|
3
|
+
*
|
|
4
|
+
* `validateSEO` returns a structured result; `auditSEO` adds a dev-friendly
|
|
5
|
+
* console summary. Neither mutates inputs.
|
|
6
|
+
*/
|
|
7
|
+
import { validateHreflangEntry } from "./hreflang-validator.js";
|
|
8
|
+
import { validateOpenGraphImage } from "./image-validator.js";
|
|
9
|
+
import { validateDescriptionLength, validateTitleLength } from "./length-validator.js";
|
|
10
|
+
import { validateJsonLd } from "./jsonld-validator.js";
|
|
11
|
+
import { validateUrl } from "./url-validator.js";
|
|
12
|
+
export const validateSEO = (options) => {
|
|
13
|
+
const errors = [];
|
|
14
|
+
const warnings = [];
|
|
15
|
+
const suggestions = [];
|
|
16
|
+
// Title
|
|
17
|
+
if (options.title) {
|
|
18
|
+
const error = validateTitleLength(options.title);
|
|
19
|
+
if (error)
|
|
20
|
+
warnings.push(error);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
errors.push("Title is missing");
|
|
24
|
+
}
|
|
25
|
+
// Description
|
|
26
|
+
if (options.description) {
|
|
27
|
+
const error = validateDescriptionLength(options.description);
|
|
28
|
+
if (error)
|
|
29
|
+
warnings.push(error);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
warnings.push("Description is missing");
|
|
33
|
+
}
|
|
34
|
+
// Canonical
|
|
35
|
+
if (options.canonical) {
|
|
36
|
+
const error = validateUrl(options.canonical, "Canonical URL");
|
|
37
|
+
if (error)
|
|
38
|
+
errors.push(error);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
suggestions.push("Add a canonical URL to prevent duplicate content issues");
|
|
42
|
+
}
|
|
43
|
+
// Language
|
|
44
|
+
if (!options.lang) {
|
|
45
|
+
warnings.push("Language attribute is missing");
|
|
46
|
+
suggestions.push('Add lang attribute (e.g., <html lang="en">)');
|
|
47
|
+
}
|
|
48
|
+
// Open Graph
|
|
49
|
+
if (!options.openGraph) {
|
|
50
|
+
suggestions.push("Add Open Graph tags for better social media sharing");
|
|
51
|
+
}
|
|
52
|
+
else if (!options.openGraph.image) {
|
|
53
|
+
suggestions.push("Add Open Graph image for better social media previews");
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const imageUrl = typeof options.openGraph.image === "string"
|
|
57
|
+
? options.openGraph.image
|
|
58
|
+
: options.openGraph.image[0]?.url ?? "";
|
|
59
|
+
const imageError = validateOpenGraphImage(imageUrl);
|
|
60
|
+
if (imageError)
|
|
61
|
+
warnings.push(imageError);
|
|
62
|
+
}
|
|
63
|
+
// Twitter
|
|
64
|
+
if (!options.twitter) {
|
|
65
|
+
suggestions.push("Add Twitter Card tags for better Twitter sharing");
|
|
66
|
+
}
|
|
67
|
+
// Hreflang
|
|
68
|
+
if (options.hreflang && options.hreflang.length > 0) {
|
|
69
|
+
options.hreflang.forEach((entry, index) => {
|
|
70
|
+
const error = validateHreflangEntry(entry);
|
|
71
|
+
if (error)
|
|
72
|
+
errors.push(`Hreflang entry ${index + 1}: ${error}`);
|
|
73
|
+
});
|
|
74
|
+
if (!options.hreflang.some((entry) => entry.lang === "x-default")) {
|
|
75
|
+
suggestions.push('Add hreflang="x-default" for language fallback');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// JSON-LD
|
|
79
|
+
if (options.jsonLd && options.jsonLd.length > 0) {
|
|
80
|
+
options.jsonLd.forEach((data, index) => {
|
|
81
|
+
validateJsonLd(data).forEach((error) => {
|
|
82
|
+
errors.push(`JSON-LD entry ${index + 1}: ${error}`);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
suggestions.push("Add JSON-LD structured data for rich snippets in search results");
|
|
88
|
+
}
|
|
89
|
+
// Favicon + theme color
|
|
90
|
+
if (!options.favicon) {
|
|
91
|
+
suggestions.push("Add a favicon for better brand recognition in browser tabs");
|
|
92
|
+
}
|
|
93
|
+
if (!options.themeColor) {
|
|
94
|
+
suggestions.push("Add a theme color for better mobile browser integration");
|
|
95
|
+
}
|
|
96
|
+
// Performance
|
|
97
|
+
if (!options.preconnect || options.preconnect.length === 0) {
|
|
98
|
+
suggestions.push("Add preconnect hints for external resources to improve performance");
|
|
99
|
+
}
|
|
100
|
+
return { valid: errors.length === 0, errors, warnings, suggestions };
|
|
101
|
+
};
|
|
102
|
+
const STYLE = {
|
|
103
|
+
error: "color: #d32f2f; font-weight: bold;",
|
|
104
|
+
warning: "color: #f57c00; font-weight: bold;",
|
|
105
|
+
success: "color: #388e3c; font-weight: bold;",
|
|
106
|
+
info: "color: #1976d2; font-weight: bold;",
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Validate SEO options and print a human-readable report to the console.
|
|
110
|
+
* Opt-in: pass `enableConsole: true` to actually log.
|
|
111
|
+
*
|
|
112
|
+
* Browser-safe: does not read `process.env` (which is undefined in some
|
|
113
|
+
* browser bundles). Use Vite's `import.meta.env.DEV` or a build-time
|
|
114
|
+
* constant if you want dev-only output.
|
|
115
|
+
*/
|
|
116
|
+
export const auditSEO = (options, enableConsole = true) => {
|
|
117
|
+
const result = validateSEO(options);
|
|
118
|
+
if (!enableConsole || typeof console === "undefined")
|
|
119
|
+
return result;
|
|
120
|
+
console.group("%cSEO Audit Results", STYLE.info);
|
|
121
|
+
if (result.valid) {
|
|
122
|
+
console.log("%cAll required SEO elements are present.", STYLE.success);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
console.log("%cSEO validation failed", STYLE.error);
|
|
126
|
+
}
|
|
127
|
+
if (result.errors.length > 0) {
|
|
128
|
+
console.group("%cErrors (must fix)", STYLE.error);
|
|
129
|
+
result.errors.forEach((error) => console.error(error));
|
|
130
|
+
console.groupEnd();
|
|
131
|
+
}
|
|
132
|
+
if (result.warnings.length > 0) {
|
|
133
|
+
console.group("%cWarnings (should fix)", STYLE.warning);
|
|
134
|
+
result.warnings.forEach((warning) => console.warn(warning));
|
|
135
|
+
console.groupEnd();
|
|
136
|
+
}
|
|
137
|
+
if (result.suggestions.length > 0) {
|
|
138
|
+
console.group("%cSuggestions (nice to have)", STYLE.info);
|
|
139
|
+
result.suggestions.forEach((suggestion) => console.log(suggestion));
|
|
140
|
+
console.groupEnd();
|
|
141
|
+
}
|
|
142
|
+
console.groupEnd();
|
|
143
|
+
return result;
|
|
144
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text helpers used by SEO concerns.
|
|
3
|
+
*
|
|
4
|
+
* Slug generation lives here because it's a generic string utility
|
|
5
|
+
* that the package's callers often reuse.
|
|
6
|
+
*/
|
|
7
|
+
export const generateSlug = (text) => text
|
|
8
|
+
.toLowerCase()
|
|
9
|
+
.trim()
|
|
10
|
+
.replace(/[^\w\s-]/g, "")
|
|
11
|
+
.replace(/[\s_-]+/g, "-")
|
|
12
|
+
.replace(/^-+|-+$/g, "");
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL validation.
|
|
3
|
+
*
|
|
4
|
+
* Exposed at module scope so individual checks can be reused (e.g. the
|
|
5
|
+
* image validator and the hreflang validator both need `isValidUrl`).
|
|
6
|
+
*/
|
|
7
|
+
export declare const isValidUrl: (url: string) => boolean;
|
|
8
|
+
export declare const validateUrl: (url: string, fieldName?: string) => string | null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL validation.
|
|
3
|
+
*
|
|
4
|
+
* Exposed at module scope so individual checks can be reused (e.g. the
|
|
5
|
+
* image validator and the hreflang validator both need `isValidUrl`).
|
|
6
|
+
*/
|
|
7
|
+
export const isValidUrl = (url) => {
|
|
8
|
+
if (!url)
|
|
9
|
+
return false;
|
|
10
|
+
try {
|
|
11
|
+
const parsed = new URL(url);
|
|
12
|
+
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
export const validateUrl = (url, fieldName = "URL") => {
|
|
19
|
+
if (!url) {
|
|
20
|
+
return `${fieldName} is required`;
|
|
21
|
+
}
|
|
22
|
+
if (!isValidUrl(url)) {
|
|
23
|
+
return `${fieldName} must be a valid HTTP/HTTPS URL`;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Vite plugin entry point.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Lives behind a subpath import (`@umituz/web-seo/vite`) to keep Node-only
|
|
5
|
+
* dependencies out of the main browser bundle.
|
|
5
6
|
*/
|
|
6
|
-
export { viteSEOPlugin
|
|
7
|
-
export
|
|
7
|
+
export { viteSEOPlugin } from "./plugin.js";
|
|
8
|
+
export { quickSEOSetup } from "./presets.js";
|
|
9
|
+
export type { ViteSEOPluginOptions } from "./types.js";
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Vite plugin entry point.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Lives behind a subpath import (`@umituz/web-seo/vite`) to keep Node-only
|
|
5
|
+
* dependencies out of the main browser bundle.
|
|
5
6
|
*/
|
|
6
|
-
export { viteSEOPlugin
|
|
7
|
+
export { viteSEOPlugin } from "./plugin.js";
|
|
8
|
+
export { quickSEOSetup } from "./presets.js";
|
|
@@ -1,62 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Vite SEO Plugin
|
|
3
3
|
*
|
|
4
|
-
* Build-time SEO
|
|
5
|
-
*
|
|
4
|
+
* Build-time SEO asset generator. Emits `sitemap.xml` and `robots.txt`
|
|
5
|
+
* as Vite bundle assets so they land in the build output automatically.
|
|
6
6
|
*/
|
|
7
7
|
import type { Plugin } from "vite";
|
|
8
|
-
|
|
9
|
-
/** Base URL of your site (e.g., https://example.com) */
|
|
10
|
-
siteUrl: string;
|
|
11
|
-
/** Array of routes to include in sitemap */
|
|
12
|
-
routes?: string[];
|
|
13
|
-
/** Automatically discover routes from build output */
|
|
14
|
-
discoverRoutes?: boolean;
|
|
15
|
-
/** Sitemap options */
|
|
16
|
-
sitemap?: {
|
|
17
|
-
/** Output filename */
|
|
18
|
-
filename?: string;
|
|
19
|
-
/** Changefreq for all URLs */
|
|
20
|
-
changefreq?: "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
|
|
21
|
-
/** Priority for all URLs (0.0-1.0) */
|
|
22
|
-
priority?: number;
|
|
23
|
-
/** Last modified date */
|
|
24
|
-
lastmod?: string;
|
|
25
|
-
};
|
|
26
|
-
/** Robots.txt options */
|
|
27
|
-
robotsTxt?: {
|
|
28
|
-
/** Output filename */
|
|
29
|
-
filename?: string;
|
|
30
|
-
/** User-agent rules */
|
|
31
|
-
userAgent?: string | Record<string, string[]>;
|
|
32
|
-
/** Disallow rules */
|
|
33
|
-
disallow?: string | string[];
|
|
34
|
-
/** Allow rules */
|
|
35
|
-
allow?: string | string[];
|
|
36
|
-
/** Sitemap location */
|
|
37
|
-
sitemap?: string;
|
|
38
|
-
/** Crawl delay */
|
|
39
|
-
crawlDelay?: number;
|
|
40
|
-
};
|
|
41
|
-
/** Suppress console output */
|
|
42
|
-
silent?: boolean;
|
|
43
|
-
}
|
|
8
|
+
import type { ViteSEOPluginOptions } from "./types.js";
|
|
44
9
|
export declare const viteSEOPlugin: (options: ViteSEOPluginOptions) => Plugin;
|
|
45
10
|
export default viteSEOPlugin;
|
|
46
|
-
/**
|
|
47
|
-
* Quick setup for common SEO scenarios
|
|
48
|
-
*/
|
|
49
|
-
export declare const quickSEOSetup: {
|
|
50
|
-
/**
|
|
51
|
-
* Basic setup for a simple website
|
|
52
|
-
*/
|
|
53
|
-
basic: (siteUrl: string, routes?: string[]) => Plugin<any>;
|
|
54
|
-
/**
|
|
55
|
-
* Setup for a blog
|
|
56
|
-
*/
|
|
57
|
-
blog: (siteUrl: string, routes?: string[]) => Plugin<any>;
|
|
58
|
-
/**
|
|
59
|
-
* Setup for an e-commerce site
|
|
60
|
-
*/
|
|
61
|
-
ecommerce: (siteUrl: string, routes?: string[]) => Plugin<any>;
|
|
62
|
-
};
|
|
@@ -1,222 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Vite SEO Plugin
|
|
3
3
|
*
|
|
4
|
-
* Build-time SEO
|
|
5
|
-
*
|
|
4
|
+
* Build-time SEO asset generator. Emits `sitemap.xml` and `robots.txt`
|
|
5
|
+
* as Vite bundle assets so they land in the build output automatically.
|
|
6
6
|
*/
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
const
|
|
10
|
-
const urlElements = urls
|
|
11
|
-
.map((url) => ` <url>
|
|
12
|
-
<loc>${escapeXML(url.loc)}</loc>
|
|
13
|
-
${url.lastmod ? `<lastmod>${url.lastmod}</lastmod>` : ""}
|
|
14
|
-
${url.changefreq ? `<changefreq>${url.changefreq}</changefreq>` : ""}
|
|
15
|
-
${url.priority !== undefined ? `<priority>${url.priority}</priority>` : ""}
|
|
16
|
-
</url>`)
|
|
17
|
-
.join("\n");
|
|
18
|
-
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
19
|
-
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
20
|
-
${urlElements}
|
|
21
|
-
</urlset>`;
|
|
22
|
-
};
|
|
23
|
-
const escapeXML = (str) => {
|
|
24
|
-
return str
|
|
25
|
-
.replace(/&/g, "&")
|
|
26
|
-
.replace(/</g, "<")
|
|
27
|
-
.replace(/>/g, ">")
|
|
28
|
-
.replace(/"/g, """)
|
|
29
|
-
.replace(/'/g, "'");
|
|
30
|
-
};
|
|
31
|
-
// ============================================================================
|
|
32
|
-
// Robots.txt Generation
|
|
33
|
-
// ============================================================================
|
|
34
|
-
const generateRobotsTxt = (options) => {
|
|
35
|
-
const lines = [];
|
|
36
|
-
if (!options)
|
|
37
|
-
return "";
|
|
38
|
-
// User-agent
|
|
39
|
-
if (typeof options.userAgent === "string") {
|
|
40
|
-
lines.push(`User-agent: ${options.userAgent}`);
|
|
41
|
-
}
|
|
42
|
-
else if (typeof options.userAgent === "object") {
|
|
43
|
-
Object.entries(options.userAgent).forEach(([agent, rules]) => {
|
|
44
|
-
lines.push(`User-agent: ${agent}`);
|
|
45
|
-
rules.forEach((rule) => lines.push(rule));
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
lines.push("User-agent: *");
|
|
50
|
-
}
|
|
51
|
-
// Disallow
|
|
52
|
-
if (options.disallow) {
|
|
53
|
-
const disallowRules = Array.isArray(options.disallow) ? options.disallow : [options.disallow];
|
|
54
|
-
disallowRules.forEach((rule) => lines.push(`Disallow: ${rule}`));
|
|
55
|
-
}
|
|
56
|
-
// Allow
|
|
57
|
-
if (options.allow) {
|
|
58
|
-
const allowRules = Array.isArray(options.allow) ? options.allow : [options.allow];
|
|
59
|
-
allowRules.forEach((rule) => lines.push(`Allow: ${rule}`));
|
|
60
|
-
}
|
|
61
|
-
// Crawl delay
|
|
62
|
-
if (options.crawlDelay) {
|
|
63
|
-
lines.push(`Crawl-delay: ${options.crawlDelay}`);
|
|
64
|
-
}
|
|
65
|
-
// Sitemap
|
|
66
|
-
if (options.sitemap) {
|
|
67
|
-
lines.push(`Sitemap: ${options.sitemap}`);
|
|
68
|
-
}
|
|
69
|
-
return lines.join("\n") + "\n";
|
|
70
|
-
};
|
|
71
|
-
// ============================================================================
|
|
72
|
-
// Route Discovery
|
|
73
|
-
// ============================================================================
|
|
74
|
-
const discoverRoutesFromBuild = (outDir) => {
|
|
75
|
-
const routes = new Set("/");
|
|
76
|
-
const traverseDirectory = (dir, basePath = "") => {
|
|
77
|
-
try {
|
|
78
|
-
const files = fs.readdirSync(dir);
|
|
79
|
-
files.forEach((file) => {
|
|
80
|
-
const fullPath = path.join(dir, file);
|
|
81
|
-
const stat = fs.statSync(fullPath);
|
|
82
|
-
if (stat.isDirectory()) {
|
|
83
|
-
traverseDirectory(fullPath, path.join(basePath, file));
|
|
84
|
-
}
|
|
85
|
-
else if (file === "index.html") {
|
|
86
|
-
routes.add(basePath || "/");
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
catch (error) {
|
|
91
|
-
// Directory doesn't exist or isn't accessible yet - silently skip
|
|
92
|
-
if (error.code !== 'ENOENT') {
|
|
93
|
-
console.warn(`[vite-plugin-seo] Warning: Could not traverse directory ${dir}:`, error);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
traverseDirectory(outDir);
|
|
98
|
-
return Array.from(routes);
|
|
99
|
-
};
|
|
100
|
-
// ============================================================================
|
|
101
|
-
// Main Plugin
|
|
102
|
-
// ============================================================================
|
|
7
|
+
import { generateRobotsTxt } from "./robots-generator.js";
|
|
8
|
+
import { discoverRoutesFromBuild, generateSitemapXML } from "./sitemap-generator.js";
|
|
9
|
+
const PLUGIN_NAME = "umituz-web-seo";
|
|
103
10
|
export const viteSEOPlugin = (options) => {
|
|
104
11
|
const { siteUrl, routes: manualRoutes = [], discoverRoutes = false, sitemap: sitemapOptions = {}, robotsTxt: robotsOptions = {}, silent = false, } = options;
|
|
105
|
-
const sitemapFilename = sitemapOptions.filename
|
|
106
|
-
const robotsFilename = robotsOptions.filename
|
|
107
|
-
|
|
12
|
+
const sitemapFilename = sitemapOptions.filename ?? "sitemap.xml";
|
|
13
|
+
const robotsFilename = robotsOptions.filename ?? "robots.txt";
|
|
14
|
+
const normalizedSiteUrl = siteUrl.replace(/\/$/, "");
|
|
108
15
|
const pluginState = {};
|
|
109
16
|
return {
|
|
110
|
-
name:
|
|
17
|
+
name: PLUGIN_NAME,
|
|
111
18
|
enforce: "post",
|
|
112
19
|
configResolved(config) {
|
|
113
|
-
// Store resolved config for later use
|
|
114
20
|
pluginState.publicDir = config.publicDir;
|
|
115
21
|
pluginState.outDir = config.build.outDir;
|
|
116
22
|
},
|
|
117
|
-
generateBundle(
|
|
118
|
-
const outDir = pluginState.outDir
|
|
119
|
-
// Collect routes
|
|
23
|
+
generateBundle() {
|
|
24
|
+
const outDir = pluginState.outDir ?? "dist";
|
|
120
25
|
let routes = [...manualRoutes];
|
|
121
26
|
if (discoverRoutes) {
|
|
122
|
-
const
|
|
123
|
-
routes =
|
|
27
|
+
const discovered = discoverRoutesFromBuild(outDir);
|
|
28
|
+
routes = Array.from(new Set([...routes, ...discovered]));
|
|
124
29
|
}
|
|
125
|
-
|
|
126
|
-
if (routes.length === 0) {
|
|
30
|
+
if (routes.length === 0)
|
|
127
31
|
routes.push("/");
|
|
128
|
-
}
|
|
129
|
-
// Generate sitemap
|
|
130
32
|
const sitemapUrls = routes.map((route) => ({
|
|
131
|
-
loc: `${
|
|
33
|
+
loc: `${normalizedSiteUrl}${route}`,
|
|
132
34
|
lastmod: sitemapOptions.lastmod,
|
|
133
35
|
changefreq: sitemapOptions.changefreq,
|
|
134
36
|
priority: sitemapOptions.priority,
|
|
135
37
|
}));
|
|
136
|
-
const sitemapXML = generateSitemapXML(sitemapUrls);
|
|
137
|
-
// Add sitemap to bundle
|
|
138
38
|
this.emitFile({
|
|
139
39
|
type: "asset",
|
|
140
40
|
fileName: sitemapFilename,
|
|
141
|
-
source:
|
|
41
|
+
source: generateSitemapXML(sitemapUrls),
|
|
142
42
|
});
|
|
143
|
-
// Generate robots.txt
|
|
144
|
-
const robotsTxtContent = generateRobotsTxt({
|
|
145
|
-
siteUrl,
|
|
146
|
-
...robotsOptions,
|
|
147
|
-
sitemap: robotsOptions.sitemap || `${siteUrl.replace(/\/$/, "")}/${sitemapFilename}`,
|
|
148
|
-
});
|
|
149
|
-
// Add robots.txt to bundle
|
|
150
43
|
this.emitFile({
|
|
151
44
|
type: "asset",
|
|
152
45
|
fileName: robotsFilename,
|
|
153
|
-
source:
|
|
46
|
+
source: generateRobotsTxt({
|
|
47
|
+
siteUrl,
|
|
48
|
+
...robotsOptions,
|
|
49
|
+
sitemap: robotsOptions.sitemap ?? `${normalizedSiteUrl}/${sitemapFilename}`,
|
|
50
|
+
}),
|
|
154
51
|
});
|
|
155
52
|
},
|
|
156
53
|
closeBundle() {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
54
|
+
if (silent)
|
|
55
|
+
return;
|
|
56
|
+
console.log(`\n${"=".repeat(50)}`);
|
|
57
|
+
console.log("SEO files generated:");
|
|
58
|
+
console.log(` ${sitemapFilename}`);
|
|
59
|
+
console.log(` ${robotsFilename}`);
|
|
60
|
+
console.log(`${"=".repeat(50)}\n`);
|
|
165
61
|
},
|
|
166
62
|
};
|
|
167
63
|
};
|
|
168
64
|
export default viteSEOPlugin;
|
|
169
|
-
// ============================================================================
|
|
170
|
-
// Convenience Exports
|
|
171
|
-
// ============================================================================
|
|
172
|
-
/**
|
|
173
|
-
* Quick setup for common SEO scenarios
|
|
174
|
-
*/
|
|
175
|
-
export const quickSEOSetup = {
|
|
176
|
-
/**
|
|
177
|
-
* Basic setup for a simple website
|
|
178
|
-
*/
|
|
179
|
-
basic: (siteUrl, routes) => viteSEOPlugin({
|
|
180
|
-
siteUrl,
|
|
181
|
-
routes,
|
|
182
|
-
discoverRoutes: true,
|
|
183
|
-
sitemap: {
|
|
184
|
-
changefreq: "weekly",
|
|
185
|
-
priority: 0.8,
|
|
186
|
-
},
|
|
187
|
-
robotsTxt: {
|
|
188
|
-
disallow: ["/api", "/admin"],
|
|
189
|
-
},
|
|
190
|
-
}),
|
|
191
|
-
/**
|
|
192
|
-
* Setup for a blog
|
|
193
|
-
*/
|
|
194
|
-
blog: (siteUrl, routes) => viteSEOPlugin({
|
|
195
|
-
siteUrl,
|
|
196
|
-
routes: ["/", routes || []].flat(),
|
|
197
|
-
discoverRoutes: true,
|
|
198
|
-
sitemap: {
|
|
199
|
-
changefreq: "daily",
|
|
200
|
-
priority: 0.9,
|
|
201
|
-
},
|
|
202
|
-
robotsTxt: {
|
|
203
|
-
disallow: ["/api", "/admin", "/private"],
|
|
204
|
-
},
|
|
205
|
-
}),
|
|
206
|
-
/**
|
|
207
|
-
* Setup for an e-commerce site
|
|
208
|
-
*/
|
|
209
|
-
ecommerce: (siteUrl, routes) => viteSEOPlugin({
|
|
210
|
-
siteUrl,
|
|
211
|
-
routes: ["/", routes || []].flat(),
|
|
212
|
-
discoverRoutes: true,
|
|
213
|
-
sitemap: {
|
|
214
|
-
changefreq: "hourly",
|
|
215
|
-
priority: 1.0,
|
|
216
|
-
},
|
|
217
|
-
robotsTxt: {
|
|
218
|
-
disallow: ["/api", "/admin", "/cart", "/checkout", "/account"],
|
|
219
|
-
crawlDelay: 1,
|
|
220
|
-
},
|
|
221
|
-
}),
|
|
222
|
-
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenience presets for common SEO setups.
|
|
3
|
+
*
|
|
4
|
+
* Each preset returns a fully-configured Vite plugin instance.
|
|
5
|
+
*/
|
|
6
|
+
import type { ViteSEOPluginOptions } from "./types.js";
|
|
7
|
+
export declare const quickSEOSetup: {
|
|
8
|
+
readonly basic: (siteUrl: string, routes?: string[]) => import("vite").Plugin<any>;
|
|
9
|
+
readonly blog: (siteUrl: string, routes?: string[]) => import("vite").Plugin<any>;
|
|
10
|
+
readonly ecommerce: (siteUrl: string, routes?: string[]) => import("vite").Plugin<any>;
|
|
11
|
+
};
|
|
12
|
+
export type QuickSetupKey = keyof typeof quickSEOSetup;
|
|
13
|
+
/** Re-export so plugin.ts can satisfy Vite's plugin shape without a circular import. */
|
|
14
|
+
export type { ViteSEOPluginOptions };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenience presets for common SEO setups.
|
|
3
|
+
*
|
|
4
|
+
* Each preset returns a fully-configured Vite plugin instance.
|
|
5
|
+
*/
|
|
6
|
+
import { viteSEOPlugin } from "./plugin.js";
|
|
7
|
+
const baseDisallow = ["/api", "/admin"];
|
|
8
|
+
const blogDisallow = [...baseDisallow, "/private"];
|
|
9
|
+
const ecommerceDisallow = [...baseDisallow, "/cart", "/checkout", "/account"];
|
|
10
|
+
const withRoutes = (routes) => ["/", ...(routes ?? [])];
|
|
11
|
+
export const quickSEOSetup = {
|
|
12
|
+
basic: (siteUrl, routes) => viteSEOPlugin({
|
|
13
|
+
siteUrl,
|
|
14
|
+
routes,
|
|
15
|
+
discoverRoutes: true,
|
|
16
|
+
sitemap: { changefreq: "weekly", priority: 0.8 },
|
|
17
|
+
robotsTxt: { disallow: baseDisallow },
|
|
18
|
+
}),
|
|
19
|
+
blog: (siteUrl, routes) => viteSEOPlugin({
|
|
20
|
+
siteUrl,
|
|
21
|
+
routes: withRoutes(routes),
|
|
22
|
+
discoverRoutes: true,
|
|
23
|
+
sitemap: { changefreq: "daily", priority: 0.9 },
|
|
24
|
+
robotsTxt: { disallow: blogDisallow },
|
|
25
|
+
}),
|
|
26
|
+
ecommerce: (siteUrl, routes) => viteSEOPlugin({
|
|
27
|
+
siteUrl,
|
|
28
|
+
routes: withRoutes(routes),
|
|
29
|
+
discoverRoutes: true,
|
|
30
|
+
sitemap: { changefreq: "hourly", priority: 1.0 },
|
|
31
|
+
robotsTxt: { disallow: ecommerceDisallow, crawlDelay: 1 },
|
|
32
|
+
}),
|
|
33
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* robots.txt generation.
|
|
3
|
+
*/
|
|
4
|
+
import type { ViteSEOPluginOptions } from "./types.js";
|
|
5
|
+
type RobotsOptions = ViteSEOPluginOptions["robotsTxt"] & {
|
|
6
|
+
siteUrl: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const generateRobotsTxt: (options: RobotsOptions) => string;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* robots.txt generation.
|
|
3
|
+
*/
|
|
4
|
+
export const generateRobotsTxt = (options) => {
|
|
5
|
+
if (!options)
|
|
6
|
+
return "";
|
|
7
|
+
const lines = [];
|
|
8
|
+
if (typeof options.userAgent === "string") {
|
|
9
|
+
lines.push(`User-agent: ${options.userAgent}`);
|
|
10
|
+
}
|
|
11
|
+
else if (typeof options.userAgent === "object" && options.userAgent !== null) {
|
|
12
|
+
for (const [agent, rules] of Object.entries(options.userAgent)) {
|
|
13
|
+
lines.push(`User-agent: ${agent}`);
|
|
14
|
+
for (const rule of rules)
|
|
15
|
+
lines.push(rule);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
lines.push("User-agent: *");
|
|
20
|
+
}
|
|
21
|
+
if (options.disallow) {
|
|
22
|
+
const rules = Array.isArray(options.disallow) ? options.disallow : [options.disallow];
|
|
23
|
+
for (const rule of rules)
|
|
24
|
+
lines.push(`Disallow: ${rule}`);
|
|
25
|
+
}
|
|
26
|
+
if (options.allow) {
|
|
27
|
+
const rules = Array.isArray(options.allow) ? options.allow : [options.allow];
|
|
28
|
+
for (const rule of rules)
|
|
29
|
+
lines.push(`Allow: ${rule}`);
|
|
30
|
+
}
|
|
31
|
+
if (options.crawlDelay)
|
|
32
|
+
lines.push(`Crawl-delay: ${options.crawlDelay}`);
|
|
33
|
+
if (options.sitemap)
|
|
34
|
+
lines.push(`Sitemap: ${options.sitemap}`);
|
|
35
|
+
return lines.join("\n") + "\n";
|
|
36
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sitemap XML generation.
|
|
3
|
+
*/
|
|
4
|
+
export interface SitemapURL {
|
|
5
|
+
loc: string;
|
|
6
|
+
lastmod?: string;
|
|
7
|
+
changefreq?: string;
|
|
8
|
+
priority?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const generateSitemapXML: (urls: SitemapURL[]) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Walk the build output and collect every directory that produced an
|
|
13
|
+
* `index.html`. Each one becomes a sitemap entry.
|
|
14
|
+
*/
|
|
15
|
+
export declare const discoverRoutesFromBuild: (outDir: string) => string[];
|