stencil icon indicating copy to clipboard operation
stencil copied to clipboard

[Feature Request] Proxy Option for Development Server

Open ardatan opened this issue 8 years ago • 5 comments

Stencil version:

 @stencil/core@<version>

I'm submitting a:

[ ] bug report [X] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or https://stencil-worldwide.slack.com

Copied from https://github.com/ionic-team/stencil-dev-server/issues/26

Request for that development server supports proxying files from another server for given urls. So it can be configured like this in stencil.config.js:

exports.devServer = {
  root: 'www',
  watchGlob: '**/**',
  proxy: {
    '{URL_PATTERN}': '{API_URL}'
  }
}

ardatan avatar Jul 02 '18 19:07 ardatan

The dev server has since moved to the integrated dev server within @stencil/core. Happy to help answer any questions with stencil slack for anyone interested in contributing. Thanks.

adamdbradley avatar Jul 26 '18 02:07 adamdbradley

Can we re-open this issue? The fact that the dev server moved into core had nothing to do with resolving this feature request, and the issue is linked to stencil core on Github as you would expect.

apillard avatar Apr 09 '19 17:04 apillard

Did anyone discover a way to set up proxy for development environment? I am trying to create a login component in my dev environment that's posting auth information on an api hosted elsewhere. Keep running into CORS issues and would like to set up proxying. I've done this same proof-of-concept with a react app.

sunbot avatar Oct 11 '19 02:10 sunbot

@sunbot! I'm using my own implementation and it's working fine. I hope some day stencil will include this feature into their dev server too. I've put the code to a gist.

peterreisz avatar Oct 11 '19 06:10 peterreisz

Better late than never. This solution adds proxy support for the StencilJS dev server:

stencil.config.ts

import { Config } from '@stencil/core';
import { resolve } from 'path';

export const config: Config = {
  ...
  devServer: {
    requestListenerPath: resolve(__dirname, 'proxy.middleware.js'), // adds proxy support
  },
};

proxy.conf.js (example)

module.exports = [
  {
    paths: ['/api/'],
    target: 'http://localhost:8080',
    changeOrigin: true
  }
];

proxy.middleware.js

const httpProxy = require('http-proxy');
const proxyRules = require('./proxy.conf');

const proxy = httpProxy.createProxyServer({});

// global error handler
proxy.on('error', (err, req, res) => {
  console.error(`Proxy error [${req.url}]:`, err.message);
  if (!res.headersSent) {
    res.statusCode = 500;
    res.end('Proxy error');
  }
});

// proxy middleware for Stencil dev server
module.exports = (req, res, next) => {
  // find the first rule where the request URL starts with one of the defined paths
  const rule = proxyRules.find(({ paths }) => 
    paths.some(path => req.url.startsWith(path))
  );

  // if a matching rule is found, proxy the request
  if (rule) {
    const { paths, ...options } = rule;

    return proxy.web(req, res, options);
  }

  // no matching rule found, continue to Stencil dev server
  next();
};

vskosp avatar Nov 20 '25 12:11 vskosp