简体   繁体   English

如何让 AWS Amplify 访问我的 React App 的公共目录

[英]How to make AWS Amplify access my React App's public directory

I deployed my React App on AWS Amplify.我在 AWS Amplify 上部署了我的 React 应用程序。 The problem is it doesn't access the sources in Public folder(eg images, models).问题是它不访问公共文件夹中的源(例如图像、模型)。 I heard I can add rules to Rewrite and Redirect on AWS Amplify, but I have no idea how to make the rule.我听说我可以在 AWS Amplify 上添加规则以重写和重定向,但我不知道如何制定规则。

This is my folder layout:这是我的文件夹布局:

在此处输入图像描述

The rules in AWS Amplify: AWS Amplify 中的规则:

在此处输入图像描述

How do I make the rules?我如何制定规则? Could someone help me out?有人可以帮帮我吗?

Sorry, this is sort of a 'shotgun' answer, but I suspect one of these should move you forward.抱歉,这是一种“霰弹枪”式的回答,但我怀疑其中一个应该会推动你前进。

This issue may have more to do with your build tools than Amplify.这个问题可能与您的构建工具有关,而不是与 Amplify 有关。 In my vite spa, I put my favicon.svg, logo.svg in my /src directory.在我的vite spa 中,我将我的 favicon.svg、logo.svg 放在我的/src目录中。 The only thing in my public dir is an image referenced in an email html - and for that it seems I hardcoded a fully qualified path.我的public目录中唯一的东西是 email html 中引用的图像 - 为此,我似乎硬编码了一个完全合格的路径。

I have an old CRA SPA, and I see things like this:我有一个旧的 CRA SPA,我看到这样的事情:

<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />

And you may need to expose files via manifest.json您可能需要通过manifest.json公开文件

{
  "short_name": "React App",
  "name": "Create React App Sample",
  "icons": [
    {
      "src": "favicon.ico",
      "sizes": "64x64 32x32 24x24 16x16",
      "type": "image/x-icon"
    },
    {
      "src": "logo192.png",
      "type": "image/png",
      "sizes": "192x192"
    },
    {
      "src": "logo512.png",
      "type": "image/png",
      "sizes": "512x512"
    }
  ],
  "start_url": ".",
  "display": "standalone",
  "theme_color": "#000000",
  "background_color": "#ffffff"
}

Rewrites/redirects probably aren't the reason images aren't working.重写/重定向可能不是图像无法正常工作的原因。 But if everything appears to work as long as you nav from your homepage, but breaks if you reload or deep link - that's where the rewrite rules will help.但是,如果只要您从主页导航,一切似乎都能正常工作,但如果您重新加载或深度链接就会中断——这就是重写规则的用武之地。

The doc is on this page .该文档在此页面上 From the doc:从文档:

Redirects for single page web apps (SPA)重定向单页 web 应用程序 (SPA)

Most SPA frameworks support HTML5 history.pushState() to change browser location without triggering a server request.大多数 SPA 框架支持 HTML5 history.pushState() 以在不触发服务器请求的情况下更改浏览器位置。 This works for users who begin their journey from the root (or /index.html), but fails for users who navigate directly to any other page.这适用于从根目录(或 /index.html)开始旅程的用户,但对直接导航到任何其他页面的用户无效。 Using regular expressions, the following example sets up a 200 rewrite for all files to index.html, except for the specific file extensions specified in the regular expression.使用正则表达式,以下示例将所有文件设置为 200 重写到 index.html,正则表达式中指定的特定文件扩展名除外。

Source address:来源地址:

</^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|woff2|ttf|map|json)$)([^.]+$)/>

Target address:目标地址:

/index.html

Type:类型:

200 (Rewrite)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM