Use OSDK with Vite
Configure Vite for OSDK apps, including the process.env fix, a local CORS proxy to Foundry, and the OSDK Devtools plugin.
Vite is the default bundler for apps created with @osdk/create-app. This page covers the Vite settings an OSDK app needs and why.
The React template in @osdk/create-app already includes these settings in its generated vite.config.ts. Read on if you add OSDK to an existing Vite app or want to know what the generated config does.
Define process.env.NODE_ENV
OSDK packages read process.env.NODE_ENV to decide how much to log and to strip development-only code from production builds. Vite does not replace process.env by default, so the browser throws:
ReferenceError: process is not definedTell Vite to replace process.env.NODE_ENV with the current mode:
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig(({ mode }) => {
return {
plugins: [react()],
define: {
"process.env.NODE_ENV": JSON.stringify(mode),
},
};
});vite dev runs in development mode and vite build in production mode, so production bundles get "production".
Proxy Foundry requests in development
The browser blocks requests from http://localhost:8080 to your Foundry stack unless the stack allows that origin for CORS. If you cannot get localhost allowed, proxy the requests through the Vite dev server instead. @osdk/create-app generates this setup when you answer yes to the CORS proxy prompt or pass --corsProxy true:
Proxy the API paths
Forward /api and /multipass/api to your Foundry URL:
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig(({ mode }) => {
return {
plugins: [react()],
server: {
port: 8080,
proxy: {
"^(/multipass/api|/api)": {
target: "https://your-stack.palantirfoundry.com",
changeOrigin: true,
secure: true,
},
},
},
define: {
"process.env.NODE_ENV": JSON.stringify(mode),
},
};
});Point the client at the dev server
Set the Foundry URL that the client uses to the dev server's origin, so requests go through the proxy:
VITE_FOUNDRY_API_URL=http://localhost:8080
VITE_FOUNDRY_REDIRECT_URL=http://localhost:8080/auth/callbackPass that URL to both createPublicOauthClient and createClient. The OAuth calls under /multipass/api and the data calls under /api then reach Foundry through the proxy.
Allow the redirect URL
Add http://localhost:8080/auth/callback as a redirect URL on the OAuth client in Developer Console.
The proxy runs only under vite dev. Production builds call Foundry directly, so keep the real Foundry URL in .env.production. Once your stack allows http://localhost:8080 for CORS, you can remove the proxy and set VITE_FOUNDRY_API_URL back to the Foundry URL.
Add OSDK Devtools
@osdk/react-devtools ships a Vite plugin that injects an inspection panel into your app during vite dev and does nothing in production builds:
import { osdkDevTools } from "@osdk/react-devtools/vite";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [react(), osdkDevTools()],
});See OSDK Devtools for installation, options, and the panel tabs.
Integration testing
Run OSDK code against a local Ontology server with @osdk/integration-testing, seed it with objects and links, and stub functions in Vitest.
Troubleshooting
Fixes for common OSDK errors, including a missing OsdkProvider, mismatched package versions, CORS, auth failures, and Vite build issues.