Svelte: proxy config

Created on 16 Oct 2019  路  6Comments  路  Source: sveltejs/svelte

Is your feature request related to a problem? Please describe.
In development mode it would be great to have some proxy like angular, to have relative fetch

Describe the solution you'd like

ng serve --proxy-config proxy-conf.json

proxy-conf.json:

{
    "/api": {
        "target": "http://localhost:8080/ustvarjalnik",
        "secure": false
    },
    "/aaiLogin": {
        "target": "http://localhost:8080/ustvarjalnik",
        "secure": false
    }
}

Describe alternatives you've considered
I don't know roolup

How important is this feature to you?
Very

Additional context
Add any other context or screenshots about the feature request here.

Most helpful comment

It is very bad development experience, angular, vue, react have it out of the box

All 6 comments

This is out of scope for the compiler, and is also out of scope for the simple templates at https://github.com/sveltejs/template and https://github.com/sveltejs/template-webpack. If you want proxying, you can write a simple web server (with Express, for example) that uses middleware to proxy the appropriate URLs and serve local files for others.

It is very bad development experience, angular, vue, react have it out of the box

@sysmat

Angular, Vue, React all use webpack-dev-server; in which the proxy functionality is built-in. Although they expose the proxy functionality somewhat differently.

Since https://github.com/sveltejs/template-webpack uses webpack-dev-server, you can just configure webpack.config.js with the proxy config and you should be good to go.

  entry: './app.js',
  devServer: {
    proxy: {
      '/api': 'http://127.0.0.1:50545',
    },
  },

In the webpack-dev-server repo you can find a couple more proxy example configurations:
https://github.com/webpack/webpack-dev-server/blob/master/examples/general

There is also some good documentation on https://webpack.js.org/configuration/dev-server/#devserverproxy

I would say its absolute "Must have feature" that should bring together with rollup

Coming from Create React App and stumbled across this hurdle. The convenience of just adding one line of code to the client-side package.json was great. This avoided CORS issues or having to add additional code to the backend.

If someone wants this and uses rollup,
Just use https://github.com/pearofducks/rollup-plugin-dev
You could do something like this:

// rollup.config.js
import dev from 'rollup-plugin-dev';

...
plugins: [
...
!production && dev({
        dirs: ['public'],
    spa: 'public/index.html', 
    port: 5000, 
    proxy: { 
        '/api/*': 'path/to/your/proxy/',
    },
}),
// In dev mode, call `npm run start` once
// the bundle has been generated
// !production && serve(), // Also Mark/Delete this line.. no longer needed.

...
Was this page helpful?
0 / 5 - 0 ratings