Skip to content

Commit 47b9f96

Browse files
committed
feat(site): support PUBLIC_API_BASE_URL for Vercel develop previews
Lets a preview build serve develop's UI while reading main's live public dump instead of regenerating its own — no code needed beyond setting the env var on the preview deployment.
1 parent 4c74b3e commit 47b9f96

4 files changed

Lines changed: 27 additions & 19 deletions

File tree

site/astro.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import { defineConfig } from 'astro/config';
44
// the built site in CI, so internal links use import.meta.env.BASE_URL.
55
export default defineConfig({
66
site: 'https://gettechapi.github.io',
7-
base: '/TechAPI',
7+
// Vercel previews serve from the domain root, not the GitHub Pages project path.
8+
base: process.env.VERCEL ? '/' : '/TechAPI',
89
});

site/src/pages/docs.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
const raw = import.meta.env.BASE_URL;
2+
const raw = import.meta.env.PUBLIC_API_BASE_URL || import.meta.env.BASE_URL;
33
const base = raw.endsWith("/") ? raw : raw + "/"; // ensure trailing slash
44
const openapiUrl = `${base}openapi.json`;
55
---

site/src/pages/index.astro

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@ import "../styles/techapi.css";
33
44
const raw = import.meta.env.BASE_URL;
55
const base = raw.endsWith("/") ? raw : raw + "/";
6+
// Data links only: PUBLIC_API_BASE_URL (e.g. a Vercel preview reading main's live data)
7+
// takes over just these, while site nav/assets below keep using `base`.
8+
const apiRaw = import.meta.env.PUBLIC_API_BASE_URL || raw;
9+
const apiBase = apiRaw.endsWith("/") ? apiRaw : apiRaw + "/";
610
const ghUrl = "https://github.com/GetTechAPI/TechAPI";
711
const engineUrl = "https://github.com/GetTechAPI/TechEngine";
812
913
const endpoints = [
10-
{ label: "/v1/smartphones", desc: "List all phones", href: `${base}v1/smartphones/index.json` },
11-
{ label: "/v1/smartphones/{slug}", desc: "Phone detail + scores", href: `${base}v1/smartphones/galaxy-s26-ultra/index.json` },
12-
{ label: "/v1/smartphones/{slug}/score", desc: "Computed scores", href: `${base}v1/smartphones/galaxy-s26-ultra/score/index.json` },
13-
{ label: "/v1/tablets", desc: "List all tablets", href: `${base}v1/tablets/index.json` },
14-
{ label: "/v1/laptops", desc: "List all laptops", href: `${base}v1/laptops/index.json` },
15-
{ label: "/v1/monitors", desc: "List all monitors", href: `${base}v1/monitors/index.json` },
16-
{ label: "/v1/watches", desc: "List all watches", href: `${base}v1/watches/index.json` },
17-
{ label: "/v1/pdas", desc: "List all PDAs", href: `${base}v1/pdas/index.json` },
18-
{ label: "/v1/socs/{slug}", desc: "System-on-chip detail", href: `${base}v1/socs/snapdragon-8-elite-gen-5/index.json` },
19-
{ label: "/v1/gpus/{slug}", desc: "Discrete GPU detail", href: `${base}v1/gpus/geforce-rtx-5090/index.json` },
20-
{ label: "/v1/cpus/{slug}", desc: "Desktop / laptop CPU", href: `${base}v1/cpus/core-i9-14900k/index.json` },
21-
{ label: "/v1/software", desc: "List all software", href: `${base}v1/software/index.json` },
22-
{ label: "/v1/websites", desc: "List all websites", href: `${base}v1/websites/index.json` },
23-
{ label: "/v1/brands", desc: "All brands", href: `${base}v1/brands/index.json` },
14+
{ label: "/v1/smartphones", desc: "List all phones", href: `${apiBase}v1/smartphones/index.json` },
15+
{ label: "/v1/smartphones/{slug}", desc: "Phone detail + scores", href: `${apiBase}v1/smartphones/galaxy-s26-ultra/index.json` },
16+
{ label: "/v1/smartphones/{slug}/score", desc: "Computed scores", href: `${apiBase}v1/smartphones/galaxy-s26-ultra/score/index.json` },
17+
{ label: "/v1/tablets", desc: "List all tablets", href: `${apiBase}v1/tablets/index.json` },
18+
{ label: "/v1/laptops", desc: "List all laptops", href: `${apiBase}v1/laptops/index.json` },
19+
{ label: "/v1/monitors", desc: "List all monitors", href: `${apiBase}v1/monitors/index.json` },
20+
{ label: "/v1/watches", desc: "List all watches", href: `${apiBase}v1/watches/index.json` },
21+
{ label: "/v1/pdas", desc: "List all PDAs", href: `${apiBase}v1/pdas/index.json` },
22+
{ label: "/v1/socs/{slug}", desc: "System-on-chip detail", href: `${apiBase}v1/socs/snapdragon-8-elite-gen-5/index.json` },
23+
{ label: "/v1/gpus/{slug}", desc: "Discrete GPU detail", href: `${apiBase}v1/gpus/geforce-rtx-5090/index.json` },
24+
{ label: "/v1/cpus/{slug}", desc: "Desktop / laptop CPU", href: `${apiBase}v1/cpus/core-i9-14900k/index.json` },
25+
{ label: "/v1/software", desc: "List all software", href: `${apiBase}v1/software/index.json` },
26+
{ label: "/v1/websites", desc: "List all websites", href: `${apiBase}v1/websites/index.json` },
27+
{ label: "/v1/brands", desc: "All brands", href: `${apiBase}v1/brands/index.json` },
2428
];
2529
---
2630

site/src/scripts/techapi.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// TechAPI — homepage interactions (Astro client script)
2-
// Real static-JSON fetch against import.meta.env.BASE_URL (static JSON dump).
3-
const raw = import.meta.env.BASE_URL;
2+
// Real static-JSON fetch against import.meta.env.BASE_URL (static JSON dump),
3+
// or PUBLIC_API_BASE_URL when set (e.g. a Vercel preview reading main's live data).
4+
const raw = import.meta.env.PUBLIC_API_BASE_URL || import.meta.env.BASE_URL;
45
const base = raw.endsWith("/") ? raw : raw + "/";
5-
const absUrl = (path) => new URL(path.replace(/^\//, ""), location.origin + base).href;
6+
// base may be relative ("/TechAPI/") or absolute (PUBLIC_API_BASE_URL) — resolve against origin either way.
7+
const baseUrl = new URL(base, location.origin);
8+
const absUrl = (path) => new URL(path.replace(/^\//, ""), baseUrl).href;
69
const esc = (s) => String(s)
710
.replace(/&/g, "&")
811
.replace(/</g, "&lt;")

0 commit comments

Comments
 (0)