Hey everyone! Wondering if there has been any dis...
# sst
a
Hey everyone! Wondering if there has been any discussion about adding URL rewrite/proxy in the static site resources. I know we can do this manually by adding a function to the cloudfront distribution to proxy requests from
<http://example.com/api|example.com/api>
to
<http://my-api-domain.com/|my-api-domain.com/>
but I'm wondering if there's any plan on adding this to the current static site resources to automate it a bit.
f
Hey @Arpad, I have a snippet here that I’ve been sending to ppl https://gist.github.com/fwang/49a455beb3ce1dae3f7116024803f430
U mean building this into the construct right?
I haven’t found out how to redirect
<http://example.com/api/users|example.com/api/users>
to
<http://my-api-domain.com/users|my-api-domain.com/users>
. The setup in gist redirects
<http://example.com/api/users|example.com/api/users>
to
<http://my-api-domain.com/api/users|my-api-domain.com/api/users>
which isn’t ideal.
a
Hey @Frank, yep, that snippet seems about right!
U mean building this into the construct right?
Yes, something like:
Copy code
const site = new sst.ReactStaticSite(stack, "Frontend", {
  ...
  rewrites: {
    'api/*': '<http://api.domain.com/:splat|api.domain.com/:splat>' // idea from netlify _redirects file
  }
});
For context, right now I am in the process of migrating an old backend from SAM + CloudFormation to SST and we have a Lambda@Edge that handles the rewriting of requests to our backend domain (another guy wrote the code and right now I'm still wrapping my head around everything there). I guess this would be the only way of handling custom rewrites.
this is how the lambda looks like:
Copy code
exports.handler = async (event) => {
    const request = event.Records[0].cf.request;
    const newUri = event.Records[0].cf.request.uri.split('/api');
    request.uri = newUri[0] + newUri[1];

    return request;
}
Pretty basic. It just removes the
/api
part from the URL