Skip to content

Commit ae262a3

Browse files
committed
fix: Resolve Vercel schema loading and CI path issues
1 parent 8e481c9 commit ae262a3

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

lib/api-client.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ interface SchemaPropertyDetails {
2121
* Gets the base URL for API requests, adjusting for GitHub Pages in production
2222
*/
2323
const getApiBase = () => {
24-
// In production (GitHub Pages), use static JSON files in the /api directory
25-
if (process.env.NODE_ENV === "production") {
24+
// Determine if this is a GitHub Pages specific build
25+
const isGithubPagesBuild = process.env.GITHUB_PAGES_BUILD === 'true';
26+
27+
// Use GitHub Pages path ONLY when specifically building for GitHub Pages
28+
if (isGithubPagesBuild) {
2629
return "/JSON_Schema_Validator/api";
2730
}
28-
// In development, use the dynamic API routes
31+
// In all other cases (development, Vercel, CI tests, standard prod builds)
2932
return "/api";
3033
};
3134

@@ -63,12 +66,18 @@ export async function getSchemaByName(
6366
.replace(/\.json$/, "")
6467
.replace(/[^\w-]/g, "");
6568

66-
// In development, fetch directly from public folder
67-
// In production, fetch the static JSON generated by the build script
68-
const url =
69-
process.env.NODE_ENV === "production"
70-
? `${getApiBase()}/schemas/${safeSchemaName}.json` // Prod URL remains the same (fetches from public/api/schemas)
71-
: `/schemas/v1/${safeSchemaName}.json`; // Changed dev URL (fetches from public/schemas/v1)
69+
// Determine the correct URL based on environment
70+
const isGithubPagesBuild = process.env.GITHUB_PAGES_BUILD === 'true';
71+
const isProduction = process.env.NODE_ENV === 'production';
72+
73+
// Use static paths for GitHub Pages build, dynamic API otherwise
74+
const url = isGithubPagesBuild
75+
? `${getApiBase()}/schemas/${safeSchemaName}.json` // /JSON_Schema_Validator/api/schemas/...json
76+
: isProduction
77+
? `/api/get-schema/${safeSchemaName}` // Use API route in regular production (Vercel)
78+
: `/schemas/v1/${safeSchemaName}.json`; // Dev fetches directly from public/schemas/v1
79+
80+
console.log(`[getSchemaByName] Fetching schema from URL: ${url}`); // Debug logging
7281

7382
try {
7483
const response = await fetch(url);
@@ -137,8 +146,14 @@ function generateClientSideMarkdown(schema: Record<string, unknown>): string {
137146
export async function generateSchemaDocumentation(
138147
schema: Record<string, unknown>,
139148
): Promise<{ markdown: string }> {
140-
// In production, generate markdown on the client side
141-
if (process.env.NODE_ENV === "production") {
149+
// Generate on client side if building for GitHub Pages or in production (Vercel)
150+
const isClientSideGeneration =
151+
process.env.GITHUB_PAGES_BUILD === 'true' || process.env.NODE_ENV === "production";
152+
153+
if (isClientSideGeneration) {
154+
console.log(
155+
"Generating schema documentation on the client side (Prod/GitHub Pages)",
156+
);
142157
try {
143158
return { markdown: generateClientSideMarkdown(schema) };
144159
} catch (error) {
@@ -153,6 +168,7 @@ export async function generateSchemaDocumentation(
153168
}
154169

155170
// In development, try the API endpoint first, fallback to client-side if unavailable
171+
console.log("Attempting to generate schema documentation via API (Dev)");
156172
try {
157173
// Use the absolute path to ensure the request works correctly
158174
const url = "/api/generate-schema-doc";

next.config.mjs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ try {
77
// ignore error
88
}
99

10+
// Determine if this build is specifically for GitHub Pages static export
11+
const isGithubPagesBuild = process.env.GITHUB_PAGES_BUILD === 'true';
12+
1013
/** @type {import('next').NextConfig} */
1114
const nextConfig = {
1215
// Configure basePath and assetPrefix conditionally
13-
// Use basePath for GitHub Pages only in production & when VERCEL is not set
14-
basePath:
15-
process.env.NODE_ENV === "production" && !process.env.VERCEL
16-
? "/JSON_Schema_Validator"
17-
: "",
18-
assetPrefix:
19-
process.env.NODE_ENV === "production" && !process.env.VERCEL
20-
? "/JSON_Schema_Validator/"
21-
: "/",
16+
// Use basePath only for GitHub Pages builds
17+
basePath: isGithubPagesBuild ? "/JSON_Schema_Validator" : "",
18+
assetPrefix: isGithubPagesBuild ? "/JSON_Schema_Validator/" : "/",
2219

2320
// Add eslint configuration to ignore during builds
2421
eslint: {

0 commit comments

Comments
 (0)