[英]Injecting information into requests returned from S3 origins in AWS CloudFront
I have a React SPA that I would like to deploy for different customers each with their own separate backend API.我有一个 React SPA,我想为不同的客户部署它,每个客户都有自己独立的后端 API。
To that end, I'd like to pass the backend api endpoint into the SPA.为此,我想将后端 api 端点传递给 SPA。
What I'd like to do is, if the user sends a get request https://myapp.mydomain.com
, he gets back a document, but the api endpoint has been added to the request in some way I can parse and use in React, eg his URL has been modified to read https://myapp.mydomain.com?api=myapi.mydomain.com
我想做的是,如果用户发送 get 请求
https://myapp.mydomain.com
,他会返回一个文档,但是 api 端点已以某种我可以解析和使用的方式添加到请求中在 React 中,例如他的 URL 已被修改为https://myapp.mydomain.com?api=myapi.mydomain.com
I'm using AWS CloudFront with an S3 origin, so AWS CloudFront functions look promising, but I've not had much luck so far.我正在使用具有 S3 源的 AWS CloudFront,因此 AWS CloudFront 功能看起来很有希望,但到目前为止我运气不佳。
I'm open to suggestions on how and where to inject the information and how to retrieve it in my React SPA, including entirely different approaches.我愿意接受有关如何以及在何处注入信息以及如何在我的 React SPA 中检索信息的建议,包括完全不同的方法。 The only parts I'm pretty much committed on is CloudFront and using a single S3 bucket to serve all customers.
我几乎致力于的唯一部分是 CloudFront 并使用单个 S3 存储桶为所有客户提供服务。
The solution I ended up implementing is based on the runtime-env-cra library.我最终实现的解决方案是基于 runtime-env-cra 库。 That basically loads an additional script from the server containing some javascript code which installs environment variables in the window object.
这基本上从服务器加载了一个附加脚本,其中包含一些在窗口对象中安装环境变量的 javascript 代码。
The necessary javascript file is generated by an AWS Lambda function which in turn is dynamically generated for each customer in the CloudFormation template from the input ressources.必要的 javascript 文件由 AWS Lambda 函数生成,该函数又从输入资源为 CloudFormation 模板中的每个客户动态生成。
This unfortunately cannot be done with CloudFront functions, as you have no access to the body of the request there.不幸的是,这无法通过 CloudFront 函数完成,因为您无法访问那里的请求正文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.