Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 使用基于多路径的单页应用程序的S3/Cloudfront客户端路由_Amazon Web Services_Amazon S3_React Router_Single Page Application_Amazon Cloudfront - Fatal编程技术网

Amazon web services 使用基于多路径的单页应用程序的S3/Cloudfront客户端路由

Amazon web services 使用基于多路径的单页应用程序的S3/Cloudfront客户端路由,amazon-web-services,amazon-s3,react-router,single-page-application,amazon-cloudfront,Amazon Web Services,Amazon S3,React Router,Single Page Application,Amazon Cloudfront,我有以下情况: 一个S3存储桶,包含多个基于路径的应用程序,按版本号分组。 简化示例: 每个应用程序都是(React)SPA,需要客户端路由(通过React路由器) 我将S3与Cloudfront一起使用,使一切都能正常工作,但是客户端路由被破坏了。也就是说,我可以访问每个应用程序的根目录,即https://以重定向到index.html,但我相信此解决方案仅在每个bucket有一个index.html时才有效(即,我无法为每个基于路由的路径设置错误文档) 解决这个问题的最佳方法是什么?

我有以下情况:

  • 一个S3存储桶,包含多个基于路径的应用程序,按版本号分组。 简化示例:
  • 每个应用程序都是(React)SPA,需要客户端路由(通过React路由器)
我将S3与Cloudfront一起使用,使一切都能正常工作,但是客户端路由被破坏了。也就是说,我可以访问每个应用程序的根目录,即
https://以重定向到
index.html
,但我相信此解决方案仅在每个bucket有一个
index.html
时才有效(即,我无法为每个基于路由的路径设置错误文档)


解决这个问题的最佳方法是什么?

通过Cloudfront处理SPA的一个简单方法是使用Lambda@Edge-原产地请求(或)。 目标是更改原始URI

我经常用于SPA的简单js代码(用于v1.0.0 webapp):

exports.handler=async(事件)=>{
const request=event.Records[0].cf.request;
const hasType=request.uri.split(/\\\\?/)[0]。split('.')。长度>=2;
if(hasType)return request;//只需转发到S3对象,因为它是一个资产
request.uri='/v1.0.0/index.html';//处理所有反应路由
返回请求;
};
我检查URL中是否有扩展名(.png、.js、.css、…)。如果它是一个资产,我只需转发到S3对象,否则我将发送index.html。
在这种情况下,index.html被发送到路径/v1.0.0/my-react-router

已更新
对于动态处理,您可以这样做(出于想法):
request.uri='/'+request.uri.split('/')[1]+'/index.html'


或者更好的是,使用regexp解析
request.uri
,以提取版本、资产扩展或spa路由。

谢谢!这很有帮助。唯一的缺点是,我似乎需要硬编码的版本?我将上传频繁的版本,理想情况下,处理程序将能够动态路由到正确的版本。您是否每次都有该路径中的版本?我可以更改动态路由处理的答案。@NicholasHaley,我添加了一个示例来处理该版本。您可以通过使用regexp一次性解析路径来优化代码。很高兴确认(大部分)一切正常!唯一的问题是资产检查
hasType
不适用于我的特定URL,因为版本号包含句点并产生误报
/v1.0.0
  index.html
  main.js
/v1.1.0
  index.html
  main.js