dc-google-reviews
v0.0.12
Published
Reusable Google Reviews widgets + API helpers for Astro projects.
Maintainers
Readme
Google Reviews for Astro
Reusable Google Reviews components, helpers, and API handler for Astro projects. Combine the exported widgets with the server handler to render ratings client-side while keeping Google Places API calls on the server with KV caching.
Project Layout
/
├── docs/ # Integration notes + QA checklist
├── src/
│ ├── components/google-reviews/ # Widget, badge, inline bootstrapper
│ ├── lib/ # Client + server helpers
│ ├── server/ # API handler export
│ ├── styles/ # Shared CSS bundle
│ └── index.ts # Package entry point
└── package.jsonScripts
| Command | Description |
| --------------- | ------------------------------------- |
| npm run build | Produces the distributable in dist/ |
| npm test | Runs vitest suite for utilities |
| npm run typecheck | Type-checks the package with TypeScript |
npm run buildcopies components/styles intodist/alongside transpiled JS and type declarations so the published package is ready for direct consumption.
Using the Package
npm install dc-google-reviews---
import {
GoogleReviewInlineScript,
GoogleReviewsWidget,
GoogleReviewsBadge,
} from 'dc-google-reviews';
import 'dc-google-reviews/styles.css';
---
<GoogleReviewInlineScript />
<GoogleReviewsWidget
placeId="YOUR_GOOGLE_PLACE_ID"
businessName="Your Business"
fallbackRating={4.9}
fallbackReviewCount={237}
/><!-- ... -->
<GoogleReviewsBadge
placeId="YOUR_GOOGLE_PLACE_ID"
businessName="Your Business"
fallbackRating={4.9}
/>- Multiple locations: render multiple
<GoogleReviewsWidget>instances, each with its ownplaceId(often sourced fromimport.meta.envper site). - Customise CTA copy via the
ctaTextprop ({count}and{label}tokens supported). - Bundle the shared stylesheet once per project (see import above) to receive the default Google styling.
API Route
Create src/pages/api/google-reviews.ts in your Astro site:
import { googleReviewsHandler } from 'dc-google-reviews';
export const prerender = false;
export const GET = googleReviewsHandler;Environment & Infra Checklist
- Create a server-side Google Places API key with the Places API (New) enabled. Lock it down to your hosting IP/compute provider with Google Cloud restrictions.
- Set
GOOGLE_PLACES_API_KEY,GOOGLE_PLACES_DEFAULT_PLACE_ID,GOOGLE_REVIEWS_CACHE_TTL, andCRON_SECRETin.env(and mirror them in your hosting platform, e.g. Vercel). Never embed the API key in client bundles. - Attach Vercel KV so cached payloads persist across deployments.
- Schedule a Vercel cron hitting
https://<deploy-domain>/api/google-reviews?placeId=<ID>&force=truewithAuthorization: Bearer <CRON_SECRET>for daily refreshes. - Provide
fallbackRating/fallbackReviewCountvalues during builds to avoid UI flicker on first paint.
Detailed architecture, prop docs, and QA steps live in docs/google-reviews.md.
