Skip to content

Commit 819de49

Browse files
committed
chore: add static mock deployment
- Add GitHub Pages workflow to build and deploy MSW mock dashboard - Make MSW worker URL and API base URL respect Vite BASE_URL for subpath hosting - Document GitHub Pages and Vercel mock deployment steps
1 parent f43db44 commit 819de49

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

.github/workflows/pages-mock.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Deploy Mock (GitHub Pages)
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
workflow_dispatch: {}
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
concurrency:
14+
group: pages
15+
cancel-in-progress: true
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
- uses: pnpm/action-setup@v4
23+
with:
24+
version: 9
25+
- uses: actions/setup-node@v4
26+
with:
27+
node-version: 20
28+
cache: pnpm
29+
- run: pnpm install --frozen-lockfile
30+
- run: pnpm build -- --base="/${{ github.event.repository.name }}/"
31+
env:
32+
VITE_USE_MOCKS: "true"
33+
VITE_API_BASE_URL: "/${{ github.event.repository.name }}/api/v1/admin"
34+
- uses: actions/upload-pages-artifact@v3
35+
with:
36+
path: dist
37+
38+
deploy:
39+
needs: build
40+
runs-on: ubuntu-latest
41+
environment:
42+
name: github-pages
43+
url: ${{ steps.deployment.outputs.page_url }}
44+
steps:
45+
- id: deployment
46+
uses: actions/deploy-pages@v4
47+

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,26 @@ e-commerce-dashboard/
182182
2. `?mock=1`
183183
3. `localStorage.USE_MOCKS === "1"`
184184

185+
## Deploy Mock (Static)
186+
187+
This repo can be deployed as a fully-static “mock” dashboard (no backend) using MSW.
188+
189+
### GitHub Pages (automatic)
190+
191+
1. In GitHub, open **Settings → Pages** and set **Build and deployment** to **GitHub Actions**.
192+
2. Push to `main` (or run the workflow manually).
193+
194+
Workflow: `.github/workflows/pages-mock.yml`.
195+
196+
### Vercel (automatic)
197+
198+
1. Import the repo in Vercel (Framework preset: Vite).
199+
2. Set environment variables:
200+
- `VITE_USE_MOCKS=true`
201+
- (optional) `VITE_API_BASE_URL=/api/v1/admin`
202+
3. Build command: `pnpm build` (or `npm run build`)
203+
4. Output directory: `dist`
204+
185205
## API Documentation
186206

187207
- OpenAPI YAML: `docs/swagger.yaml`

src/lib/api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,16 @@ import type {
6161
import { QueryParams, buildQueryParams, validateQueryParams } from './queryBuilder';
6262
import { transformResponse } from './transformers';
6363
import type { ErrorResponse } from '@/types/api';
64+
import { shouldEnableMocks } from '@/mocks/config';
6465

6566
// Accepts plain objects (typed form values) or FormData payloads
6667
type UnknownRecord = Record<string, unknown> | FormData;
6768

68-
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000/api/v1/admin';
69+
const API_BASE_URL =
70+
import.meta.env.VITE_API_BASE_URL ||
71+
(shouldEnableMocks()
72+
? `${import.meta.env.BASE_URL}api/v1/admin`
73+
: 'http://localhost:3000/api/v1/admin');
6974

7075
const api = axios.create({
7176
baseURL: API_BASE_URL,

src/mocks/browser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export async function startMockWorker() {
88
if (!config.enabled) return;
99

1010
const apiBaseUrl =
11-
import.meta.env.VITE_API_BASE_URL ?? "http://localhost:3000/api/v1/admin";
11+
import.meta.env.VITE_API_BASE_URL ??
12+
`${import.meta.env.BASE_URL}api/v1/admin`;
1213
const handlers = createHandlers({
1314
spec: mockSpec,
1415
apiBaseUrl,
@@ -23,7 +24,7 @@ export async function startMockWorker() {
2324

2425
await worker.start({
2526
onUnhandledRequest: "bypass",
26-
serviceWorker: { url: "/mockServiceWorker.js" },
27+
serviceWorker: { url: `${import.meta.env.BASE_URL}mockServiceWorker.js` },
2728
// Ensure the worker is ready before continuing
2829
quiet: false,
2930
});

0 commit comments

Comments
 (0)