@eldrajs/theme-cli 0.2.0 → 0.2.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/package.json +3 -3
- package/template/.eldra/block-types.d.ts +12 -3
- package/template/.eldra/manifest.json +315 -25
- package/template/.eldra/previews.json +35 -34
- package/template/.github/workflows/eldra-deploy.yml +2 -0
- package/template/.gitlab-ci.yml +2 -0
- package/template/.storybook/main.ts +3 -0
- package/template/README.md +22 -10
- package/template/app/components/FormHoneypot.vue +21 -0
- package/template/app/composables/useFormSpam.ts +55 -0
- package/template/app/composables/usePageStructuredData.ts +112 -0
- package/template/app/composables/useTurnstile.ts +111 -0
- package/template/app/formSeeds.ts +93 -0
- package/template/app/pages/[...slug].vue +48 -12
- package/template/app/plugins/eldra-storefront.ts +19 -2
- package/template/app/seededPages.ts +7 -0
- package/template/app/storefront/analytics.ts +202 -0
- package/template/app/storefront/cart.ts +7 -0
- package/template/app/storefront/demo.ts +48 -2
- package/template/app/storefront/forms.ts +133 -0
- package/template/app/storefront/gateway.ts +113 -7
- package/template/app/storefront/structuredData.ts +161 -0
- package/template/app/storefront/types.ts +101 -2
- package/template/app/utils/seoInput.ts +172 -0
- package/template/bitbucket-pipelines.yml +3 -1
- package/template/blocks/breadcrumbs/Block.vue +1 -0
- package/template/blocks/cart/Block.vue +1 -0
- package/template/blocks/cart/parts/Summary.vue +1 -0
- package/template/blocks/contact/Block.vue +50 -21
- package/template/blocks/contact/__tests__/Block.spec.ts +89 -8
- package/template/blocks/contact/block.json +2 -9
- package/template/blocks/contact/mock.json +1 -2
- package/template/blocks/footer/Block.vue +56 -6
- package/template/blocks/footer/__tests__/Block.spec.ts +70 -16
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/form/Block.vue +614 -0
- package/template/blocks/form/__tests__/Block.spec.ts +536 -0
- package/template/blocks/form/block.json +69 -0
- package/template/blocks/form/mock.json +25 -0
- package/template/blocks/form/preview.png +0 -0
- package/template/blocks/form/validate.ts +68 -0
- package/template/blocks/newsletter/Block.vue +57 -3
- package/template/blocks/newsletter/__tests__/Block.spec.ts +89 -31
- package/template/blocks/newsletter/block.json +4 -5
- package/template/blocks/newsletter/mock.json +1 -1
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/i18n/en-US.json +33 -2
- package/template/i18n/is-IS.json +33 -2
- package/template/nuxt.config.ts +6 -0
- package/template/stories/generated/form.stories.ts +68 -0
- package/template/test/formValidate.spec.ts +46 -0
- package/template/test/prerenderFailure.spec.ts +1 -1
- package/template/test/prerenderRefresh.browser.spec.ts +5 -1
- package/template/test/seededPages.spec.ts +13 -0
- package/template/test/seoInput.spec.ts +119 -0
- package/template/test/slugPage.spec.ts +62 -26
- package/template/test/slugPageTemplate.spec.ts +5 -1
- package/template/test/starter.spec.ts +41 -3
- package/template/test/storefront/analytics.spec.ts +283 -0
- package/template/test/storefront/forms.spec.ts +385 -0
- package/template/test/storefront/structuredData.spec.ts +138 -0
- package/template/test/structuredData.generate.spec.ts +159 -0
- package/template/test/support/mockGateway.ts +29 -1
- package/template/test/support/turnstileForms.ts +53 -0
- package/template/app/utils/pageTitle.ts +0 -37
- package/template/test/pageTitle.spec.ts +0 -49
|
@@ -17,6 +17,8 @@ jobs:
|
|
|
17
17
|
env:
|
|
18
18
|
ELDRA_GATEWAY_URL: ${{ vars.ELDRA_GATEWAY_URL }}
|
|
19
19
|
ELDRA_ORG_ID: ${{ vars.ELDRA_ORG_ID }}
|
|
20
|
+
# A build is production unless ELDRA_BUILD_TARGET=preview, so none is set here.
|
|
21
|
+
ELDRA_SITE_URL: ${{ vars.ELDRA_SITE_URL }}
|
|
20
22
|
- run: pnpm exec eldra-theme deploy
|
|
21
23
|
env:
|
|
22
24
|
ELDRA_API_URL: ${{ vars.ELDRA_API_URL }}
|
package/template/.gitlab-ci.yml
CHANGED
|
@@ -13,4 +13,6 @@ eldra-deploy:
|
|
|
13
13
|
- pnpm exec nuxi generate
|
|
14
14
|
- pnpm exec eldra-theme deploy
|
|
15
15
|
variables:
|
|
16
|
+
# A build is production unless ELDRA_BUILD_TARGET=preview, so none is set here.
|
|
17
|
+
# ELDRA_SITE_URL (the public origin) is a CI/CD variable set on the project.
|
|
16
18
|
ELDRA_TRIGGER_DEPLOYMENT_ID: $ELDRA_TRIGGER_DEPLOYMENT_ID
|
|
@@ -4,6 +4,7 @@ import tailwindcss from '@tailwindcss/vite';
|
|
|
4
4
|
import vue from '@vitejs/plugin-vue';
|
|
5
5
|
import eldraTheme from '@eldrajs/vite-plugin-theme';
|
|
6
6
|
import { generateStories } from '../scripts/generate-stories.mjs';
|
|
7
|
+
import { starterFormSeeds } from '../app/formSeeds';
|
|
7
8
|
import { starterSeeds, starterTemplateRoles } from '../app/templates';
|
|
8
9
|
|
|
9
10
|
const themeDir = fileURLToPath(new URL('..', import.meta.url));
|
|
@@ -47,6 +48,8 @@ const config: StorybookConfig = {
|
|
|
47
48
|
tailwind: false,
|
|
48
49
|
templates: starterSeeds(),
|
|
49
50
|
templateRoles: starterTemplateRoles(),
|
|
51
|
+
// Declared for the same reason: this instance writes `.eldra/manifest.json` too.
|
|
52
|
+
formSeeds: starterFormSeeds(),
|
|
50
53
|
}),
|
|
51
54
|
];
|
|
52
55
|
config.resolve = {
|
package/template/README.md
CHANGED
|
@@ -102,7 +102,7 @@ alongside it.
|
|
|
102
102
|
|
|
103
103
|
- **structure** — `navigation` ("Header"), `announcement-bar`, `breadcrumbs`, `footer`.
|
|
104
104
|
- **marketing** — `hero`, `cta` ("Call to action"), `feature-grid`, `split-content`, `stats`,
|
|
105
|
-
`logo-cloud`, `testimonials`, `faq`, `pricing-table`, `newsletter`, `contact`
|
|
105
|
+
`logo-cloud`, `testimonials`, `faq`, `pricing-table`, `newsletter`, `contact`, `form`
|
|
106
106
|
("Contact and map"), `video-embed`, `team`, `timeline`.
|
|
107
107
|
- **content** — `article`, `article-list`, `rich-text`, `gallery`, `image`, `quote`, `tabs`.
|
|
108
108
|
- **commerce** — `collection-header`, `product-carousel`, `collection-grid`, `product-detail`,
|
|
@@ -126,10 +126,14 @@ your own site's pages, or as a reference for how a set of blocks composes.
|
|
|
126
126
|
|
|
127
127
|
## Storefront forms
|
|
128
128
|
|
|
129
|
-
|
|
130
|
-
(`
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
Forms come from the platform: a merchant builds them on Studio's Forms page, and the `form` block
|
|
130
|
+
(plus `contact`, `newsletter` and the footer's sign-up, which submit to the seeded `contact` and
|
|
131
|
+
`newsletter` forms) renders and submits them through `useStorefront().forms`. `app/formSeeds.ts` is
|
|
132
|
+
what a fresh site starts with (the `newsletter` seed carries its consent sentence per locale, which the `form` block shows under the button). See `docs/starter-kit.md`, "Forms".
|
|
133
|
+
|
|
134
|
+
`formsEndpoint` overrides where a form _submission_ goes, and is where the back-in-stock form
|
|
135
|
+
(`catalog.notifyBackInStock`, which has no platform endpoint) posts. Without it that one resolves
|
|
136
|
+
`{ ok: false, reason: 'unsupported' }` and the block renders its own "isn't set up yet" copy:
|
|
133
137
|
|
|
134
138
|
```ts
|
|
135
139
|
export default defineNuxtConfig({
|
|
@@ -141,10 +145,10 @@ export default defineNuxtConfig({
|
|
|
141
145
|
});
|
|
142
146
|
```
|
|
143
147
|
|
|
144
|
-
or
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
+
or set the matching `NUXT_PUBLIC_FORMS_ENDPOINT` environment variable at build/deploy time. A form
|
|
149
|
+
submission is posted as `{ kind: 'form', key, values, locale, pagePath, turnstileToken?, hp? }` JSON (the last two are the
|
|
150
|
+
spam signals the form collected; verify them or ignore them), a back-in-stock
|
|
151
|
+
request as `{ kind: 'notifyBackInStock', ... }`; a non-2xx is a recoverable failure.
|
|
148
152
|
|
|
149
153
|
## Checkout
|
|
150
154
|
|
|
@@ -155,6 +159,14 @@ button appears when that resolves. If the platform publishes no checkout URL, or
|
|
|
155
159
|
come back, the cart still adds, updates and persists lines normally; it just has no Check out
|
|
156
160
|
button to show, since there is nowhere to hand the cart off to.
|
|
157
161
|
|
|
162
|
+
## Analytics
|
|
163
|
+
|
|
164
|
+
On a production build the theme reports to Studio's analytics through `@eldrajs/theme-nuxt` (see
|
|
165
|
+
`docs/themes.md`, _Built-in analytics_), and `app/storefront/analytics.ts` sends `product_view`,
|
|
166
|
+
`add_to_cart`, `remove_from_cart`, `begin_checkout` and `search` by wrapping the storefront source in
|
|
167
|
+
`app/plugins/eldra-storefront.ts`, so blocks never call it. Opt out with `eldra: { analytics: false }`
|
|
168
|
+
in `nuxt.config.ts` or `ELDRA_ANALYTICS=off`. Nothing is written to cookies or storage.
|
|
169
|
+
|
|
158
170
|
## Prices and the store currency
|
|
159
171
|
|
|
160
172
|
Nothing to configure here either, and nothing to guess: the organisation's own commerce settings
|
|
@@ -286,7 +298,7 @@ The deploy command uploads the generated static site and exact `.eldra/manifest.
|
|
|
286
298
|
|
|
287
299
|
The starter includes GitHub Actions, GitLab CI, and Bitbucket Pipelines examples. All build locally with Node 20 and pnpm, run `nuxi generate`, then push the static output with `eldra-theme deploy`.
|
|
288
300
|
|
|
289
|
-
- GitHub: set repository variables `ELDRA_GATEWAY_URL`, `ELDRA_ORG_ID`, and `
|
|
301
|
+
- GitHub: set repository variables `ELDRA_GATEWAY_URL`, `ELDRA_ORG_ID`, `ELDRA_API_URL` and `ELDRA_SITE_URL` (the public origin, for canonical and `og:url`), plus secret `ELDRA_DEPLOY_TOKEN`. In Studio choose the GitHub trigger and configure `https://api.github.com/repos/<owner>/<repo>/dispatches` with a token allowed to dispatch workflows.
|
|
290
302
|
- GitLab: configure those four names as project CI/CD variables, masking `ELDRA_DEPLOY_TOKEN`. In Studio use the project pipeline-trigger URL, trigger token, and branch/tag ref.
|
|
291
303
|
- Bitbucket: configure repository variables, securing `ELDRA_DEPLOY_TOKEN`. In Studio use the pipelines endpoint, `username:app-password` token, and ref; the backend selects the `eldra-rebuild` custom pipeline.
|
|
292
304
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* The honeypot of a platform form: a text input named by the form's definition, off-screen and
|
|
4
|
+
* inert so a person never reaches it, and a bot that fills every input does. Its value is sent as
|
|
5
|
+
* `hp` and the platform drops the submission when it is not empty. Renders nothing until the
|
|
6
|
+
* definition names the field.
|
|
7
|
+
*/
|
|
8
|
+
defineProps<{ name: string | null }>();
|
|
9
|
+
const value = defineModel<string>({ default: '' });
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<template>
|
|
13
|
+
<div
|
|
14
|
+
v-if="name"
|
|
15
|
+
aria-hidden="true"
|
|
16
|
+
inert
|
|
17
|
+
class="absolute -left-[10000px] size-px overflow-hidden opacity-0"
|
|
18
|
+
>
|
|
19
|
+
<input v-model="value" type="text" :name="name" tabindex="-1" autocomplete="off" />
|
|
20
|
+
</div>
|
|
21
|
+
</template>
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { computed, ref, type Ref } from 'vue';
|
|
2
|
+
import { useI18n } from 'vue-i18n';
|
|
3
|
+
import { useStorefront } from './useStorefront';
|
|
4
|
+
import { useTurnstile } from './useTurnstile';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The spam protection of a platform form for a block that renders its own fields (`contact`,
|
|
8
|
+
* `newsletter`, the footer's sign-up): the honeypot and the Cloudflare Turnstile the merchant may
|
|
9
|
+
* turn on, both read from the form's definition (`forms.definition`), the way `blocks/form` does.
|
|
10
|
+
* Without it Core rejects every submission of a form that has Turnstile on.
|
|
11
|
+
*
|
|
12
|
+
* A block renders `<FormHoneypot v-model="honeypot" :name="honeypotField" />` and, when `siteKey`
|
|
13
|
+
* is set, an element bound to `turnstileRoot`; before it submits it asks `challenge()` (a message
|
|
14
|
+
* to show, or `null` to go ahead) and spreads `signals()` into the submission. A definition that
|
|
15
|
+
* has not loaded is not a reason to block: the platform decides.
|
|
16
|
+
*
|
|
17
|
+
* @param formKey the key of the form; empty reads nothing (a block that is not showing the form)
|
|
18
|
+
*/
|
|
19
|
+
export function useFormSpam(formKey: Ref<string>) {
|
|
20
|
+
const { t } = useI18n();
|
|
21
|
+
const definition = useStorefront().forms.definition(formKey);
|
|
22
|
+
|
|
23
|
+
const honeypot = ref('');
|
|
24
|
+
const turnstileRoot = ref<HTMLElement | null>(null);
|
|
25
|
+
const honeypotField = computed(() => definition.data.value?.honeypotField ?? null);
|
|
26
|
+
const siteKey = computed(() => definition.data.value?.turnstileSiteKey ?? null);
|
|
27
|
+
/** The platform's consent sentence of a subscribing form (`{shop}` filled in, page locale), or ''. */
|
|
28
|
+
const consentSentence = computed(() => (definition.data.value?.consentText ?? '').trim());
|
|
29
|
+
const turnstile = useTurnstile(siteKey, turnstileRoot);
|
|
30
|
+
|
|
31
|
+
/** Why the visitor cannot submit yet (Turnstile is on and not passed), or `null`. */
|
|
32
|
+
function challenge(): string | null {
|
|
33
|
+
if (siteKey.value === null || turnstile.token.value !== null) return null;
|
|
34
|
+
return turnstile.failed.value ? t('form.turnstileFailed') : t('form.turnstileRequired');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The spam signals to send with a submission: a token is single-use, so reset after it. */
|
|
38
|
+
function signals(): { turnstileToken?: string; hp?: string } {
|
|
39
|
+
return {
|
|
40
|
+
...(turnstile.token.value ? { turnstileToken: turnstile.token.value } : {}),
|
|
41
|
+
...(honeypot.value ? { hp: honeypot.value } : {}),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
honeypot,
|
|
47
|
+
honeypotField,
|
|
48
|
+
siteKey,
|
|
49
|
+
consentSentence,
|
|
50
|
+
turnstileRoot,
|
|
51
|
+
challenge,
|
|
52
|
+
signals,
|
|
53
|
+
reset: turnstile.reset,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { computed, ref, type ComputedRef, type Ref } from 'vue';
|
|
2
|
+
import { siteOrigin, type ResolvedSeo } from '@eldrajs/theme-core';
|
|
3
|
+
import { useEldraLocale } from '@eldrajs/theme-vue';
|
|
4
|
+
import { useI18n } from 'vue-i18n';
|
|
5
|
+
import { SEEDED_PAGE_SLUGS } from '../seededPages';
|
|
6
|
+
import { categoryHref } from '../utils/links';
|
|
7
|
+
import { useMoney } from '../storefront/money';
|
|
8
|
+
import { pageStructuredData, type PageLdKind } from '../storefront/structuredData';
|
|
9
|
+
import { useStorefront } from './useStorefront';
|
|
10
|
+
|
|
11
|
+
/** The listing's own first page: what a shopper sees before they scroll, and what Google reads. */
|
|
12
|
+
const ITEM_LIST_SIZE = 24;
|
|
13
|
+
|
|
14
|
+
/** The search page the seeded `search` page lives at (`app/templates.ts`): its `SearchAction`. */
|
|
15
|
+
const SEARCH_PATH = '/search';
|
|
16
|
+
/** Advertised only when the theme seeds that page: a `SearchAction` into a 404 is worse than none. */
|
|
17
|
+
const SEARCH_ACTION_PATH = SEEDED_PAGE_SLUGS.includes('search') ? SEARCH_PATH : null;
|
|
18
|
+
|
|
19
|
+
export interface PageStructuredDataInput {
|
|
20
|
+
catalog: Ref<{ kind: 'product' | 'collection' | 'category'; slug: string; path?: string } | null>;
|
|
21
|
+
isNotFound: Ref<boolean>;
|
|
22
|
+
isHome: Ref<boolean>;
|
|
23
|
+
/** `useEldraSeo`'s answer: the canonical, and `noindex`, which carries no markup. */
|
|
24
|
+
seo: Ref<ResolvedSeo>;
|
|
25
|
+
/** The page's (or catalog object's) own name, the last breadcrumb level. */
|
|
26
|
+
title: ComputedRef<string>;
|
|
27
|
+
/** The request path as served. */
|
|
28
|
+
path: Ref<string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Writes the page's schema.org JSON-LD (`app/storefront/structuredData.ts` says which nodes a page
|
|
33
|
+
* type gets) through `@eldrajs/theme-nuxt`'s `useStructuredData`, which applies the theme's
|
|
34
|
+
* opt-out and extender.
|
|
35
|
+
*
|
|
36
|
+
* It reads through the storefront the blocks read through, so a product page's markup carries the
|
|
37
|
+
* very product, price, currency and stock state the buy box renders; the results share the
|
|
38
|
+
* blocks' cache keys, so the build reads each one once. A page the SEO resolver marks `noindex`
|
|
39
|
+
* (a preview build, the not-found shell, an author's own choice) carries none.
|
|
40
|
+
*/
|
|
41
|
+
export function usePageStructuredData(input: PageStructuredDataInput): void {
|
|
42
|
+
const storefront = useStorefront();
|
|
43
|
+
const locale = useEldraLocale();
|
|
44
|
+
const money = useMoney();
|
|
45
|
+
const profile = useSiteProfile();
|
|
46
|
+
const { t } = useI18n();
|
|
47
|
+
|
|
48
|
+
// Final at setup, like every source a result is keyed by: a route change remounts the page.
|
|
49
|
+
const kind = input.catalog.value?.kind ?? null;
|
|
50
|
+
const catalogSlug = input.catalog.value?.slug ?? null;
|
|
51
|
+
const categoryPath = input.catalog.value?.path ?? null;
|
|
52
|
+
|
|
53
|
+
const product = storefront.catalog.product(ref(kind === 'product' ? catalogSlug : null)).data;
|
|
54
|
+
const category = storefront.catalog.category(ref(kind === 'category' ? categoryPath : null)).data;
|
|
55
|
+
const items =
|
|
56
|
+
kind === 'collection' && catalogSlug !== null
|
|
57
|
+
? storefront.catalog.collectionProducts(
|
|
58
|
+
ref({ slug: catalogSlug }),
|
|
59
|
+
ref({ page: 1, pageSize: ITEM_LIST_SIZE })
|
|
60
|
+
).data
|
|
61
|
+
: kind === 'category' && categoryPath !== null
|
|
62
|
+
? storefront.catalog.products(ref({ page: 1, pageSize: ITEM_LIST_SIZE, categoryPath })).data
|
|
63
|
+
: ref(null);
|
|
64
|
+
|
|
65
|
+
const origin = siteOrigin(profile.siteUrl);
|
|
66
|
+
// A site path as the URL to write: the page's language kept, absolute when the origin is known.
|
|
67
|
+
const url = (path: string): string => {
|
|
68
|
+
const prefixed = locale.path(path);
|
|
69
|
+
return origin === null ? prefixed : `${origin}${prefixed}`;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const pageKind = computed<PageLdKind>(() => {
|
|
73
|
+
if (input.isNotFound.value) return 'notFound';
|
|
74
|
+
if (kind !== null) return kind;
|
|
75
|
+
return input.isHome.value ? 'home' : 'page';
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
useStructuredData(
|
|
79
|
+
() => {
|
|
80
|
+
const trail =
|
|
81
|
+
kind === 'product'
|
|
82
|
+
? (product.value?.categoryTrail ?? []).map((level) => ({
|
|
83
|
+
label: level.label,
|
|
84
|
+
path: level.href,
|
|
85
|
+
}))
|
|
86
|
+
: kind === 'category'
|
|
87
|
+
? (category.value?.ancestors ?? []).map((ancestor) => ({
|
|
88
|
+
label: ancestor.title,
|
|
89
|
+
path: categoryHref(ancestor.path),
|
|
90
|
+
}))
|
|
91
|
+
: [];
|
|
92
|
+
return pageStructuredData({
|
|
93
|
+
kind: pageKind.value,
|
|
94
|
+
title: input.title.value,
|
|
95
|
+
currentUrl: input.seo.value.canonical ?? url(input.path.value),
|
|
96
|
+
url,
|
|
97
|
+
absolute: origin !== null,
|
|
98
|
+
homeLabel: t('breadcrumbs.home'),
|
|
99
|
+
trail,
|
|
100
|
+
product: product.value,
|
|
101
|
+
currency: money.currency.value,
|
|
102
|
+
items: (items.value?.items ?? []).map((item) => ({
|
|
103
|
+
title: item.title,
|
|
104
|
+
url: url(item.url),
|
|
105
|
+
})),
|
|
106
|
+
profile,
|
|
107
|
+
searchPath: SEARCH_ACTION_PATH,
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
{ enabled: () => !input.seo.value.noindex }
|
|
111
|
+
);
|
|
112
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { onBeforeUnmount, ref, watch, type Ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
const SCRIPT_SRC = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
|
|
4
|
+
|
|
5
|
+
interface TurnstileApi {
|
|
6
|
+
render(
|
|
7
|
+
container: HTMLElement,
|
|
8
|
+
options: {
|
|
9
|
+
sitekey: string;
|
|
10
|
+
callback: (token: string) => void;
|
|
11
|
+
'expired-callback': () => void;
|
|
12
|
+
'error-callback': () => void;
|
|
13
|
+
}
|
|
14
|
+
): string;
|
|
15
|
+
reset(widgetId: string): void;
|
|
16
|
+
remove(widgetId: string): void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare global {
|
|
20
|
+
interface Window {
|
|
21
|
+
turnstile?: TurnstileApi;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let scriptPromise: Promise<TurnstileApi> | null = null;
|
|
26
|
+
|
|
27
|
+
/** Loads Cloudflare's script once per page; a failed load is forgotten so a later form retries. */
|
|
28
|
+
function loadTurnstile(): Promise<TurnstileApi> {
|
|
29
|
+
if (typeof window === 'undefined') return Promise.reject(new Error('no window'));
|
|
30
|
+
if (window.turnstile) return Promise.resolve(window.turnstile);
|
|
31
|
+
scriptPromise ??= new Promise<TurnstileApi>((resolve, reject) => {
|
|
32
|
+
const script = document.createElement('script');
|
|
33
|
+
script.src = SCRIPT_SRC;
|
|
34
|
+
script.async = true;
|
|
35
|
+
script.onload = () => {
|
|
36
|
+
if (window.turnstile) resolve(window.turnstile);
|
|
37
|
+
else reject(new Error('turnstile missing'));
|
|
38
|
+
};
|
|
39
|
+
script.onerror = () => reject(new Error('turnstile failed to load'));
|
|
40
|
+
document.head.appendChild(script);
|
|
41
|
+
}).catch((error: unknown) => {
|
|
42
|
+
scriptPromise = null;
|
|
43
|
+
throw error;
|
|
44
|
+
});
|
|
45
|
+
return scriptPromise;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Cloudflare Turnstile for one form: renders the widget into `container` once a `siteKey` is
|
|
50
|
+
* known (the merchant turned it on) and exposes the visitor's `token`. Nothing loads without a
|
|
51
|
+
* site key, so a form on a site that never enabled Turnstile makes no third-party request.
|
|
52
|
+
*
|
|
53
|
+
* `token` is `null` until the visitor passes the check, again after it expires, and after
|
|
54
|
+
* `reset()` — which a form calls after every submit, since a token is single-use. `failed` is true
|
|
55
|
+
* when the script or the check could not run, so the form can say so instead of waiting forever.
|
|
56
|
+
* Browser only: on the server (prerender) it does nothing.
|
|
57
|
+
*/
|
|
58
|
+
export function useTurnstile(siteKey: Ref<string | null>, container: Ref<HTMLElement | null>) {
|
|
59
|
+
const token = ref<string | null>(null);
|
|
60
|
+
const failed = ref(false);
|
|
61
|
+
let api: TurnstileApi | null = null;
|
|
62
|
+
let widgetId: string | null = null;
|
|
63
|
+
|
|
64
|
+
async function mount(): Promise<void> {
|
|
65
|
+
if (typeof window === 'undefined' || !siteKey.value || !container.value || widgetId !== null) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const el = container.value;
|
|
69
|
+
const key = siteKey.value;
|
|
70
|
+
try {
|
|
71
|
+
api = await loadTurnstile();
|
|
72
|
+
// The form may have gone away, or changed key, while the script loaded.
|
|
73
|
+
if (container.value !== el || siteKey.value !== key || widgetId !== null) return;
|
|
74
|
+
widgetId = api.render(el, {
|
|
75
|
+
sitekey: key,
|
|
76
|
+
callback: (value) => {
|
|
77
|
+
token.value = value;
|
|
78
|
+
failed.value = false;
|
|
79
|
+
},
|
|
80
|
+
'expired-callback': () => {
|
|
81
|
+
token.value = null;
|
|
82
|
+
},
|
|
83
|
+
'error-callback': () => {
|
|
84
|
+
token.value = null;
|
|
85
|
+
failed.value = true;
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
} catch {
|
|
89
|
+
failed.value = true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function remove(): void {
|
|
94
|
+
if (api && widgetId !== null) api.remove(widgetId);
|
|
95
|
+
widgetId = null;
|
|
96
|
+
token.value = null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function reset(): void {
|
|
100
|
+
token.value = null;
|
|
101
|
+
if (api && widgetId !== null) api.reset(widgetId);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
watch([siteKey, container], () => {
|
|
105
|
+
if (widgetId !== null && (!container.value || !siteKey.value)) remove();
|
|
106
|
+
void mount();
|
|
107
|
+
});
|
|
108
|
+
onBeforeUnmount(remove);
|
|
109
|
+
|
|
110
|
+
return { token, failed, reset, mount };
|
|
111
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { DeclaredFormSeed } from '@eldrajs/vite-plugin-theme';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The forms this theme seeds a site with on its first deploy (`eldra.formSeeds` in
|
|
5
|
+
* `nuxt.config.ts`): the two the starter's own pages use, `contact` and `newsletter`. Core creates
|
|
6
|
+
* a form only when its key is absent and refreshes an unedited seeded form on a theme version bump,
|
|
7
|
+
* so a merchant's edits in Studio's Forms page always win. Every field has a label per locale the
|
|
8
|
+
* starter ships (`en-US`, `is-IS`); a merchant adds the rest in Studio.
|
|
9
|
+
*
|
|
10
|
+
* Also the source of the demo storefront's form definitions (`app/storefront/demo.ts`), so
|
|
11
|
+
* Storybook, the block specs and the deployed site agree on what these two forms are. Nothing here
|
|
12
|
+
* reaches the browser bundle except through that demo module, which is `.storybook`/test-only.
|
|
13
|
+
*/
|
|
14
|
+
export function starterFormSeeds(): DeclaredFormSeed[] {
|
|
15
|
+
return [
|
|
16
|
+
{
|
|
17
|
+
key: 'contact',
|
|
18
|
+
name: 'Contact',
|
|
19
|
+
fields: [
|
|
20
|
+
{
|
|
21
|
+
id: 'name',
|
|
22
|
+
type: 'text',
|
|
23
|
+
required: true,
|
|
24
|
+
label: { 'en-US': 'Name', 'is-IS': 'Nafn' },
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
id: 'email',
|
|
28
|
+
type: 'email',
|
|
29
|
+
required: true,
|
|
30
|
+
label: { 'en-US': 'Email address', 'is-IS': 'Netfang' },
|
|
31
|
+
placeholder: { 'en-US': 'name@example.com', 'is-IS': 'nafn@example.is' },
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'topic',
|
|
35
|
+
type: 'text',
|
|
36
|
+
required: false,
|
|
37
|
+
label: { 'en-US': 'What is it about?', 'is-IS': 'Um hvað er málið?' },
|
|
38
|
+
validation: { maxLength: 120 },
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: 'orderNumber',
|
|
42
|
+
type: 'text',
|
|
43
|
+
required: false,
|
|
44
|
+
label: { 'en-US': 'Order number', 'is-IS': 'Pöntunarnúmer' },
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: 'message',
|
|
48
|
+
type: 'textarea',
|
|
49
|
+
required: true,
|
|
50
|
+
label: { 'en-US': 'Message', 'is-IS': 'Skilaboð' },
|
|
51
|
+
validation: { minLength: 10, maxLength: 4000 },
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
actions: { notify: true, confirm: true },
|
|
55
|
+
success: {
|
|
56
|
+
message: {
|
|
57
|
+
'en-US': 'Thanks. We reply within one business day, usually sooner.',
|
|
58
|
+
'is-IS': 'Takk fyrir. Við svörum innan eins virks dags, oftast fyrr.',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
key: 'newsletter',
|
|
64
|
+
name: 'Newsletter',
|
|
65
|
+
fields: [
|
|
66
|
+
{
|
|
67
|
+
id: 'email',
|
|
68
|
+
type: 'email',
|
|
69
|
+
required: true,
|
|
70
|
+
label: { 'en-US': 'Email address', 'is-IS': 'Netfang' },
|
|
71
|
+
placeholder: { 'en-US': 'name@example.com', 'is-IS': 'nafn@example.is' },
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
actions: {
|
|
75
|
+
notify: false,
|
|
76
|
+
confirm: false,
|
|
77
|
+
subscribe: {
|
|
78
|
+
list: 'newsletter',
|
|
79
|
+
consentText: {
|
|
80
|
+
'en-US': 'I want to receive the newsletter from {shop}. I can unsubscribe at any time.',
|
|
81
|
+
'is-IS': 'Ég vil fá fréttabréf frá {shop}. Ég get skráð mig úr áskrift hvenær sem er.',
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
success: {
|
|
86
|
+
message: {
|
|
87
|
+
'en-US': 'Check your inbox to confirm your subscription.',
|
|
88
|
+
'is-IS': 'Kíktu í pósthólfið til að staðfesta áskriftina.',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
}
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
import { computed } from 'vue';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
EldraBlockZone,
|
|
5
|
+
EldraLayout,
|
|
6
|
+
getBlockSchemaApiId,
|
|
7
|
+
useEldraLocale,
|
|
8
|
+
} from '@eldrajs/theme-vue';
|
|
4
9
|
import { Button, Container, Section } from '@eldrajs/ui';
|
|
5
10
|
import EldraRouterLink from '../components/EldraRouterLink.vue';
|
|
6
11
|
import { useI18n } from 'vue-i18n';
|
|
7
12
|
import { partitionPageBlocks } from '../utils/pageStructure';
|
|
8
|
-
import {
|
|
13
|
+
import { seoInput } from '../utils/seoInput';
|
|
14
|
+
import { useMoney } from '../storefront/money';
|
|
9
15
|
|
|
10
16
|
const {
|
|
11
17
|
page,
|
|
@@ -41,18 +47,48 @@ const isNotFound = computed(
|
|
|
41
47
|
() => isNotFoundRoute.value || (page.value === null && template.value === null)
|
|
42
48
|
);
|
|
43
49
|
|
|
44
|
-
|
|
45
|
-
|
|
50
|
+
// The title, description, canonical, Open Graph and robots tags come from the platform's SEO
|
|
51
|
+
// settings (Studio, Website and theme) and the route's own `seo` field, resolved by the one
|
|
52
|
+
// resolver Studio's previews share. A catalog route is named by the object it shows — the shared
|
|
53
|
+
// route template's own title names the template ("Product" on every product page) — and the
|
|
54
|
+
// not-found shell names itself and is never indexed. See `docs/theme-seo.md`.
|
|
55
|
+
const money = useMoney();
|
|
56
|
+
const { supported } = useEldraLocale();
|
|
57
|
+
// The product's category name for `product.category`: the deepest level of the trail the product
|
|
58
|
+
// page draws (same cache key as the blocks and `usePageStructuredData`, so no extra read).
|
|
59
|
+
const seoProduct = useStorefront().catalog.product(
|
|
60
|
+
computed(() => (catalog.value?.kind === 'product' ? catalog.value.slug : null))
|
|
61
|
+
).data;
|
|
62
|
+
const seo = computed(() =>
|
|
63
|
+
seoInput({
|
|
64
|
+
productCategory: seoProduct.value?.categoryTrail.at(-1)?.label ?? null,
|
|
46
65
|
isNotFound: isNotFound.value,
|
|
47
66
|
notFoundTitle: t('notFound.title'),
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
})
|
|
67
|
+
catalog: catalog.value,
|
|
68
|
+
entry: entry.value,
|
|
69
|
+
page: page.value,
|
|
70
|
+
template: template.value,
|
|
71
|
+
path: route.path,
|
|
72
|
+
locales: supported,
|
|
73
|
+
formatPrice: money.format,
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
const resolvedSeo = useEldraSeo(
|
|
77
|
+
() => seo.value.context,
|
|
78
|
+
() => seo.value.data,
|
|
79
|
+
() => seo.value.options
|
|
80
|
+
);
|
|
81
|
+
// The schema.org JSON-LD (`Product`, `BreadcrumbList`, `ItemList`, `Organization`, `WebSite`), from
|
|
82
|
+
// the same product, price and stock the page renders; a `noindex` page carries none. See
|
|
83
|
+
// `app/composables/usePageStructuredData.ts` and `docs/theme-seo.md`.
|
|
84
|
+
usePageStructuredData({
|
|
85
|
+
catalog,
|
|
86
|
+
isNotFound,
|
|
87
|
+
isHome: computed(() => seo.value.context === 'home'),
|
|
88
|
+
seo: resolvedSeo,
|
|
89
|
+
title: computed(() => String(seo.value.data['page.title'] ?? '')),
|
|
90
|
+
path: computed(() => route.path),
|
|
91
|
+
});
|
|
56
92
|
|
|
57
93
|
/**
|
|
58
94
|
* The page's flat block list, cut into the three landmark regions — see
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineNuxtPlugin, useAsyncData, useRouter, useRuntimeConfig } from 'nuxt/app';
|
|
2
2
|
import { getCurrentInstance, nextTick, reactive, watchEffect } from 'vue';
|
|
3
|
-
import { createEldraClient, type EldraRetryOptions } from '@eldrajs/sdk';
|
|
3
|
+
import { createEldraClient, type EldraFormDefinition, type EldraRetryOptions } from '@eldrajs/sdk';
|
|
4
4
|
import {
|
|
5
5
|
STOREFRONT_KEY,
|
|
6
6
|
type StorefrontRoute,
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type StorefrontPrerenderHandle,
|
|
14
14
|
type StorefrontRuntime,
|
|
15
15
|
} from '../storefront/gateway';
|
|
16
|
+
import { withAnalytics } from '../storefront/analytics';
|
|
16
17
|
import { createVolatileRefresher } from '../storefront/refresh';
|
|
17
18
|
import {
|
|
18
19
|
prerenderThroughAsyncData,
|
|
@@ -172,6 +173,7 @@ export default defineNuxtPlugin({
|
|
|
172
173
|
orgId?: string;
|
|
173
174
|
commerce?: unknown;
|
|
174
175
|
retry?: EldraRetryOptions | null;
|
|
176
|
+
forms?: Record<string, EldraFormDefinition> | '';
|
|
175
177
|
};
|
|
176
178
|
formsEndpoint?: string;
|
|
177
179
|
};
|
|
@@ -280,9 +282,24 @@ export default defineNuxtPlugin({
|
|
|
280
282
|
? undefined
|
|
281
283
|
: activeLocale.active,
|
|
282
284
|
formsEndpoint: publicConfig.formsEndpoint,
|
|
285
|
+
// The form definitions the build baked (`@eldrajs/theme-nuxt`'s `forms` read); a form the
|
|
286
|
+
// build could not read, or the merchant made after it, is fetched when the page opens.
|
|
287
|
+
formDefinitions:
|
|
288
|
+
typeof publicConfig.eldra?.forms === 'object' ? publicConfig.eldra.forms : undefined,
|
|
289
|
+
// The default locale included: a submission always says which language it was written in,
|
|
290
|
+
// where `locale` above is `undefined` for it so catalogue reads keep their old keys.
|
|
291
|
+
formLocale: () => activeLocale.active ?? activeLocale.defaultLocale ?? undefined,
|
|
283
292
|
runtime,
|
|
284
293
|
});
|
|
285
294
|
|
|
286
|
-
|
|
295
|
+
// Commerce events (`app/storefront/analytics.ts`) go through the tracker `@eldrajs/theme-nuxt`
|
|
296
|
+
// loads on a deployed site; everywhere else (dev, preview, no organisation, opted out)
|
|
297
|
+
// `enabled` is false and the source is provided untouched. The unwrapped `source` stays the one
|
|
298
|
+
// the refresher above reads.
|
|
299
|
+
const analytics = useEldraAnalytics();
|
|
300
|
+
nuxtApp.vueApp.provide(
|
|
301
|
+
STOREFRONT_KEY,
|
|
302
|
+
analytics.enabled ? withAnalytics(source, analytics.track) : source
|
|
303
|
+
);
|
|
287
304
|
},
|
|
288
305
|
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slugs of the pages this theme seeds (`PAGE_SEEDS` in `./templates.ts`, which reads files and
|
|
3
|
+
* so cannot be imported at runtime). The storefront asks it whether a surface exists before it
|
|
4
|
+
* advertises it: the `WebSite` markup names a `SearchAction` only when the `search` page is seeded.
|
|
5
|
+
* `test/seededPages.spec.ts` fails when the two lists drift.
|
|
6
|
+
*/
|
|
7
|
+
export const SEEDED_PAGE_SLUGS: readonly string[] = ['cart', 'wishlist', 'search', 'products'];
|