Search code examples
webpacksveltesapper

Webpack devServer proxy not working with Sapper


I have a node dev server running Sapper on http://localhost:3000, and I want all /api/ requests proxy another local dev server written on python http://localhost:8000/api/

This worked perfectly for pure Svelte:

// webpack.config.js

module.exports.devServer = {
    historyApiFallback: true,
    proxy: {
        '/api/': {
            target: 'http://localhost:8000',
            secure: false,
            changeOrigin: true
        }
    },
};

But does absolutely nothing with Sapper - just get default Sapper's 404 error

I guess it is somehow related with Sapper's routing mechanism, but can not find how to deal with it


Solution

  • Sapper uses Polka server. Proxy can be configured using http-proxy-middleware

    src/server.js

    const { createProxyMiddleware } = require('http-proxy-middleware');
    
    polka()
        .use('/api', createProxyMiddleware({ target: 'http://localhost:8000' }))
        // other .use, .listen rules