David Rodrigues
David Rodrigues

Reputation: 12552

astro.js build with vite proxy

I am developing a page using Astro for the first time, version 2.0.16. However, it was necessary to perform a "more in-depth" configuration to avoid CORS problems. So I configured vite proxy as follows:

astro.config.js

export default defineConfig({
  // ...
  vite: {
    server: {
      proxy: {
        "/api/products": {
          target: "https://external.api/",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api\/products/, ""),
        },
      },
    },
  },
});

While running in dev mode, everything works perfectly! However, when building, things start to go awry. The generated file is a simple .html file, without a server. So this proxy would be impossible. Even using the node adapter, nothing works as expected.

As a result, I can only run the project in dev mode.

Is there any solution in this regard using Astro, or do I need to resort to other methods?

Upvotes: 0

Views: 1414

Answers (1)

Giacomo
Giacomo

Reputation: 1

Have you consider to use a reverse proxy also for production? Try with nginx, basically you have to route static files requests to the astro stuff, and the api endpoints to your api production server

Upvotes: 0

Related Questions