Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const API_ROOT = '/api'
const API_ROOT = `${import.meta.env.BASE_URL}api`

class ApiError extends Error {
readonly status: number
Expand Down
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, refetchOnWindowFocus: false } },
})

const router = createRouter({ routeTree })
const router = createRouter({ routeTree, basepath: import.meta.env.BASE_URL })

declare module '@tanstack/react-router' {
interface Register {
Expand Down
6 changes: 5 additions & 1 deletion vite-plugin-mock-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ const mockApiPlugin = (): Plugin => {
const module = await server.ssrLoadModule(HANDLERS_MODULE)
const middleware = createMiddleware(...(module.handlers as RequestHandler[]))

// This runs ahead of vite's own middleware, so requests still carry the base.
const apiRoot = `${server.config.base}api/`
server.middlewares.use((request, response, next) => {
if (!request.url?.startsWith('/api/')) return next()
if (!request.url?.startsWith(apiRoot)) return next()
// The handlers are written against the root, as the test suite serves them.
request.url = request.url.slice(server.config.base.length - 1)
addExpressAccessors(request as unknown as ExpressLikeRequest)
return middleware(request, response, next)
})
Expand Down
2 changes: 2 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { fileURLToPath } from 'node:url'
import { mockApiPlugin } from './vite-plugin-mock-api.ts'

export default defineConfig({
// Lets the app be served under a path, such as behind a proxy.
base: process.env.BASE_PATH ?? '/',
plugins: [
tanstackRouter({
target: 'react',
Expand Down
Loading