Personal Catalogue: React, Express & MongoDB — Chapter 10, Exercise 1
=====================================================================
TASK
Set base: '/catalogue/' in vite.config.js and VITE_API_URL=/catalogue
in .env.production, rebuild the frontend, and confirm the built
index.html's own asset references and every compiled fetch call both
correctly include the /catalogue prefix.
SOLUTION
catalogue-web/vite.config.js:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
base: '/catalogue/',
});
catalogue-web/.env.production:
VITE_API_URL=/catalogue
Rebuild:
cd catalogue-web
npm run build
Inspecting the real output in dist/index.html:
Both asset references now carry the real /catalogue/ prefix, where an
unset base would have produced plain /assets/index-a1b2c3d4.js instead.
Confirming the compiled fetch calls: searching the built JS bundle
itself (grep -o 'catalogue/api[^"'"'"']*' dist/assets/index-*.js, or
simply opening the minified file and searching for "catalogue") shows
the literal string /catalogue baked into the bundle everywhere
API_BASE_URL is used, producing paths like /catalogue/api/items and
/catalogue/api/items/search once evaluated at runtime -- confirming
VITE_API_URL was correctly picked up and inlined into the production
build, not just left as a reference to an environment variable that
would need to exist at runtime (it doesn't; Vite replaces
import.meta.env.VITE_API_URL with the literal string at build time).
WHY THIS WORKS AS AN ANSWER
----------------------------
It sets both real configuration values exactly as the chapter
specifies, performs a real production build, and inspects the actual
generated output -- both the HTML's own asset tags and the compiled
JavaScript bundle -- to confirm the /catalogue prefix genuinely made it
into both places, rather than assuming the configuration alone
guarantees the result without checking the real build artifacts.