create-brainerce-store 1.34.5 → 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 +1 -1
- package/package.json +1 -1
- package/templates/nextjs/base/src/app/products/[slug]/page.tsx +2 -0
- package/templates/nextjs/base/src/components/reviews/review-form.tsx +293 -0
- package/templates/nextjs/base/src/components/reviews/reviews-section.tsx +85 -0
- package/templates/nextjs/base/src/components/seo/product-json-ld.tsx +116 -104
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
|
@@ -2,6 +2,7 @@ import type { Metadata } from 'next';
|
|
|
2
2
|
import { notFound } from 'next/navigation';
|
|
3
3
|
import { getServerClient } from '@/lib/brainerce';
|
|
4
4
|
import { ProductJsonLd } from '@/components/seo/product-json-ld';
|
|
5
|
+
import { ReviewsSection } from '@/components/reviews/reviews-section';
|
|
5
6
|
import { ProductClientSection } from './product-client-section';
|
|
6
7
|
|
|
7
8
|
type Props = {
|
|
@@ -68,6 +69,7 @@ export default async function ProductDetailPage({ params }: Props) {
|
|
|
68
69
|
<>
|
|
69
70
|
<ProductJsonLd product={product} url={productUrl} currency={currency} />
|
|
70
71
|
<ProductClientSection product={product} />
|
|
72
|
+
<ReviewsSection productId={product.id} />
|
|
71
73
|
</>
|
|
72
74
|
);
|
|
73
75
|
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
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';
|
|
7
|
+
|
|
8
|
+
interface ReviewFormProps {
|
|
9
|
+
productId: string;
|
|
10
|
+
}
|
|
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
|
+
*/
|
|
27
|
+
export function ReviewForm({ productId }: ReviewFormProps) {
|
|
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);
|
|
191
|
+
const [submitting, setSubmitting] = useState(false);
|
|
192
|
+
const [error, setError] = useState<string | null>(null);
|
|
193
|
+
const [success, setSuccess] = useState(false);
|
|
194
|
+
|
|
195
|
+
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
196
|
+
event.preventDefault();
|
|
197
|
+
setError(null);
|
|
198
|
+
setSubmitting(true);
|
|
199
|
+
try {
|
|
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
|
+
}
|
|
207
|
+
setSuccess(true);
|
|
208
|
+
setTimeout(() => window.location.reload(), 1200);
|
|
209
|
+
} catch (err) {
|
|
210
|
+
const status = (err as { status?: number })?.status;
|
|
211
|
+
if (status === 409) {
|
|
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.');
|
|
215
|
+
} else if (status === 429) {
|
|
216
|
+
setError('Too many submissions. Please try again in a few minutes.');
|
|
217
|
+
} else {
|
|
218
|
+
setError(
|
|
219
|
+
mode === 'edit'
|
|
220
|
+
? 'Could not update your review. Please try again.'
|
|
221
|
+
: 'Could not submit your review. Please try again.'
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
} finally {
|
|
225
|
+
setSubmitting(false);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (success) {
|
|
230
|
+
return (
|
|
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.'}
|
|
235
|
+
</p>
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return (
|
|
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>
|
|
242
|
+
|
|
243
|
+
<fieldset>
|
|
244
|
+
<legend className="mb-1 text-sm">Your rating</legend>
|
|
245
|
+
<div className="inline-flex gap-1" role="radiogroup" aria-label="Star rating">
|
|
246
|
+
{[1, 2, 3, 4, 5].map((n) => (
|
|
247
|
+
<button
|
|
248
|
+
key={n}
|
|
249
|
+
type="button"
|
|
250
|
+
role="radio"
|
|
251
|
+
aria-checked={rating === n}
|
|
252
|
+
onClick={() => setRating(n)}
|
|
253
|
+
className={`text-2xl leading-none ${n <= rating ? 'text-yellow-500' : 'text-gray-300'}`}
|
|
254
|
+
>
|
|
255
|
+
★
|
|
256
|
+
</button>
|
|
257
|
+
))}
|
|
258
|
+
</div>
|
|
259
|
+
</fieldset>
|
|
260
|
+
|
|
261
|
+
<label className="block text-sm">
|
|
262
|
+
Your review <span className="text-muted-foreground text-xs">(optional)</span>
|
|
263
|
+
<textarea
|
|
264
|
+
value={body}
|
|
265
|
+
onChange={(event) => setBody(event.target.value)}
|
|
266
|
+
maxLength={5000}
|
|
267
|
+
rows={4}
|
|
268
|
+
className="mt-1 block w-full rounded border px-2 py-1.5 text-sm"
|
|
269
|
+
/>
|
|
270
|
+
</label>
|
|
271
|
+
|
|
272
|
+
{error && (
|
|
273
|
+
<p role="alert" className="text-sm text-red-700">
|
|
274
|
+
{error}
|
|
275
|
+
</p>
|
|
276
|
+
)}
|
|
277
|
+
|
|
278
|
+
<button
|
|
279
|
+
type="submit"
|
|
280
|
+
disabled={submitting}
|
|
281
|
+
className="inline-flex items-center rounded bg-black px-4 py-2 text-sm text-white disabled:opacity-50"
|
|
282
|
+
>
|
|
283
|
+
{submitting
|
|
284
|
+
? mode === 'edit'
|
|
285
|
+
? 'Saving…'
|
|
286
|
+
: 'Submitting…'
|
|
287
|
+
: mode === 'edit'
|
|
288
|
+
? 'Save changes'
|
|
289
|
+
: 'Submit review'}
|
|
290
|
+
</button>
|
|
291
|
+
</form>
|
|
292
|
+
);
|
|
293
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { ProductReview } from 'brainerce';
|
|
2
|
+
import { getServerClient } from '@/lib/brainerce';
|
|
3
|
+
import { ReviewForm } from './review-form';
|
|
4
|
+
|
|
5
|
+
interface ReviewsSectionProps {
|
|
6
|
+
productId: string;
|
|
7
|
+
initialReviews?: ProductReview[];
|
|
8
|
+
/** Where to send users back to after submit (defaults to the current product URL). */
|
|
9
|
+
returnUrl?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Renders the visible reviews for a product plus a submit form.
|
|
14
|
+
*
|
|
15
|
+
* Server component — fetches with the SDK on the server so reviews are in
|
|
16
|
+
* the initial HTML payload (good for SEO; JSON-LD on the same page already
|
|
17
|
+
* carries `aggregateRating` when reviewCount > 0).
|
|
18
|
+
*/
|
|
19
|
+
export async function ReviewsSection({ productId, initialReviews }: ReviewsSectionProps) {
|
|
20
|
+
let reviews: ProductReview[] = initialReviews ?? [];
|
|
21
|
+
|
|
22
|
+
if (!initialReviews) {
|
|
23
|
+
try {
|
|
24
|
+
const result = await getServerClient().listProductReviews(productId, { page: 1, limit: 20 });
|
|
25
|
+
reviews = result.data;
|
|
26
|
+
} catch {
|
|
27
|
+
// Reviews disabled at store level or network error — render the form anyway.
|
|
28
|
+
reviews = [];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<section className="mt-8" aria-labelledby="reviews-heading">
|
|
34
|
+
<h2 id="reviews-heading" className="mb-4 text-xl font-semibold">
|
|
35
|
+
Reviews
|
|
36
|
+
</h2>
|
|
37
|
+
|
|
38
|
+
{reviews.length === 0 ? (
|
|
39
|
+
<p className="text-muted-foreground mb-6 text-sm">
|
|
40
|
+
No reviews yet — be the first to share your experience.
|
|
41
|
+
</p>
|
|
42
|
+
) : (
|
|
43
|
+
<ul className="mb-6 space-y-4">
|
|
44
|
+
{reviews.map((review) => (
|
|
45
|
+
<ReviewCard key={review.id} review={review} />
|
|
46
|
+
))}
|
|
47
|
+
</ul>
|
|
48
|
+
)}
|
|
49
|
+
|
|
50
|
+
<ReviewForm productId={productId} />
|
|
51
|
+
</section>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function ReviewCard({ review }: { review: ProductReview }) {
|
|
56
|
+
return (
|
|
57
|
+
<li className="rounded-md border p-4">
|
|
58
|
+
<header className="flex flex-wrap items-center gap-2">
|
|
59
|
+
<Stars rating={review.rating} />
|
|
60
|
+
<span className="text-sm font-medium">{review.authorName}</span>
|
|
61
|
+
{review.verifiedPurchase && (
|
|
62
|
+
<span className="rounded bg-emerald-50 px-2 py-0.5 text-xs text-emerald-700">
|
|
63
|
+
Verified purchase
|
|
64
|
+
</span>
|
|
65
|
+
)}
|
|
66
|
+
<time className="text-muted-foreground ms-auto text-xs">
|
|
67
|
+
{new Date(review.createdAt).toLocaleDateString()}
|
|
68
|
+
</time>
|
|
69
|
+
</header>
|
|
70
|
+
{review.body && <p className="mt-2 whitespace-pre-line break-words text-sm">{review.body}</p>}
|
|
71
|
+
</li>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function Stars({ rating }: { rating: number }) {
|
|
76
|
+
return (
|
|
77
|
+
<span aria-label={`${rating} of 5 stars`} className="inline-flex">
|
|
78
|
+
{[1, 2, 3, 4, 5].map((n) => (
|
|
79
|
+
<span key={n} className={n <= rating ? 'text-yellow-500' : 'text-gray-300'}>
|
|
80
|
+
★
|
|
81
|
+
</span>
|
|
82
|
+
))}
|
|
83
|
+
</span>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
@@ -1,104 +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 = {
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
{
|
|
70
|
-
'@type': 'ListItem',
|
|
71
|
-
position:
|
|
72
|
-
name:
|
|
73
|
-
item:
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
+
}
|