create-brainerce-store 1.35.0 → 1.36.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/index.js
CHANGED
|
@@ -31,7 +31,7 @@ var require_package = __commonJS({
|
|
|
31
31
|
"package.json"(exports2, module2) {
|
|
32
32
|
module2.exports = {
|
|
33
33
|
name: "create-brainerce-store",
|
|
34
|
-
version: "1.
|
|
34
|
+
version: "1.36.0",
|
|
35
35
|
description: "Scaffold a production-ready e-commerce storefront connected to Brainerce",
|
|
36
36
|
bin: {
|
|
37
37
|
"create-brainerce-store": "dist/index.js"
|
package/package.json
CHANGED
|
@@ -1,17 +1,193 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import {
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
import { getClient } from '@/lib/brainerce';
|
|
5
|
+
import { checkAuthStatus } from '@/lib/auth';
|
|
6
|
+
import type { MyProductReview, ProductReview } from 'brainerce';
|
|
5
7
|
|
|
6
8
|
interface ReviewFormProps {
|
|
7
9
|
productId: string;
|
|
8
10
|
}
|
|
9
11
|
|
|
12
|
+
type Stage =
|
|
13
|
+
| { kind: 'loading' }
|
|
14
|
+
| { kind: 'signed_out' }
|
|
15
|
+
| { kind: 'not_eligible'; reason: MyProductReview['reason'] }
|
|
16
|
+
| { kind: 'submit' }
|
|
17
|
+
| { kind: 'edit'; review: ProductReview };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Single component that decides which UI to render for a customer on a PDP:
|
|
21
|
+
* - Loading while we check auth state and eligibility
|
|
22
|
+
* - Signed out -> sign-in CTA
|
|
23
|
+
* - Signed in but not eligible -> "purchase this product to review"
|
|
24
|
+
* - Eligible without review -> submit form
|
|
25
|
+
* - Eligible with existing review -> edit/delete form (rating + body prefilled)
|
|
26
|
+
*/
|
|
10
27
|
export function ReviewForm({ productId }: ReviewFormProps) {
|
|
11
|
-
const [
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
28
|
+
const [stage, setStage] = useState<Stage>({ kind: 'loading' });
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
let cancelled = false;
|
|
32
|
+
async function load() {
|
|
33
|
+
const auth = await checkAuthStatus();
|
|
34
|
+
if (cancelled) return;
|
|
35
|
+
if (!auth.isLoggedIn) {
|
|
36
|
+
setStage({ kind: 'signed_out' });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const me = await getClient().getMyProductReview(productId);
|
|
41
|
+
if (cancelled) return;
|
|
42
|
+
if (!me.eligible) {
|
|
43
|
+
setStage({ kind: 'not_eligible', reason: me.reason });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (me.myReview) {
|
|
47
|
+
setStage({ kind: 'edit', review: me.myReview });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
setStage({ kind: 'submit' });
|
|
51
|
+
} catch {
|
|
52
|
+
if (!cancelled) setStage({ kind: 'not_eligible', reason: 'product_not_found' });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
void load();
|
|
56
|
+
return () => {
|
|
57
|
+
cancelled = true;
|
|
58
|
+
};
|
|
59
|
+
}, [productId]);
|
|
60
|
+
|
|
61
|
+
if (stage.kind === 'loading') {
|
|
62
|
+
return <div className="text-muted-foreground text-sm">Loading…</div>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (stage.kind === 'signed_out') {
|
|
66
|
+
return (
|
|
67
|
+
<div className="rounded-md border p-4">
|
|
68
|
+
<p className="text-sm">
|
|
69
|
+
<a href="/account/login" className="font-medium underline">
|
|
70
|
+
Sign in
|
|
71
|
+
</a>{' '}
|
|
72
|
+
to leave a review for this product.
|
|
73
|
+
</p>
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (stage.kind === 'not_eligible') {
|
|
79
|
+
return <NotEligibleMessage reason={stage.reason} />;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (stage.kind === 'edit') {
|
|
83
|
+
return (
|
|
84
|
+
<EditReviewBlock
|
|
85
|
+
productId={productId}
|
|
86
|
+
initial={stage.review}
|
|
87
|
+
onDeleted={() => setStage({ kind: 'submit' })}
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return <SubmitReviewBlock productId={productId} />;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function NotEligibleMessage({ reason }: { reason: MyProductReview['reason'] }) {
|
|
96
|
+
let message: string;
|
|
97
|
+
switch (reason) {
|
|
98
|
+
case 'no_eligible_order':
|
|
99
|
+
message = 'Only customers who purchased this product can leave a review.';
|
|
100
|
+
break;
|
|
101
|
+
case 'reviews_disabled':
|
|
102
|
+
message = 'Reviews are not enabled for this store.';
|
|
103
|
+
break;
|
|
104
|
+
default:
|
|
105
|
+
message = 'You cannot review this product right now.';
|
|
106
|
+
}
|
|
107
|
+
return (
|
|
108
|
+
<div className="bg-muted/30 rounded-md border p-4">
|
|
109
|
+
<p className="text-muted-foreground text-sm">{message}</p>
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function SubmitReviewBlock({ productId }: { productId: string }) {
|
|
115
|
+
return <ReviewEditor productId={productId} mode="create" initialRating={5} initialBody="" />;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function EditReviewBlock({
|
|
119
|
+
productId,
|
|
120
|
+
initial,
|
|
121
|
+
onDeleted,
|
|
122
|
+
}: {
|
|
123
|
+
productId: string;
|
|
124
|
+
initial: ProductReview;
|
|
125
|
+
onDeleted: () => void;
|
|
126
|
+
}) {
|
|
127
|
+
const [deleting, setDeleting] = useState(false);
|
|
128
|
+
const [deleteError, setDeleteError] = useState<string | null>(null);
|
|
129
|
+
|
|
130
|
+
async function handleDelete() {
|
|
131
|
+
if (!window.confirm('Delete your review for this product?')) return;
|
|
132
|
+
setDeleting(true);
|
|
133
|
+
setDeleteError(null);
|
|
134
|
+
try {
|
|
135
|
+
await getClient().deleteMyProductReview(productId);
|
|
136
|
+
onDeleted();
|
|
137
|
+
window.location.reload();
|
|
138
|
+
} catch {
|
|
139
|
+
setDeleteError('Could not delete your review. Please try again.');
|
|
140
|
+
} finally {
|
|
141
|
+
setDeleting(false);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<div className="space-y-3">
|
|
147
|
+
<ReviewEditor
|
|
148
|
+
productId={productId}
|
|
149
|
+
mode="edit"
|
|
150
|
+
initialRating={initial.rating}
|
|
151
|
+
initialBody={initial.body ?? ''}
|
|
152
|
+
/>
|
|
153
|
+
<div className="flex items-center justify-between rounded-md border border-red-100 bg-red-50/50 p-3">
|
|
154
|
+
<p className="text-xs text-red-700">Want to start over? You can delete your review.</p>
|
|
155
|
+
<button
|
|
156
|
+
type="button"
|
|
157
|
+
onClick={handleDelete}
|
|
158
|
+
disabled={deleting}
|
|
159
|
+
className="text-xs font-medium text-red-700 underline disabled:opacity-50"
|
|
160
|
+
>
|
|
161
|
+
{deleting ? 'Deleting…' : 'Delete review'}
|
|
162
|
+
</button>
|
|
163
|
+
</div>
|
|
164
|
+
{deleteError && (
|
|
165
|
+
<p role="alert" className="text-sm text-red-700">
|
|
166
|
+
{deleteError}
|
|
167
|
+
</p>
|
|
168
|
+
)}
|
|
169
|
+
</div>
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The actual form. Same shape for create and edit — only the API method
|
|
175
|
+
* and the heading differ. Optimistic UX: success state + page reload to
|
|
176
|
+
* let the server-rendered reviews list pick up the change.
|
|
177
|
+
*/
|
|
178
|
+
function ReviewEditor({
|
|
179
|
+
productId,
|
|
180
|
+
mode,
|
|
181
|
+
initialRating,
|
|
182
|
+
initialBody,
|
|
183
|
+
}: {
|
|
184
|
+
productId: string;
|
|
185
|
+
mode: 'create' | 'edit';
|
|
186
|
+
initialRating: number;
|
|
187
|
+
initialBody: string;
|
|
188
|
+
}) {
|
|
189
|
+
const [rating, setRating] = useState(initialRating);
|
|
190
|
+
const [body, setBody] = useState(initialBody);
|
|
15
191
|
const [submitting, setSubmitting] = useState(false);
|
|
16
192
|
const [error, setError] = useState<string | null>(null);
|
|
17
193
|
const [success, setSuccess] = useState(false);
|
|
@@ -21,29 +197,29 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
|
|
21
197
|
setError(null);
|
|
22
198
|
setSubmitting(true);
|
|
23
199
|
try {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
200
|
+
const client = getClient();
|
|
201
|
+
const input = { rating, body: body.trim() || undefined };
|
|
202
|
+
if (mode === 'edit') {
|
|
203
|
+
await client.updateMyProductReview(productId, input);
|
|
204
|
+
} else {
|
|
205
|
+
await client.submitProductReview(productId, input);
|
|
206
|
+
}
|
|
30
207
|
setSuccess(true);
|
|
31
|
-
setAuthorName('');
|
|
32
|
-
setAuthorEmail('');
|
|
33
|
-
setBody('');
|
|
34
|
-
setRating(5);
|
|
35
|
-
// The reviews list is server-rendered — refresh to pick up the new review.
|
|
36
208
|
setTimeout(() => window.location.reload(), 1200);
|
|
37
209
|
} catch (err) {
|
|
38
210
|
const status = (err as { status?: number })?.status;
|
|
39
211
|
if (status === 409) {
|
|
40
212
|
setError("You've already submitted a review for this product.");
|
|
213
|
+
} else if (status === 403) {
|
|
214
|
+
setError('Only customers who purchased this product can leave a review.');
|
|
41
215
|
} else if (status === 429) {
|
|
42
216
|
setError('Too many submissions. Please try again in a few minutes.');
|
|
43
|
-
} else if (status === 403) {
|
|
44
|
-
setError('Reviews are not accepted for this product right now.');
|
|
45
217
|
} else {
|
|
46
|
-
setError(
|
|
218
|
+
setError(
|
|
219
|
+
mode === 'edit'
|
|
220
|
+
? 'Could not update your review. Please try again.'
|
|
221
|
+
: 'Could not submit your review. Please try again.'
|
|
222
|
+
);
|
|
47
223
|
}
|
|
48
224
|
} finally {
|
|
49
225
|
setSubmitting(false);
|
|
@@ -52,18 +228,20 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
|
|
52
228
|
|
|
53
229
|
if (success) {
|
|
54
230
|
return (
|
|
55
|
-
<p className="
|
|
56
|
-
|
|
231
|
+
<p className="rounded-md border border-emerald-200 bg-emerald-50 p-3 text-sm text-emerald-700">
|
|
232
|
+
{mode === 'edit'
|
|
233
|
+
? 'Thanks! Your review has been updated.'
|
|
234
|
+
: 'Thanks! Your review has been submitted.'}
|
|
57
235
|
</p>
|
|
58
236
|
);
|
|
59
237
|
}
|
|
60
238
|
|
|
61
239
|
return (
|
|
62
|
-
<form onSubmit={handleSubmit} className="
|
|
63
|
-
<h3 className="font-medium">Write a review</h3>
|
|
240
|
+
<form onSubmit={handleSubmit} className="space-y-3 rounded-md border p-4">
|
|
241
|
+
<h3 className="font-medium">{mode === 'edit' ? 'Edit your review' : 'Write a review'}</h3>
|
|
64
242
|
|
|
65
243
|
<fieldset>
|
|
66
|
-
<legend className="text-sm
|
|
244
|
+
<legend className="mb-1 text-sm">Your rating</legend>
|
|
67
245
|
<div className="inline-flex gap-1" role="radiogroup" aria-label="Star rating">
|
|
68
246
|
{[1, 2, 3, 4, 5].map((n) => (
|
|
69
247
|
<button
|
|
@@ -80,30 +258,6 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
|
|
80
258
|
</div>
|
|
81
259
|
</fieldset>
|
|
82
260
|
|
|
83
|
-
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
|
84
|
-
<label className="block text-sm">
|
|
85
|
-
Name
|
|
86
|
-
<input
|
|
87
|
-
type="text"
|
|
88
|
-
value={authorName}
|
|
89
|
-
onChange={(event) => setAuthorName(event.target.value)}
|
|
90
|
-
required
|
|
91
|
-
maxLength={100}
|
|
92
|
-
className="mt-1 block w-full rounded border px-2 py-1.5 text-sm"
|
|
93
|
-
/>
|
|
94
|
-
</label>
|
|
95
|
-
<label className="block text-sm">
|
|
96
|
-
Email <span className="text-muted-foreground text-xs">(optional)</span>
|
|
97
|
-
<input
|
|
98
|
-
type="email"
|
|
99
|
-
value={authorEmail}
|
|
100
|
-
onChange={(event) => setAuthorEmail(event.target.value)}
|
|
101
|
-
maxLength={200}
|
|
102
|
-
className="mt-1 block w-full rounded border px-2 py-1.5 text-sm"
|
|
103
|
-
/>
|
|
104
|
-
</label>
|
|
105
|
-
</div>
|
|
106
|
-
|
|
107
261
|
<label className="block text-sm">
|
|
108
262
|
Your review <span className="text-muted-foreground text-xs">(optional)</span>
|
|
109
263
|
<textarea
|
|
@@ -123,10 +277,16 @@ export function ReviewForm({ productId }: ReviewFormProps) {
|
|
|
123
277
|
|
|
124
278
|
<button
|
|
125
279
|
type="submit"
|
|
126
|
-
disabled={submitting
|
|
280
|
+
disabled={submitting}
|
|
127
281
|
className="inline-flex items-center rounded bg-black px-4 py-2 text-sm text-white disabled:opacity-50"
|
|
128
282
|
>
|
|
129
|
-
{submitting
|
|
283
|
+
{submitting
|
|
284
|
+
? mode === 'edit'
|
|
285
|
+
? 'Saving…'
|
|
286
|
+
: 'Submitting…'
|
|
287
|
+
: mode === 'edit'
|
|
288
|
+
? 'Save changes'
|
|
289
|
+
: 'Submit review'}
|
|
130
290
|
</button>
|
|
131
291
|
</form>
|
|
132
292
|
);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ProductReview } from 'brainerce';
|
|
2
|
-
import {
|
|
2
|
+
import { getServerClient } from '@/lib/brainerce';
|
|
3
3
|
import { ReviewForm } from './review-form';
|
|
4
4
|
|
|
5
5
|
interface ReviewsSectionProps {
|
|
@@ -21,7 +21,7 @@ export async function ReviewsSection({ productId, initialReviews }: ReviewsSecti
|
|
|
21
21
|
|
|
22
22
|
if (!initialReviews) {
|
|
23
23
|
try {
|
|
24
|
-
const result = await
|
|
24
|
+
const result = await getServerClient().listProductReviews(productId, { page: 1, limit: 20 });
|
|
25
25
|
reviews = result.data;
|
|
26
26
|
} catch {
|
|
27
27
|
// Reviews disabled at store level or network error — render the form anyway.
|
|
@@ -31,16 +31,16 @@ export async function ReviewsSection({ productId, initialReviews }: ReviewsSecti
|
|
|
31
31
|
|
|
32
32
|
return (
|
|
33
33
|
<section className="mt-8" aria-labelledby="reviews-heading">
|
|
34
|
-
<h2 id="reviews-heading" className="text-xl font-semibold
|
|
34
|
+
<h2 id="reviews-heading" className="mb-4 text-xl font-semibold">
|
|
35
35
|
Reviews
|
|
36
36
|
</h2>
|
|
37
37
|
|
|
38
38
|
{reviews.length === 0 ? (
|
|
39
|
-
<p className="text-
|
|
39
|
+
<p className="text-muted-foreground mb-6 text-sm">
|
|
40
40
|
No reviews yet — be the first to share your experience.
|
|
41
41
|
</p>
|
|
42
42
|
) : (
|
|
43
|
-
<ul className="space-y-4
|
|
43
|
+
<ul className="mb-6 space-y-4">
|
|
44
44
|
{reviews.map((review) => (
|
|
45
45
|
<ReviewCard key={review.id} review={review} />
|
|
46
46
|
))}
|
|
@@ -54,22 +54,20 @@ export async function ReviewsSection({ productId, initialReviews }: ReviewsSecti
|
|
|
54
54
|
|
|
55
55
|
function ReviewCard({ review }: { review: ProductReview }) {
|
|
56
56
|
return (
|
|
57
|
-
<li className="
|
|
58
|
-
<header className="flex items-center gap-2
|
|
57
|
+
<li className="rounded-md border p-4">
|
|
58
|
+
<header className="flex flex-wrap items-center gap-2">
|
|
59
59
|
<Stars rating={review.rating} />
|
|
60
|
-
<span className="font-medium
|
|
60
|
+
<span className="text-sm font-medium">{review.authorName}</span>
|
|
61
61
|
{review.verifiedPurchase && (
|
|
62
|
-
<span className="
|
|
62
|
+
<span className="rounded bg-emerald-50 px-2 py-0.5 text-xs text-emerald-700">
|
|
63
63
|
Verified purchase
|
|
64
64
|
</span>
|
|
65
65
|
)}
|
|
66
|
-
<time className="text-
|
|
66
|
+
<time className="text-muted-foreground ms-auto text-xs">
|
|
67
67
|
{new Date(review.createdAt).toLocaleDateString()}
|
|
68
68
|
</time>
|
|
69
69
|
</header>
|
|
70
|
-
{review.body &&
|
|
71
|
-
<p className="mt-2 text-sm whitespace-pre-line break-words">{review.body}</p>
|
|
72
|
-
)}
|
|
70
|
+
{review.body && <p className="mt-2 whitespace-pre-line break-words text-sm">{review.body}</p>}
|
|
73
71
|
</li>
|
|
74
72
|
);
|
|
75
73
|
}
|
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import type { Product } from 'brainerce';
|
|
2
|
-
import { getProductPriceInfo } from 'brainerce';
|
|
3
|
-
import { getNonce } from '@/lib/nonce';
|
|
4
|
-
|
|
5
|
-
interface ProductJsonLdProps {
|
|
6
|
-
product: Product;
|
|
7
|
-
url: string;
|
|
8
|
-
currency?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function ProductJsonLd({ product, url, currency = 'USD' }: ProductJsonLdProps) {
|
|
12
|
-
const nonce = await getNonce();
|
|
13
|
-
const priceInfo = getProductPriceInfo(product);
|
|
14
|
-
const imageUrl = product.images?.[0]?.url;
|
|
15
|
-
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || '';
|
|
16
|
-
|
|
17
|
-
const availability =
|
|
18
|
-
product.inventory?.canPurchase !== false
|
|
19
|
-
? 'https://schema.org/InStock'
|
|
20
|
-
: 'https://schema.org/OutOfStock';
|
|
21
|
-
|
|
22
|
-
const isVariable = product.type === 'VARIABLE';
|
|
23
|
-
const offers =
|
|
24
|
-
isVariable && product.priceMin
|
|
25
|
-
? {
|
|
26
|
-
'@type': 'AggregateOffer',
|
|
27
|
-
lowPrice: product.priceMin,
|
|
28
|
-
highPrice: product.priceMax ?? product.priceMin,
|
|
29
|
-
offerCount: product.variants?.length ?? 1,
|
|
30
|
-
priceCurrency: currency,
|
|
31
|
-
availability,
|
|
32
|
-
url,
|
|
33
|
-
}
|
|
34
|
-
: {
|
|
35
|
-
'@type': 'Offer',
|
|
36
|
-
price: priceInfo.price,
|
|
37
|
-
priceCurrency: currency,
|
|
38
|
-
availability,
|
|
39
|
-
url,
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const productJsonLd: Record<string, unknown> = {
|
|
43
|
-
'@context': 'https://schema.org',
|
|
44
|
-
'@type': 'Product',
|
|
45
|
-
name: product.name,
|
|
46
|
-
description: product.description || product.name,
|
|
47
|
-
image: imageUrl,
|
|
48
|
-
url,
|
|
49
|
-
sku: product.sku || product.id,
|
|
50
|
-
offers,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// Emit aggregateRating only when there is real review data — Google rejects
|
|
54
|
-
// self-serving / faked ratings and stores with 0 reviews shouldn't claim any.
|
|
55
|
-
if (product.reviewCount && product.reviewCount > 0) {
|
|
56
|
-
productJsonLd.aggregateRating = {
|
|
57
|
-
'@type': 'AggregateRating',
|
|
58
|
-
ratingValue: product.avgRating,
|
|
59
|
-
reviewCount: product.reviewCount,
|
|
60
|
-
bestRating: 5,
|
|
61
|
-
worstRating: 1,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const breadcrumbJsonLd = {
|
|
66
|
-
'@context': 'https://schema.org',
|
|
67
|
-
'@type': 'BreadcrumbList',
|
|
68
|
-
itemListElement: [
|
|
69
|
-
{
|
|
70
|
-
'@type': 'ListItem',
|
|
71
|
-
position: 1,
|
|
72
|
-
name: 'Home',
|
|
73
|
-
item: baseUrl || '/',
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
'@type': 'ListItem',
|
|
77
|
-
position: 2,
|
|
78
|
-
name: 'Products',
|
|
79
|
-
item: `${baseUrl}/products`,
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
'@type': 'ListItem',
|
|
83
|
-
position: 3,
|
|
84
|
-
name: product.name,
|
|
85
|
-
item: url,
|
|
86
|
-
},
|
|
87
|
-
],
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
return (
|
|
91
|
-
<>
|
|
92
|
-
<script
|
|
93
|
-
type="application/ld+json"
|
|
94
|
-
nonce={nonce}
|
|
95
|
-
suppressHydrationWarning
|
|
96
|
-
dangerouslySetInnerHTML={{ __html: serializeJsonLd(productJsonLd) }}
|
|
97
|
-
/>
|
|
98
|
-
<script
|
|
99
|
-
type="application/ld+json"
|
|
100
|
-
nonce={nonce}
|
|
101
|
-
suppressHydrationWarning
|
|
102
|
-
dangerouslySetInnerHTML={{ __html: serializeJsonLd(breadcrumbJsonLd) }}
|
|
103
|
-
/>
|
|
104
|
-
</>
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Serialize a JSON-LD object for embedding in a <script> tag. Escapes
|
|
109
|
-
// `<`, `>`, and `&` to \uXXXX so seller-controlled product fields cannot
|
|
110
|
-
// break out of the script element with `</script>` or inject HTML.
|
|
111
|
-
function serializeJsonLd(value: unknown): string {
|
|
112
|
-
return JSON.stringify(value)
|
|
113
|
-
.replace(/</g, '\\u003c')
|
|
114
|
-
.replace(/>/g, '\\u003e')
|
|
115
|
-
.replace(/&/g, '\\u0026');
|
|
116
|
-
}
|
|
1
|
+
import type { Product } from 'brainerce';
|
|
2
|
+
import { getProductPriceInfo } from 'brainerce';
|
|
3
|
+
import { getNonce } from '@/lib/nonce';
|
|
4
|
+
|
|
5
|
+
interface ProductJsonLdProps {
|
|
6
|
+
product: Product;
|
|
7
|
+
url: string;
|
|
8
|
+
currency?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function ProductJsonLd({ product, url, currency = 'USD' }: ProductJsonLdProps) {
|
|
12
|
+
const nonce = await getNonce();
|
|
13
|
+
const priceInfo = getProductPriceInfo(product);
|
|
14
|
+
const imageUrl = product.images?.[0]?.url;
|
|
15
|
+
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || '';
|
|
16
|
+
|
|
17
|
+
const availability =
|
|
18
|
+
product.inventory?.canPurchase !== false
|
|
19
|
+
? 'https://schema.org/InStock'
|
|
20
|
+
: 'https://schema.org/OutOfStock';
|
|
21
|
+
|
|
22
|
+
const isVariable = product.type === 'VARIABLE';
|
|
23
|
+
const offers =
|
|
24
|
+
isVariable && product.priceMin
|
|
25
|
+
? {
|
|
26
|
+
'@type': 'AggregateOffer',
|
|
27
|
+
lowPrice: product.priceMin,
|
|
28
|
+
highPrice: product.priceMax ?? product.priceMin,
|
|
29
|
+
offerCount: product.variants?.length ?? 1,
|
|
30
|
+
priceCurrency: currency,
|
|
31
|
+
availability,
|
|
32
|
+
url,
|
|
33
|
+
}
|
|
34
|
+
: {
|
|
35
|
+
'@type': 'Offer',
|
|
36
|
+
price: priceInfo.price,
|
|
37
|
+
priceCurrency: currency,
|
|
38
|
+
availability,
|
|
39
|
+
url,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const productJsonLd: Record<string, unknown> = {
|
|
43
|
+
'@context': 'https://schema.org',
|
|
44
|
+
'@type': 'Product',
|
|
45
|
+
name: product.name,
|
|
46
|
+
description: product.description || product.name,
|
|
47
|
+
image: imageUrl,
|
|
48
|
+
url,
|
|
49
|
+
sku: product.sku || product.id,
|
|
50
|
+
offers,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Emit aggregateRating only when there is real review data — Google rejects
|
|
54
|
+
// self-serving / faked ratings and stores with 0 reviews shouldn't claim any.
|
|
55
|
+
if (product.reviewCount && product.reviewCount > 0) {
|
|
56
|
+
productJsonLd.aggregateRating = {
|
|
57
|
+
'@type': 'AggregateRating',
|
|
58
|
+
ratingValue: product.avgRating,
|
|
59
|
+
reviewCount: product.reviewCount,
|
|
60
|
+
bestRating: 5,
|
|
61
|
+
worstRating: 1,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const breadcrumbJsonLd = {
|
|
66
|
+
'@context': 'https://schema.org',
|
|
67
|
+
'@type': 'BreadcrumbList',
|
|
68
|
+
itemListElement: [
|
|
69
|
+
{
|
|
70
|
+
'@type': 'ListItem',
|
|
71
|
+
position: 1,
|
|
72
|
+
name: 'Home',
|
|
73
|
+
item: baseUrl || '/',
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
'@type': 'ListItem',
|
|
77
|
+
position: 2,
|
|
78
|
+
name: 'Products',
|
|
79
|
+
item: `${baseUrl}/products`,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
'@type': 'ListItem',
|
|
83
|
+
position: 3,
|
|
84
|
+
name: product.name,
|
|
85
|
+
item: url,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<>
|
|
92
|
+
<script
|
|
93
|
+
type="application/ld+json"
|
|
94
|
+
nonce={nonce}
|
|
95
|
+
suppressHydrationWarning
|
|
96
|
+
dangerouslySetInnerHTML={{ __html: serializeJsonLd(productJsonLd) }}
|
|
97
|
+
/>
|
|
98
|
+
<script
|
|
99
|
+
type="application/ld+json"
|
|
100
|
+
nonce={nonce}
|
|
101
|
+
suppressHydrationWarning
|
|
102
|
+
dangerouslySetInnerHTML={{ __html: serializeJsonLd(breadcrumbJsonLd) }}
|
|
103
|
+
/>
|
|
104
|
+
</>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Serialize a JSON-LD object for embedding in a <script> tag. Escapes
|
|
109
|
+
// `<`, `>`, and `&` to \uXXXX so seller-controlled product fields cannot
|
|
110
|
+
// break out of the script element with `</script>` or inject HTML.
|
|
111
|
+
function serializeJsonLd(value: unknown): string {
|
|
112
|
+
return JSON.stringify(value)
|
|
113
|
+
.replace(/</g, '\\u003c')
|
|
114
|
+
.replace(/>/g, '\\u003e')
|
|
115
|
+
.replace(/&/g, '\\u0026');
|
|
116
|
+
}
|