@amulet-laboratories/rig-nuxt 0.3.1 → 0.4.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.
@@ -1,4 +1,5 @@
1
1
  <script setup lang="ts">
2
+ import { computed } from 'vue'
2
3
  import { useAsyncData } from '#imports'
3
4
  import { useFathom } from '@amulet-laboratories/rig'
4
5
  import { useProducts } from '../../composables/useProducts'
@@ -14,6 +15,50 @@ const { data: product, status } = await useAsyncData(`product-${props.slug}`, ()
14
15
 
15
16
  const { trackAffiliateClick } = useFathom()
16
17
 
18
+ interface Retailer {
19
+ name: string
20
+ url: string
21
+ commission_rate: string
22
+ }
23
+
24
+ const bestRetailer = computed<Retailer | null>(() => {
25
+ if (!product.value) return null
26
+
27
+ const candidates: Retailer[] = []
28
+
29
+ if (product.value.amazon?.url) {
30
+ candidates.push({
31
+ name: 'Amazon',
32
+ url: product.value.amazon.url,
33
+ commission_rate: product.value.amazon.commission_rate || '0%',
34
+ })
35
+ }
36
+
37
+ if (product.value.alt_retailers?.length) {
38
+ for (const retailer of product.value.alt_retailers) {
39
+ if (retailer.url) candidates.push(retailer as Retailer)
40
+ }
41
+ }
42
+
43
+ if (!candidates.length) return null
44
+
45
+ return candidates.sort((a, b) => {
46
+ const rateA = parseFloat(a.commission_rate) || 0
47
+ const rateB = parseFloat(b.commission_rate) || 0
48
+ return rateB - rateA
49
+ })[0]
50
+ })
51
+
52
+ const amazonFallback = computed<Retailer | null>(() => {
53
+ if (!product.value?.amazon?.url) return null
54
+ if (bestRetailer.value?.name === 'Amazon') return null
55
+ return {
56
+ name: 'Amazon',
57
+ url: product.value.amazon.url,
58
+ commission_rate: product.value.amazon.commission_rate || '0%',
59
+ }
60
+ })
61
+
17
62
  function onAffiliateClick() {
18
63
  trackAffiliateClick(props.slug)
19
64
  }
@@ -66,13 +111,24 @@ function onAffiliateClick() {
66
111
  </div>
67
112
  <div data-rig-product-card-footer>
68
113
  <a
69
- :href="product.amazon?.url"
114
+ v-if="bestRetailer"
115
+ :href="bestRetailer.url"
70
116
  target="_blank"
71
117
  rel="nofollow noopener sponsored"
72
118
  data-rig-product-card-link
73
119
  @click="onAffiliateClick"
74
120
  >
75
- Check price on Amazon
121
+ Check price on {{ bestRetailer.name }}
122
+ </a>
123
+ <a
124
+ v-if="amazonFallback"
125
+ :href="amazonFallback.url"
126
+ target="_blank"
127
+ rel="nofollow noopener sponsored"
128
+ data-rig-product-card-link-secondary
129
+ @click="onAffiliateClick"
130
+ >
131
+ Also on Amazon
76
132
  </a>
77
133
  </div>
78
134
  </div>
@@ -23,7 +23,12 @@ const otherSites = computed(() => props.sites.filter((s) => s.name !== currentSi
23
23
  <p data-rig-network-footer-label>From our network</p>
24
24
  <ul data-rig-network-footer-list>
25
25
  <li v-for="site in otherSites" :key="site.name">
26
- <a :href="site.url" target="_blank" rel="noopener" data-rig-network-footer-link>
26
+ <a
27
+ :href="site.url"
28
+ target="_blank"
29
+ rel="noopener noreferrer"
30
+ data-rig-network-footer-link
31
+ >
27
32
  <span data-rig-network-footer-name>{{ site.name }}</span>
28
33
  <span data-rig-network-footer-tagline>{{ site.tagline }}</span>
29
34
  </a>
@@ -1,3 +1,5 @@
1
+ import { useRuntimeConfig, useRoute, useSeoMeta, useHead } from 'nuxt/app'
2
+
1
3
  export interface SeoMetaOptions {
2
4
  title: string
3
5
  description?: string
@@ -7,7 +9,9 @@ export interface SeoMetaOptions {
7
9
  }
8
10
 
9
11
  export const useArticleSeo = (options: SeoMetaOptions) => {
10
- const { name: siteName, url: siteUrl } = useSiteConfig()
12
+ const config = useRuntimeConfig()
13
+ const siteUrl = (config.public?.siteUrl as string) || ''
14
+ const siteName = (config.public?.siteName as string) || ''
11
15
  const route = useRoute()
12
16
  const canonicalUrl = `${siteUrl}${route.path}`
13
17
  const ogImage = options.ogImage || `${siteUrl}/og-default.svg`
@@ -1,3 +1,15 @@
1
+ import { useRuntimeConfig, useRoute, useHead } from 'nuxt/app'
2
+
3
+ export interface StructuredDataProduct {
4
+ slug: string
5
+ name: string
6
+ brand?: string
7
+ image_url?: string
8
+ rating?: number
9
+ price_range?: string
10
+ amazon?: { url?: string }
11
+ }
12
+
1
13
  export interface StructuredDataPage {
2
14
  title: string
3
15
  description: string
@@ -10,14 +22,59 @@ export interface StructuredDataPage {
10
22
  width: number
11
23
  height: number
12
24
  }
25
+ /** One of: 'Article' | 'HowTo' | 'Review' | 'ItemList' (auto-picked if products present). */
13
26
  schema?: string
14
27
  rating?: number
15
28
  price?: string
16
29
  faq?: Array<{ question: string; answer: string }>
30
+ /**
31
+ * Affiliate products referenced by the article. When passed, emits real
32
+ * Product entities inside the appropriate parent schema (Review for a single
33
+ * primary product, ItemList for roundups). Single biggest organic-CTR
34
+ * multiplier for affiliate content — Google surfaces star ratings + prices
35
+ * in the SERP when these are present.
36
+ */
37
+ products?: StructuredDataProduct[]
38
+ }
39
+
40
+ function productSchema(p: StructuredDataProduct, siteUrl: string) {
41
+ const node: Record<string, unknown> = {
42
+ '@type': 'Product',
43
+ name: p.name,
44
+ }
45
+ if (p.brand) node.brand = { '@type': 'Brand', name: p.brand }
46
+ if (p.image_url)
47
+ node.image = p.image_url.startsWith('http') ? p.image_url : `${siteUrl}${p.image_url}`
48
+ if (p.rating) {
49
+ node.aggregateRating = {
50
+ '@type': 'AggregateRating',
51
+ ratingValue: p.rating,
52
+ bestRating: 5,
53
+ ratingCount: 1,
54
+ }
55
+ }
56
+ if (p.amazon?.url) {
57
+ node.offers = {
58
+ '@type': 'Offer',
59
+ url: p.amazon.url,
60
+ priceCurrency: 'USD',
61
+ ...(p.price_range ? { price: extractMinPrice(p.price_range) } : {}),
62
+ availability: 'https://schema.org/InStock',
63
+ }
64
+ }
65
+ return node
66
+ }
67
+
68
+ function extractMinPrice(range: string): string | undefined {
69
+ // "$169-$199" -> "169"
70
+ const m = range.match(/\$?(\d+(?:\.\d+)?)/)
71
+ return m ? m[1] : undefined
17
72
  }
18
73
 
19
74
  export const useStructuredData = (page: StructuredDataPage) => {
20
- const { name: siteName, url: siteUrl } = useSiteConfig()
75
+ const config = useRuntimeConfig()
76
+ const siteUrl = (config.public?.siteUrl as string) || ''
77
+ const siteName = (config.public?.siteName as string) || ''
21
78
  const route = useRoute()
22
79
  const url = `${siteUrl}${route.path}`
23
80
 
@@ -54,33 +111,51 @@ export const useStructuredData = (page: StructuredDataPage) => {
54
111
  baseData.dateModified = page.updatedAt
55
112
  }
56
113
 
57
- let schemaType = 'Article'
114
+ // Auto-pick schema: explicit > products-driven > Article default.
115
+ // ≥2 products on an article without explicit schema → ItemList (roundup).
116
+ // 1 product or explicit Review → Review.
117
+ let schemaType = page.schema ?? 'Article'
118
+ const productCount = page.products?.length ?? 0
119
+ if (!page.schema && productCount >= 2) schemaType = 'ItemList'
58
120
 
59
- switch (page.schema) {
60
- case 'Review':
61
- schemaType = 'Review'
62
- baseData.itemReviewed = {
63
- '@type': 'Product',
64
- name: page.title,
65
- }
66
- if (page.rating) {
121
+ switch (schemaType) {
122
+ case 'Review': {
123
+ baseData['@type'] = 'Review'
124
+ const primary = page.products?.[0]
125
+ baseData.itemReviewed = primary
126
+ ? productSchema(primary, siteUrl)
127
+ : { '@type': 'Product', name: page.title }
128
+ const rating = page.rating ?? primary?.rating
129
+ if (rating) {
67
130
  baseData.reviewRating = {
68
131
  '@type': 'Rating',
69
- ratingValue: page.rating,
132
+ ratingValue: rating,
70
133
  bestRating: 5,
71
134
  }
72
135
  }
73
136
  break
74
- case 'HowTo':
75
- schemaType = 'HowTo'
137
+ }
138
+ case 'ItemList': {
139
+ baseData['@type'] = 'ItemList'
140
+ baseData.name = page.title
141
+ baseData.numberOfItems = productCount
142
+ baseData.itemListElement = (page.products ?? []).map((p, i) => ({
143
+ '@type': 'ListItem',
144
+ position: i + 1,
145
+ item: productSchema(p, siteUrl),
146
+ }))
147
+ break
148
+ }
149
+ case 'HowTo': {
150
+ baseData['@type'] = 'HowTo'
76
151
  baseData.name = page.title
77
152
  break
78
- default:
79
- schemaType = 'Article'
153
+ }
154
+ default: {
155
+ baseData['@type'] = 'Article'
156
+ }
80
157
  }
81
158
 
82
- baseData['@type'] = schemaType
83
-
84
159
  const scripts: Array<{ type: string; innerHTML: string }> = [
85
160
  {
86
161
  type: 'application/ld+json',
@@ -1,3 +1,5 @@
1
+ import { defineNuxtPlugin, useRuntimeConfig, useHead } from 'nuxt/app'
2
+
1
3
  export default defineNuxtPlugin(() => {
2
4
  const config = useRuntimeConfig()
3
5
  const siteId = config.public.fathomSiteId as string
@@ -1,4 +1,5 @@
1
1
  import * as Sentry from '@sentry/vue'
2
+ import { defineNuxtPlugin, useRuntimeConfig } from 'nuxt/app'
2
3
 
3
4
  export default defineNuxtPlugin((nuxtApp) => {
4
5
  const config = useRuntimeConfig()
@@ -1,4 +1,5 @@
1
1
  import { defineEventHandler, readBody, createError } from 'h3'
2
+ import { useRuntimeConfig } from 'nitropack/runtime'
2
3
 
3
4
  export default defineEventHandler(async (event) => {
4
5
  const body = await readBody(event)
@@ -1,4 +1,6 @@
1
1
  import { defineEventHandler } from 'h3'
2
+ import { useRuntimeConfig } from 'nitropack/runtime'
3
+ import { queryCollection } from '@nuxt/content/nitro'
2
4
 
3
5
  export default defineEventHandler(async (event) => {
4
6
  const config = useRuntimeConfig()
@@ -1,6 +1,9 @@
1
1
  import { readFileSync, readdirSync, existsSync } from 'node:fs'
2
2
  import { join, resolve } from 'node:path'
3
3
  import { parse } from 'yaml'
4
+ import { defineEventHandler } from 'h3'
5
+ import { useRuntimeConfig } from 'nitropack/runtime'
6
+ import { queryCollection } from '@nuxt/content/nitro'
4
7
 
5
8
  export default defineEventHandler(async (event) => {
6
9
  const config = useRuntimeConfig()
@@ -1,16 +1,43 @@
1
+ import { useRuntimeConfig } from 'nitropack/runtime'
2
+
3
+ const PLACEHOLDER_TAG = 'YOURTAG-20'
4
+ let warnedMissingTag = false
5
+
1
6
  /**
2
- * Replaces placeholder affiliate tags in product Amazon URLs
3
- * with the real tag from environment config.
7
+ * Replaces the placeholder affiliate tag in a product's Amazon URL with the
8
+ * real per-site tag from runtime config (NUXT_AMAZON_AFFILIATE_TAG).
9
+ *
10
+ * When no tag is configured, the placeholder is STRIPPED rather than shipped:
11
+ * a live URL containing `tag=YOURTAG-20` earns no commission and reads as
12
+ * broken, so we emit a clean (un-attributed) Amazon link — and warn once in
13
+ * dev — instead of leaking the placeholder to readers.
4
14
  */
5
15
  export function injectAffiliateTag(product) {
6
- const config = useRuntimeConfig()
7
- const amazonTag = config.amazonAffiliateTag || ''
16
+ const amazonTag = useRuntimeConfig().amazonAffiliateTag || ''
17
+ const url = product.amazon?.url
18
+
19
+ if (!url || !url.includes(PLACEHOLDER_TAG)) return product
8
20
 
9
- if (!amazonTag || !product.amazon) return product
21
+ const amazon = { ...product.amazon }
10
22
 
11
- const amazon = product.amazon
12
- if (amazon.url && amazon.url.includes('YOURTAG-20')) {
13
- amazon.url = amazon.url.replace('YOURTAG-20', amazonTag)
23
+ if (amazonTag) {
24
+ amazon.url = url.replace(PLACEHOLDER_TAG, amazonTag)
25
+ } else {
26
+ // No NUXT_AMAZON_AFFILIATE_TAG set — never expose the literal placeholder.
27
+ try {
28
+ const cleaned = new URL(url)
29
+ cleaned.searchParams.delete('tag')
30
+ amazon.url = cleaned.toString()
31
+ } catch {
32
+ amazon.url = url.replace(PLACEHOLDER_TAG, '')
33
+ }
34
+ if (!warnedMissingTag && process.env.NODE_ENV !== 'production') {
35
+ warnedMissingTag = true
36
+ // eslint-disable-next-line no-console
37
+ console.warn(
38
+ '[injectAffiliateTag] NUXT_AMAZON_AFFILIATE_TAG is not set — Amazon links render un-attributed (no commission). Set a per-site tag before production.',
39
+ )
40
+ }
14
41
  }
15
42
 
16
43
  return { ...product, amazon }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025-present Amulet Laboratories
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.