Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev Server Proxy #75

Closed
AlStar01 opened this issue May 30, 2017 · 3 comments
Closed

Dev Server Proxy #75

AlStar01 opened this issue May 30, 2017 · 3 comments
Assignees
Labels

Comments

@AlStar01
Copy link

Hello,

First off, I just want to say thank you for creating and maintaining this amazing tool, it's an absolute delight to work with and makes it just so easy and quick to get a Preact PWA up and running.

This may be more of a feature request, but is there any way to specify a proxy for the development server to intercept/redirect calls in either the package.json or another configuration file?

Thank you for any help you can provide.

@lukeed
Copy link
Member

lukeed commented May 30, 2017

Thanks for the kind words! A lot of guys spend a lot of time making this great. 😄

This will be doable once #56 lands 👍

@developit
Copy link
Member

Thank you! ❤️

Merging #56 makes this possible:

// preact.config.js

module.exports = function(config) {
  config.devServer.proxy = [
    {
      // proxy requests matching a pattern:
      path: '/api/**',

      // where to proxy to:
      target: 'http://api.example.com',

      // optionally change Origin: and Host: headers to match target:
      changeOrigin: true,
      changeHost: true,

      // optionally mutate request before proxying:
      pathRewrite: function(path, request) {
        // you can modify the outbound proxy request here:
        delete req.headers.referer;

        // common: remove first path segment: (/api/**)
        return '/' + path.replace(/^\/[^\/]+\//, '');
      },

      // optionally mutate proxy response:
      onProxyRes: function(proxyRes, req, res) {
        // you can modify the response here:
        proxyRes.headers.connection = 'keep-alive';
        proxyRes.headers['cache-control'] = 'no-cache';
      }
    }
  ];
};

That said, I'd love to support parsing a static-app.json file (in Firebase/Superstatic format) like preact-cli produces for setting headers. The above example would then be as simple as dropping this file into your repo root:

// static-app.json

{
  "redirects": [
    {
      "source": "/api/(.*)",
      "destination": "http://api.example.com/$1",
      "type": "proxy"
    }
  ]
}

Nice and clean, and it'd then also be available if your hosting provider supports the format.

@rkostrzewski rkostrzewski self-assigned this Jun 25, 2017
@rkostrzewski
Copy link
Collaborator

#56 landed closing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants