@eldrajs/theme-nuxt 0.2.0 → 0.3.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/CHANGELOG.md +88 -0
- package/dist/module.d.mts +48 -2
- package/dist/module.d.ts +48 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +188 -15
- package/dist/runtime/analytics.d.ts +83 -0
- package/dist/runtime/analytics.js +83 -0
- package/dist/runtime/analyticsPlugin.d.ts +15 -0
- package/dist/runtime/analyticsPlugin.js +21 -0
- package/dist/runtime/buildToken.d.ts +15 -0
- package/dist/runtime/buildToken.js +6 -0
- package/dist/runtime/catalog.d.ts +5 -0
- package/dist/runtime/catalog.js +6 -3
- package/dist/runtime/composables/useEldraAnalytics.d.ts +10 -0
- package/dist/runtime/composables/useEldraAnalytics.js +6 -0
- package/dist/runtime/composables/useEldraBuildToken.d.ts +9 -0
- package/dist/runtime/composables/useEldraBuildToken.js +6 -0
- package/dist/runtime/composables/useEldraForms.d.ts +8 -0
- package/dist/runtime/composables/useEldraForms.js +5 -0
- package/dist/runtime/composables/useEldraSeo.d.ts +27 -0
- package/dist/runtime/composables/useEldraSeo.js +41 -0
- package/dist/runtime/composables/useStructuredData.d.ts +35 -0
- package/dist/runtime/composables/useStructuredData.js +55 -0
- package/dist/runtime/crawl.d.ts +29 -0
- package/dist/runtime/crawl.js +50 -0
- package/dist/runtime/crawlOutput.d.ts +54 -0
- package/dist/runtime/crawlOutput.js +96 -0
- package/dist/runtime/forms.d.ts +29 -0
- package/dist/runtime/forms.js +154 -0
- package/dist/runtime/localeState.d.ts +6 -4
- package/dist/runtime/localeState.js +7 -15
- package/dist/runtime/plugin.js +9 -1
- package/dist/runtime/redirects.d.ts +74 -0
- package/dist/runtime/redirects.js +190 -0
- package/dist/runtime/seo.d.ts +23 -0
- package/dist/runtime/seo.js +95 -0
- package/dist/runtime/seoHead.d.ts +27 -0
- package/dist/runtime/seoHead.js +28 -0
- package/dist/runtime/siteProfile.d.ts +56 -0
- package/dist/runtime/siteProfile.js +69 -0
- package/package.json +6 -5
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { EldraClient, EldraFormDefinition } from '@eldrajs/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* The slice of an `@eldrajs/sdk` client the build-time form read needs: the typed `forms.get`. A
|
|
4
|
+
* whole `EldraClient` satisfies it.
|
|
5
|
+
*/
|
|
6
|
+
export interface FormDefinitionsReader {
|
|
7
|
+
forms: Pick<EldraClient['forms'], 'get'>;
|
|
8
|
+
}
|
|
9
|
+
/** The form definitions a build baked, by form key; a key that could not be read is absent. */
|
|
10
|
+
export type FormDefinitions = Record<string, EldraFormDefinition>;
|
|
11
|
+
/** The one line a build prints per form it could not read; the cause follows in parentheses. */
|
|
12
|
+
export declare const FORM_DEFINITION_WARNING: (key: string) => string;
|
|
13
|
+
/** How long the build waits for one form; see `READ_TIMEOUT_MS` in `./designTokens.ts`. */
|
|
14
|
+
export declare const READ_TIMEOUT_MS = 10000;
|
|
15
|
+
/**
|
|
16
|
+
* The definitions of `keys`, read in parallel at build time, **each independently fail-soft**: no
|
|
17
|
+
* client, an unreachable gateway, a `404` (the form is not seeded yet), a timeout and a response
|
|
18
|
+
* that is not the documented shape all leave that one key out and print **one** warning, so a
|
|
19
|
+
* theme keeps deploying and one form's failure never costs another its definition. A form left out
|
|
20
|
+
* is not a broken form: the storefront fetches it in the visitor's browser instead
|
|
21
|
+
* (`forms.definition` in the starter's gateway storefront).
|
|
22
|
+
*/
|
|
23
|
+
export declare function readFormDefinitions(reader: FormDefinitionsReader | null, keys: readonly string[], warn?: (message: string) => void): Promise<FormDefinitions>;
|
|
24
|
+
/**
|
|
25
|
+
* Accepts only a whole answer for `key`: half of an untrusted response never reaches a payload
|
|
26
|
+
* every visitor downloads. A definition whose own `key` differs from the one asked for is refused
|
|
27
|
+
* too, since the starter looks definitions up by the key a block names.
|
|
28
|
+
*/
|
|
29
|
+
export declare function toFormDefinition(value: unknown, key: string): EldraFormDefinition | null;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
export const FORM_DEFINITION_WARNING = (key) => `[eldra] could not read the platform\u2019s form \u201C${key}\u201D \u2014 the site fetches it when a visitor opens the page`;
|
|
2
|
+
export const READ_TIMEOUT_MS = 1e4;
|
|
3
|
+
const FIELD_TYPES = /* @__PURE__ */ new Set([
|
|
4
|
+
"text",
|
|
5
|
+
"email",
|
|
6
|
+
"phone",
|
|
7
|
+
"textarea",
|
|
8
|
+
"select",
|
|
9
|
+
"checkbox",
|
|
10
|
+
"consent",
|
|
11
|
+
"number",
|
|
12
|
+
"url"
|
|
13
|
+
]);
|
|
14
|
+
const ACTION_KINDS = /* @__PURE__ */ new Set(["store", "notify", "confirm", "subscribe"]);
|
|
15
|
+
export async function readFormDefinitions(reader, keys, warn = console.warn) {
|
|
16
|
+
const unique = [...new Set(keys)];
|
|
17
|
+
if (reader === null || unique.length === 0) {
|
|
18
|
+
if (reader === null) for (const key of unique) warn(FORM_DEFINITION_WARNING(key));
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
const read = await Promise.all(unique.map((key) => readOne(reader, key)));
|
|
22
|
+
const definitions = {};
|
|
23
|
+
for (const [index, result] of read.entries()) {
|
|
24
|
+
const key = unique[index];
|
|
25
|
+
if (result.definition === null) {
|
|
26
|
+
warn(
|
|
27
|
+
result.cause === null ? FORM_DEFINITION_WARNING(key) : `${FORM_DEFINITION_WARNING(key)} (${result.cause})`
|
|
28
|
+
);
|
|
29
|
+
} else definitions[key] = result.definition;
|
|
30
|
+
}
|
|
31
|
+
return definitions;
|
|
32
|
+
}
|
|
33
|
+
async function readOne(reader, key) {
|
|
34
|
+
const controller = new AbortController();
|
|
35
|
+
const timer = setTimeout(
|
|
36
|
+
() => controller.abort(new Error(`timed out after ${READ_TIMEOUT_MS}ms`)),
|
|
37
|
+
READ_TIMEOUT_MS
|
|
38
|
+
);
|
|
39
|
+
try {
|
|
40
|
+
const response = await reader.forms.get(key, { signal: controller.signal });
|
|
41
|
+
const definition = toFormDefinition(response, key);
|
|
42
|
+
return {
|
|
43
|
+
definition,
|
|
44
|
+
cause: definition === null ? "the platform published an unusable response" : null
|
|
45
|
+
};
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return { definition: null, cause: error instanceof Error ? error.message : String(error) };
|
|
48
|
+
} finally {
|
|
49
|
+
clearTimeout(timer);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function isRecord(value) {
|
|
53
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
54
|
+
}
|
|
55
|
+
function toLocalized(value) {
|
|
56
|
+
if (!isRecord(value)) return null;
|
|
57
|
+
const out = {};
|
|
58
|
+
for (const [locale, text] of Object.entries(value)) {
|
|
59
|
+
if (typeof text !== "string") return null;
|
|
60
|
+
out[locale] = text;
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
function toField(value) {
|
|
65
|
+
if (!isRecord(value)) return null;
|
|
66
|
+
if (typeof value.id !== "string" || value.id === "") return null;
|
|
67
|
+
if (typeof value.type !== "string" || !FIELD_TYPES.has(value.type)) return null;
|
|
68
|
+
if (typeof value.required !== "boolean") return null;
|
|
69
|
+
const label = toLocalized(value.label);
|
|
70
|
+
if (label === null) return null;
|
|
71
|
+
const field = {
|
|
72
|
+
id: value.id,
|
|
73
|
+
type: value.type,
|
|
74
|
+
required: value.required,
|
|
75
|
+
label
|
|
76
|
+
};
|
|
77
|
+
if (value.placeholder !== void 0) {
|
|
78
|
+
const placeholder = toLocalized(value.placeholder);
|
|
79
|
+
if (placeholder === null) return null;
|
|
80
|
+
field.placeholder = placeholder;
|
|
81
|
+
}
|
|
82
|
+
if (value.options !== void 0) {
|
|
83
|
+
if (!Array.isArray(value.options)) return null;
|
|
84
|
+
const options = [];
|
|
85
|
+
for (const option of value.options) {
|
|
86
|
+
if (!isRecord(option) || typeof option.value !== "string") return null;
|
|
87
|
+
const optionLabel = toLocalized(option.label);
|
|
88
|
+
if (optionLabel === null) return null;
|
|
89
|
+
options.push({ value: option.value, label: optionLabel });
|
|
90
|
+
}
|
|
91
|
+
field.options = options;
|
|
92
|
+
}
|
|
93
|
+
if (value.validation !== void 0) {
|
|
94
|
+
if (!isRecord(value.validation)) return null;
|
|
95
|
+
const { minLength, maxLength, pattern } = value.validation;
|
|
96
|
+
const validation = {};
|
|
97
|
+
if (minLength !== void 0) {
|
|
98
|
+
if (typeof minLength !== "number") return null;
|
|
99
|
+
validation.minLength = minLength;
|
|
100
|
+
}
|
|
101
|
+
if (maxLength !== void 0) {
|
|
102
|
+
if (typeof maxLength !== "number") return null;
|
|
103
|
+
validation.maxLength = maxLength;
|
|
104
|
+
}
|
|
105
|
+
if (pattern !== void 0) {
|
|
106
|
+
if (typeof pattern !== "string") return null;
|
|
107
|
+
validation.pattern = pattern;
|
|
108
|
+
}
|
|
109
|
+
field.validation = validation;
|
|
110
|
+
}
|
|
111
|
+
return field;
|
|
112
|
+
}
|
|
113
|
+
export function toFormDefinition(value, key) {
|
|
114
|
+
if (!isRecord(value)) return null;
|
|
115
|
+
if (value.key !== key || typeof value.name !== "string") return null;
|
|
116
|
+
if (!Array.isArray(value.fields)) return null;
|
|
117
|
+
const fields = [];
|
|
118
|
+
for (const raw of value.fields) {
|
|
119
|
+
const field = toField(raw);
|
|
120
|
+
if (field === null) return null;
|
|
121
|
+
fields.push(field);
|
|
122
|
+
}
|
|
123
|
+
if (typeof value.honeypotField !== "string" || value.honeypotField === "") return null;
|
|
124
|
+
const turnstile = value.turnstileSiteKey;
|
|
125
|
+
if (turnstile !== void 0 && turnstile !== null && typeof turnstile !== "string") return null;
|
|
126
|
+
if (!isRecord(value.success)) return null;
|
|
127
|
+
const message = toLocalized(value.success.message);
|
|
128
|
+
if (message === null) return null;
|
|
129
|
+
const success = { message };
|
|
130
|
+
if (value.success.redirect !== void 0) {
|
|
131
|
+
const redirect = toLocalized(value.success.redirect);
|
|
132
|
+
if (redirect === null) return null;
|
|
133
|
+
success.redirect = redirect;
|
|
134
|
+
}
|
|
135
|
+
if (!Array.isArray(value.kinds)) return null;
|
|
136
|
+
const kinds = value.kinds.filter(
|
|
137
|
+
(kind) => typeof kind === "string" && ACTION_KINDS.has(kind)
|
|
138
|
+
);
|
|
139
|
+
const definition = {
|
|
140
|
+
key,
|
|
141
|
+
name: value.name,
|
|
142
|
+
fields,
|
|
143
|
+
honeypotField: value.honeypotField,
|
|
144
|
+
turnstileSiteKey: turnstile ?? null,
|
|
145
|
+
success,
|
|
146
|
+
kinds
|
|
147
|
+
};
|
|
148
|
+
if (value.consentText !== void 0) {
|
|
149
|
+
const consentText = toLocalized(value.consentText);
|
|
150
|
+
if (consentText === null) return null;
|
|
151
|
+
definition.consentText = consentText;
|
|
152
|
+
}
|
|
153
|
+
return definition;
|
|
154
|
+
}
|
|
@@ -41,11 +41,13 @@ names?: Readonly<Record<string, string>>): EldraLocaleState;
|
|
|
41
41
|
* Empty on a site with one locale or none: a single-locale site has no alternates, and emitting
|
|
42
42
|
* one that points at itself says nothing.
|
|
43
43
|
*
|
|
44
|
-
* The hrefs are **paths
|
|
45
|
-
*
|
|
46
|
-
* origin
|
|
44
|
+
* The hrefs are **paths** unless `siteUrl` (the site's public origin, when the build knows it) is
|
|
45
|
+
* given, and then they are absolute: hreflang is the one place a search engine prefers a full
|
|
46
|
+
* URL. A prerendered artifact otherwise does not know the origin it will be served from — the
|
|
47
|
+
* host that generated it is a build machine — and a baked-in wrong origin would be worse than a
|
|
48
|
+
* relative one.
|
|
47
49
|
*/
|
|
48
|
-
export declare function localeAlternates(path: string, routing: EldraLocaleRouting): Array<{
|
|
50
|
+
export declare function localeAlternates(path: string, routing: EldraLocaleRouting, siteUrl?: string | null): Array<{
|
|
49
51
|
rel: 'alternate';
|
|
50
52
|
hreflang: string;
|
|
51
53
|
href: string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { buildAlternates } from "@eldrajs/theme-core";
|
|
1
2
|
import { reactive } from "vue";
|
|
2
3
|
import { normalizeLocale } from "./locale.js";
|
|
3
4
|
import {
|
|
@@ -30,19 +31,10 @@ export function createNuxtEldraLocaleState(routing, preview, router, names = {})
|
|
|
30
31
|
}
|
|
31
32
|
});
|
|
32
33
|
}
|
|
33
|
-
export function localeAlternates(path, routing) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}));
|
|
40
|
-
return [
|
|
41
|
-
...alternates,
|
|
42
|
-
{
|
|
43
|
-
rel: "alternate",
|
|
44
|
-
hreflang: "x-default",
|
|
45
|
-
href: localePathFor(path, void 0, routing)
|
|
46
|
-
}
|
|
47
|
-
];
|
|
34
|
+
export function localeAlternates(path, routing, siteUrl) {
|
|
35
|
+
return buildAlternates({
|
|
36
|
+
locales: routing.supported,
|
|
37
|
+
pathFor: (locale) => localePathFor(path, locale, routing),
|
|
38
|
+
siteUrl
|
|
39
|
+
});
|
|
48
40
|
}
|
package/dist/runtime/plugin.js
CHANGED
|
@@ -15,6 +15,7 @@ import blockFields from "virtual:eldra/block-fields";
|
|
|
15
15
|
import manifest from "virtual:eldra/manifest";
|
|
16
16
|
import messages from "virtual:eldra/messages";
|
|
17
17
|
import "virtual:eldra/tokens.css";
|
|
18
|
+
import { useEldraBuildToken } from "./composables/useEldraBuildToken.js";
|
|
18
19
|
import { resolveSiteDesignTokens } from "./designTokens.js";
|
|
19
20
|
import { createNuxtEldraLocaleState, localeAlternates } from "./localeState.js";
|
|
20
21
|
import { localeDisplayName, resolveLocaleRouting, resolveStoreLocales } from "./locales.js";
|
|
@@ -33,6 +34,8 @@ export default defineNuxtPlugin({
|
|
|
33
34
|
orgId: cfg.orgId,
|
|
34
35
|
stega: true,
|
|
35
36
|
retry: cfg.retry ?? void 0,
|
|
37
|
+
// Server prerender only; `undefined` in the browser. See `./buildToken.ts`.
|
|
38
|
+
buildToken: useEldraBuildToken(),
|
|
36
39
|
defaultLocale: storeLocales?.default ?? routing.default ?? null
|
|
37
40
|
});
|
|
38
41
|
const preview = createEldraPreviewState();
|
|
@@ -58,9 +61,14 @@ export default defineNuxtPlugin({
|
|
|
58
61
|
preview
|
|
59
62
|
};
|
|
60
63
|
nuxtApp.vueApp.provide(ELDRA_KEY, context);
|
|
64
|
+
const seoSiteUrl = useRuntimeConfig().public.eldraSeo?.siteUrl;
|
|
61
65
|
useHead(() => ({
|
|
62
66
|
htmlAttrs: { lang: context.locales?.active ?? void 0 },
|
|
63
|
-
link: localeAlternates(
|
|
67
|
+
link: localeAlternates(
|
|
68
|
+
canonicalRoutePath(router()?.currentRoute.value.path ?? "/"),
|
|
69
|
+
routing,
|
|
70
|
+
seoSiteUrl
|
|
71
|
+
)
|
|
64
72
|
}));
|
|
65
73
|
if (import.meta.client) primePrerenderedRoutes();
|
|
66
74
|
if (import.meta.client && window.parent !== window) {
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { EldraClient } from '@eldrajs/sdk';
|
|
2
|
+
import { type EldraLocaleRouting } from './locales.js';
|
|
3
|
+
/**
|
|
4
|
+
* The slice of an `@eldrajs/sdk` client this read needs. `GET /site/v1/redirects` is a
|
|
5
|
+
* site-module read with no typed `features.*` method, so the client's generic `request` carries
|
|
6
|
+
* it (the same shape as `CrawlReader`).
|
|
7
|
+
*/
|
|
8
|
+
export interface RedirectsReader {
|
|
9
|
+
request: EldraClient['request'];
|
|
10
|
+
}
|
|
11
|
+
/** One rule of `GET /site/v1/redirects`. */
|
|
12
|
+
export interface RedirectRule {
|
|
13
|
+
/** A path starting with `/`, no query or fragment; a trailing `*` makes it a wildcard. */
|
|
14
|
+
source: string;
|
|
15
|
+
/** A path starting with `/` (`:splat` allowed after a wildcard source) or an absolute http(s) URL. */
|
|
16
|
+
destination: string;
|
|
17
|
+
status: 301 | 302;
|
|
18
|
+
/** Also applies under every locale prefix; this build expands it into one rule per locale. */
|
|
19
|
+
applyToLocales: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** `GET /site/v1/redirects` as the platform answers it. */
|
|
22
|
+
export interface RedirectsSetting {
|
|
23
|
+
revision: number;
|
|
24
|
+
rules: RedirectRule[];
|
|
25
|
+
}
|
|
26
|
+
/** The one line a build prints when it could not read the platform's redirects. */
|
|
27
|
+
export declare const REDIRECTS_WARNING = "[eldra] could not read the platform\u2019s redirects \u2014 the site ships only the theme\u2019s own public/_redirects";
|
|
28
|
+
/** How long the build waits for the answer; see `READ_TIMEOUT_MS` in `./designTokens.ts`. */
|
|
29
|
+
export declare const READ_TIMEOUT_MS = 10000;
|
|
30
|
+
/** Cloudflare Pages' limits on one `_redirects` file: 2,000 static rules and 100 dynamic ones. */
|
|
31
|
+
export declare const MAX_STATIC_REDIRECTS = 2000;
|
|
32
|
+
export declare const MAX_DYNAMIC_REDIRECTS = 100;
|
|
33
|
+
export declare const REDIRECTS_BEGIN = "# BEGIN eldra platform redirects (generated, edit them in Studio)";
|
|
34
|
+
export declare const REDIRECTS_END = "# END eldra platform redirects";
|
|
35
|
+
/**
|
|
36
|
+
* The platform's redirect rules, or `null`.
|
|
37
|
+
*
|
|
38
|
+
* Fail-soft exactly like `readCrawlSettings`: no client, an unreachable gateway, a timeout and a
|
|
39
|
+
* response that is not the documented shape all end in `null` and **one** warning, so the theme
|
|
40
|
+
* keeps deploying. A site without rules answers `rules: []`, which is an answer, not a failure.
|
|
41
|
+
*/
|
|
42
|
+
export declare function readRedirects(reader: RedirectsReader | null, warn?: (message: string) => void): Promise<RedirectsSetting | null>;
|
|
43
|
+
/** The path the deploy's post-deploy check fetches; a redirect matching it fails every deploy. */
|
|
44
|
+
export declare const MANIFEST_URL_PATH = "/.eldra/manifest.json";
|
|
45
|
+
/**
|
|
46
|
+
* Whether a `_redirects` source would match `/.eldra/manifest.json`. Same semantics as the
|
|
47
|
+
* deploy validator: `*` matches anything, `:name` matches one segment, the rest is literal.
|
|
48
|
+
*/
|
|
49
|
+
export declare function matchesManifestPath(source: string): boolean;
|
|
50
|
+
export interface RedirectsFile {
|
|
51
|
+
/** The whole file, ready to write; `null` when there is nothing to write. */
|
|
52
|
+
content: string | null;
|
|
53
|
+
/** Rules written (after locale expansion). */
|
|
54
|
+
written: number;
|
|
55
|
+
/** One line per thing dropped and why; empty when nothing was. */
|
|
56
|
+
warnings: string[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The `_redirects` file a build writes: the platform's rules between two marker comments, first,
|
|
60
|
+
* then the theme's own `public/_redirects` unchanged, so what a merchant sets in Studio is
|
|
61
|
+
* evaluated before the theme's catch-alls.
|
|
62
|
+
*
|
|
63
|
+
* * `applyToLocales` is expanded here, into one extra rule per **prefixed** locale (the default
|
|
64
|
+
* locale is the unprefixed rule itself); a destination that is a site path gets the same
|
|
65
|
+
* prefix, an absolute URL is left alone.
|
|
66
|
+
* * Exact rules go before wildcards, wildcards longest first (the order Core resolves them in),
|
|
67
|
+
* because Cloudflare Pages takes the first match.
|
|
68
|
+
* * Cloudflare Pages caps a file at 2,000 static and 100 dynamic rules; the theme's own rules
|
|
69
|
+
* count first, then the platform's expanded ones fill what is left, in that order, and every
|
|
70
|
+
* rule over a cap is dropped with one warning rather than failing the deploy.
|
|
71
|
+
*/
|
|
72
|
+
export declare function buildRedirectsFile(rules: readonly RedirectRule[], routing: EldraLocaleRouting, themeContent?: string | null): RedirectsFile;
|
|
73
|
+
/** The theme's own `public/_redirects`, or `null` when it ships none. */
|
|
74
|
+
export declare function readThemeRedirects(themePublicDir: string): string | null;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { localeHref, localePathFor } from "./locales.js";
|
|
4
|
+
export const REDIRECTS_WARNING = "[eldra] could not read the platform\u2019s redirects \u2014 the site ships only the theme\u2019s own public/_redirects";
|
|
5
|
+
export const READ_TIMEOUT_MS = 1e4;
|
|
6
|
+
export const MAX_STATIC_REDIRECTS = 2e3;
|
|
7
|
+
export const MAX_DYNAMIC_REDIRECTS = 100;
|
|
8
|
+
const MAX_LINE_LENGTH = 1e3;
|
|
9
|
+
export const REDIRECTS_BEGIN = "# BEGIN eldra platform redirects (generated, edit them in Studio)";
|
|
10
|
+
export const REDIRECTS_END = "# END eldra platform redirects";
|
|
11
|
+
export async function readRedirects(reader, warn = console.warn) {
|
|
12
|
+
if (reader === null) {
|
|
13
|
+
warn(REDIRECTS_WARNING);
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const controller = new AbortController();
|
|
17
|
+
const timer = setTimeout(
|
|
18
|
+
() => controller.abort(new Error(`timed out after ${READ_TIMEOUT_MS}ms`)),
|
|
19
|
+
READ_TIMEOUT_MS
|
|
20
|
+
);
|
|
21
|
+
try {
|
|
22
|
+
const response = await reader.request({
|
|
23
|
+
path: "/site/v1/redirects",
|
|
24
|
+
signal: controller.signal
|
|
25
|
+
});
|
|
26
|
+
const setting = toRedirectsSetting(response);
|
|
27
|
+
if (setting === null) {
|
|
28
|
+
warn(`${REDIRECTS_WARNING} (the platform published an unusable response)`);
|
|
29
|
+
}
|
|
30
|
+
return setting;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
warn(`${REDIRECTS_WARNING} (${error instanceof Error ? error.message : String(error)})`);
|
|
33
|
+
return null;
|
|
34
|
+
} finally {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function toRedirectsSetting(value) {
|
|
39
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
|
40
|
+
const record = value;
|
|
41
|
+
if (typeof record.revision !== "number" || !Number.isFinite(record.revision)) return null;
|
|
42
|
+
if (!Array.isArray(record.rules)) return null;
|
|
43
|
+
const rules = [];
|
|
44
|
+
for (const raw of record.rules) {
|
|
45
|
+
if (typeof raw !== "object" || raw === null) return null;
|
|
46
|
+
const rule = raw;
|
|
47
|
+
if (typeof rule.source !== "string" || typeof rule.destination !== "string") return null;
|
|
48
|
+
if (rule.status !== 301 && rule.status !== 302) return null;
|
|
49
|
+
rules.push({
|
|
50
|
+
source: rule.source,
|
|
51
|
+
destination: rule.destination,
|
|
52
|
+
status: rule.status,
|
|
53
|
+
applyToLocales: rule.applyToLocales === true
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return { revision: record.revision, rules };
|
|
57
|
+
}
|
|
58
|
+
function isDynamic(source) {
|
|
59
|
+
return source.includes("*") || /:[A-Za-z]/.test(source);
|
|
60
|
+
}
|
|
61
|
+
function isPlainField(value) {
|
|
62
|
+
return value !== "" && !/\s/.test(value);
|
|
63
|
+
}
|
|
64
|
+
function isDestination(value) {
|
|
65
|
+
if (!isPlainField(value)) return false;
|
|
66
|
+
if (value.startsWith("/")) return !value.startsWith("//");
|
|
67
|
+
try {
|
|
68
|
+
const url = new URL(value);
|
|
69
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
70
|
+
} catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function isRuleLine(line) {
|
|
75
|
+
const trimmed = line.trim();
|
|
76
|
+
return trimmed !== "" && !trimmed.startsWith("#");
|
|
77
|
+
}
|
|
78
|
+
export const MANIFEST_URL_PATH = "/.eldra/manifest.json";
|
|
79
|
+
export function matchesManifestPath(source) {
|
|
80
|
+
if (!source.startsWith("/")) return false;
|
|
81
|
+
const pattern = source.split(/(\*|:[A-Za-z_]\w*)/).map(
|
|
82
|
+
(part) => part === "*" ? ".*" : /^:[A-Za-z_]/.test(part) ? "[^/]*" : part.replace(/[.+?^${}()|[\]\\]/g, "\\$&")
|
|
83
|
+
).join("");
|
|
84
|
+
return new RegExp(`^${pattern}$`).test(MANIFEST_URL_PATH);
|
|
85
|
+
}
|
|
86
|
+
export function buildRedirectsFile(rules, routing, themeContent = null) {
|
|
87
|
+
const warnings = [];
|
|
88
|
+
let themeDropped = false;
|
|
89
|
+
if (themeContent !== null) {
|
|
90
|
+
const kept = [];
|
|
91
|
+
for (const line of themeContent.split(/\r?\n/)) {
|
|
92
|
+
const source = isRuleLine(line) ? line.trim().split(/\s+/)[0] ?? "" : "";
|
|
93
|
+
if (source !== "" && matchesManifestPath(source)) {
|
|
94
|
+
themeDropped = true;
|
|
95
|
+
warnings.push(
|
|
96
|
+
`[eldra] theme redirect ${JSON.stringify(source)} dropped: it would match ${MANIFEST_URL_PATH}, which the deploy needs`
|
|
97
|
+
);
|
|
98
|
+
} else kept.push(line);
|
|
99
|
+
}
|
|
100
|
+
if (themeDropped) themeContent = kept.join("\n");
|
|
101
|
+
}
|
|
102
|
+
const themeLines = (themeContent ?? "").split(/\r?\n/).filter(isRuleLine);
|
|
103
|
+
let staticLeft = MAX_STATIC_REDIRECTS;
|
|
104
|
+
let dynamicLeft = MAX_DYNAMIC_REDIRECTS;
|
|
105
|
+
for (const line of themeLines) {
|
|
106
|
+
const source = line.trim().split(/\s+/)[0] ?? "";
|
|
107
|
+
if (isDynamic(source)) dynamicLeft -= 1;
|
|
108
|
+
else staticLeft -= 1;
|
|
109
|
+
}
|
|
110
|
+
const expanded = [];
|
|
111
|
+
const seen = /* @__PURE__ */ new Set();
|
|
112
|
+
const push = (rule) => {
|
|
113
|
+
if (seen.has(rule.source)) return;
|
|
114
|
+
seen.add(rule.source);
|
|
115
|
+
expanded.push(rule);
|
|
116
|
+
};
|
|
117
|
+
for (const rule of rules) {
|
|
118
|
+
if (!rule.source.startsWith("/") || !isPlainField(rule.source) || !isDestination(rule.destination)) {
|
|
119
|
+
warnings.push(`[eldra] redirect ${JSON.stringify(rule.source)} skipped: not a valid rule`);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (matchesManifestPath(rule.source)) {
|
|
123
|
+
warnings.push(
|
|
124
|
+
`[eldra] redirect ${JSON.stringify(rule.source)} dropped: it would match ${MANIFEST_URL_PATH}, which the deploy needs`
|
|
125
|
+
);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
push(rule);
|
|
129
|
+
if (rule.applyToLocales) {
|
|
130
|
+
for (const locale of routing.prefixed) {
|
|
131
|
+
const source = localePathFor(rule.source, locale, routing);
|
|
132
|
+
if (matchesManifestPath(source)) {
|
|
133
|
+
warnings.push(
|
|
134
|
+
`[eldra] redirect ${JSON.stringify(source)} dropped: it would match ${MANIFEST_URL_PATH}, which the deploy needs`
|
|
135
|
+
);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
push({
|
|
139
|
+
...rule,
|
|
140
|
+
source,
|
|
141
|
+
destination: rule.destination.startsWith("/") ? localeHref(rule.destination, locale, routing) : rule.destination
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const exact = expanded.filter((rule) => !isDynamic(rule.source));
|
|
147
|
+
const wildcards = expanded.filter((rule) => isDynamic(rule.source)).sort((a, b) => b.source.length - a.source.length);
|
|
148
|
+
const lines = [];
|
|
149
|
+
let droppedStatic = 0;
|
|
150
|
+
let droppedDynamic = 0;
|
|
151
|
+
for (const rule of [...exact, ...wildcards]) {
|
|
152
|
+
const line = `${rule.source} ${rule.destination} ${rule.status}`;
|
|
153
|
+
if (line.length > MAX_LINE_LENGTH) {
|
|
154
|
+
warnings.push(`[eldra] redirect ${JSON.stringify(rule.source)} skipped: line too long`);
|
|
155
|
+
} else if (isDynamic(rule.source)) {
|
|
156
|
+
if (dynamicLeft > 0) {
|
|
157
|
+
dynamicLeft -= 1;
|
|
158
|
+
lines.push(line);
|
|
159
|
+
} else droppedDynamic += 1;
|
|
160
|
+
} else if (staticLeft > 0) {
|
|
161
|
+
staticLeft -= 1;
|
|
162
|
+
lines.push(line);
|
|
163
|
+
} else droppedStatic += 1;
|
|
164
|
+
}
|
|
165
|
+
if (droppedStatic > 0) {
|
|
166
|
+
warnings.push(
|
|
167
|
+
`[eldra] ${droppedStatic} static redirect(s) dropped: Cloudflare Pages allows ${MAX_STATIC_REDIRECTS} per site, including the theme\u2019s own and the locale expansion`
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
if (droppedDynamic > 0) {
|
|
171
|
+
warnings.push(
|
|
172
|
+
`[eldra] ${droppedDynamic} wildcard redirect(s) dropped: Cloudflare Pages allows ${MAX_DYNAMIC_REDIRECTS} per site, including the theme\u2019s own and the locale expansion`
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
if (lines.length === 0) {
|
|
176
|
+
const content = themeDropped && themeContent.trim() !== "" ? `${themeContent.trimEnd()}
|
|
177
|
+
` : "";
|
|
178
|
+
return { content: themeDropped ? content : null, written: 0, warnings };
|
|
179
|
+
}
|
|
180
|
+
const platform = [REDIRECTS_BEGIN, ...lines, REDIRECTS_END].join("\n");
|
|
181
|
+
const theme = themeContent === null ? "" : `
|
|
182
|
+
${themeContent.trimEnd()}
|
|
183
|
+
`;
|
|
184
|
+
return { content: `${platform}
|
|
185
|
+
${theme}`, written: lines.length, warnings };
|
|
186
|
+
}
|
|
187
|
+
export function readThemeRedirects(themePublicDir) {
|
|
188
|
+
const path = join(themePublicDir, "_redirects");
|
|
189
|
+
return existsSync(path) ? readFileSync(path, "utf8") : null;
|
|
190
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { EldraClient } from '@eldrajs/sdk';
|
|
2
|
+
import { type SeoSetting } from '@eldrajs/theme-core';
|
|
3
|
+
/**
|
|
4
|
+
* The slice of an `@eldrajs/sdk` client this read needs — a whole `EldraClient` satisfies it. The
|
|
5
|
+
* same narrow shape as `SiteIconsReader`: `GET /site/v1/seo` is a site-module read with no typed
|
|
6
|
+
* `features.*` method, so the client's generic `request` carries it.
|
|
7
|
+
*/
|
|
8
|
+
export interface SeoReader {
|
|
9
|
+
request: EldraClient['request'];
|
|
10
|
+
}
|
|
11
|
+
/** The one line a build prints when it could not read the platform's SEO settings. */
|
|
12
|
+
export declare const SEO_WARNING = "[eldra] could not read the platform\u2019s SEO settings \u2014 page titles use the default pattern";
|
|
13
|
+
/** How long the build waits for the answer; see `READ_TIMEOUT_MS` in `./designTokens.ts`. */
|
|
14
|
+
export declare const READ_TIMEOUT_MS = 10000;
|
|
15
|
+
/**
|
|
16
|
+
* The platform's SEO setting, or `null`.
|
|
17
|
+
*
|
|
18
|
+
* Fail-soft exactly like `readSiteIcons`: no client, an unreachable gateway, a timeout and a
|
|
19
|
+
* response that is not the documented shape (including a pattern syntax this kit does not know)
|
|
20
|
+
* all end in `null` and **one** warning, so the theme keeps deploying with the default title
|
|
21
|
+
* pattern.
|
|
22
|
+
*/
|
|
23
|
+
export declare function readSeo(reader: SeoReader | null, warn?: (message: string) => void): Promise<SeoSetting | null>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { SEO_SYNTAX } from "@eldrajs/theme-core";
|
|
2
|
+
export const SEO_WARNING = "[eldra] could not read the platform\u2019s SEO settings \u2014 page titles use the default pattern";
|
|
3
|
+
export const READ_TIMEOUT_MS = 1e4;
|
|
4
|
+
export async function readSeo(reader, warn = console.warn) {
|
|
5
|
+
const read = reader === null ? { setting: null, cause: null } : await readOrNull(reader);
|
|
6
|
+
if (read.setting === null) {
|
|
7
|
+
warn(read.cause === null ? SEO_WARNING : `${SEO_WARNING} (${read.cause})`);
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return read.setting;
|
|
11
|
+
}
|
|
12
|
+
async function readOrNull(reader) {
|
|
13
|
+
const controller = new AbortController();
|
|
14
|
+
const timer = setTimeout(
|
|
15
|
+
() => controller.abort(new Error(`timed out after ${READ_TIMEOUT_MS}ms`)),
|
|
16
|
+
READ_TIMEOUT_MS
|
|
17
|
+
);
|
|
18
|
+
try {
|
|
19
|
+
const response = await reader.request({
|
|
20
|
+
path: "/site/v1/seo",
|
|
21
|
+
signal: controller.signal
|
|
22
|
+
});
|
|
23
|
+
const setting = toSeoSetting(response);
|
|
24
|
+
return {
|
|
25
|
+
setting,
|
|
26
|
+
cause: setting === null ? "the platform published an unusable response" : null
|
|
27
|
+
};
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return { setting: null, cause: error instanceof Error ? error.message : String(error) };
|
|
30
|
+
} finally {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function toSeoSetting(value) {
|
|
35
|
+
const record = asRecord(value);
|
|
36
|
+
if (record === null) return null;
|
|
37
|
+
if (typeof record.revision !== "number" || !Number.isFinite(record.revision)) return null;
|
|
38
|
+
if (record.syntax !== SEO_SYNTAX) return null;
|
|
39
|
+
const site = asRecord(record.site);
|
|
40
|
+
const templates = asRecord(record.templates);
|
|
41
|
+
if (site === null || templates === null) return null;
|
|
42
|
+
const titlePattern = toLocaleMap(site.titlePattern);
|
|
43
|
+
const description = toLocaleMap(site.description);
|
|
44
|
+
if (titlePattern === null || description === null) return null;
|
|
45
|
+
const name = optionalText(site.name);
|
|
46
|
+
const imageAssetId = optionalText(site.imageAssetId);
|
|
47
|
+
const imageUrl = optionalText(site.imageUrl);
|
|
48
|
+
const twitterHandle = optionalText(site.twitterHandle);
|
|
49
|
+
if (name === void 0 || imageAssetId === void 0 || imageUrl === void 0 || twitterHandle === void 0) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const byContext = /* @__PURE__ */ Object.create(null);
|
|
53
|
+
for (const [context, raw] of Object.entries(templates)) {
|
|
54
|
+
const entry = asRecord(raw);
|
|
55
|
+
if (entry === null) return null;
|
|
56
|
+
const patterns = {};
|
|
57
|
+
if (entry.titlePattern !== void 0) {
|
|
58
|
+
const map = toLocaleMap(entry.titlePattern);
|
|
59
|
+
if (map === null) return null;
|
|
60
|
+
patterns.titlePattern = map;
|
|
61
|
+
}
|
|
62
|
+
if (entry.descriptionPattern !== void 0) {
|
|
63
|
+
const map = toLocaleMap(entry.descriptionPattern);
|
|
64
|
+
if (map === null) return null;
|
|
65
|
+
patterns.descriptionPattern = map;
|
|
66
|
+
}
|
|
67
|
+
byContext[context] = patterns;
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
revision: record.revision,
|
|
71
|
+
syntax: SEO_SYNTAX,
|
|
72
|
+
site: { name, titlePattern, description, imageAssetId, imageUrl, twitterHandle },
|
|
73
|
+
templates: byContext
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function asRecord(value) {
|
|
77
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
78
|
+
}
|
|
79
|
+
function optionalText(value) {
|
|
80
|
+
if (value === null || value === void 0) return null;
|
|
81
|
+
if (typeof value !== "string") return void 0;
|
|
82
|
+
const text = value.trim();
|
|
83
|
+
return text === "" ? null : text;
|
|
84
|
+
}
|
|
85
|
+
function toLocaleMap(value) {
|
|
86
|
+
if (value === null || value === void 0) return {};
|
|
87
|
+
const record = asRecord(value);
|
|
88
|
+
if (record === null) return null;
|
|
89
|
+
const map = /* @__PURE__ */ Object.create(null);
|
|
90
|
+
for (const [locale, text] of Object.entries(record)) {
|
|
91
|
+
if (typeof text !== "string") return null;
|
|
92
|
+
map[locale] = text;
|
|
93
|
+
}
|
|
94
|
+
return map;
|
|
95
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ResolvedSeo } from '@eldrajs/theme-core';
|
|
2
|
+
/** A `<meta>` tag as `useHead` takes it. */
|
|
3
|
+
export interface SeoMetaTag {
|
|
4
|
+
name?: string;
|
|
5
|
+
property?: string;
|
|
6
|
+
content: string;
|
|
7
|
+
}
|
|
8
|
+
export interface SeoHead {
|
|
9
|
+
title: string;
|
|
10
|
+
meta: SeoMetaTag[];
|
|
11
|
+
link: {
|
|
12
|
+
rel: 'canonical';
|
|
13
|
+
href: string;
|
|
14
|
+
}[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The head one resolved route writes: `<title>`, description, canonical, Open Graph, Twitter card
|
|
18
|
+
* and robots. Pure, so a test can read every tag without a Nuxt app.
|
|
19
|
+
*
|
|
20
|
+
* `og:url`, `og:image` and the canonical link are written only when absolute (the resolver
|
|
21
|
+
* returns `null` otherwise); a share card with a relative URL is worse than none. `robots` is
|
|
22
|
+
* written only for `noindex` — an indexable page says nothing, which is the default.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildSeoHead(seo: ResolvedSeo, context: {
|
|
25
|
+
locale: string | null;
|
|
26
|
+
siteName: string;
|
|
27
|
+
}): SeoHead;
|