Umi: html 中资源路径引用问题

Created on 25 Mar 2018  ·  14Comments  ·  Source: umijs/umi

模板文件如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="theme-color" content="#000000">
  <link rel="shortcut icon" href="/public/favicon.ico">    <- 引入 Public 下的 Logo 文件
  <title>Title</title>
</head>
<body>
  <noscript>
    You need to enable JavaScript to run this app.
  </noscript>
  <div id="root"></div>
</body>
</html>

打包后 index.html 文件中没有重新修改路径。引用不正确

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  <meta name="theme-color" content="#000000">
  <link rel="shortcut icon" href="/public/favicon.ico">    <-Public 路径没有修改成static
  <title>Title</title>
  <link rel="stylesheet" href="/static/umi.2754f378.css">
</head>
<body>
  <noscript>You need to enable JavaScript to run this app.</noscript>
  <div id="root"></div>
  <script>window.routerBase = '/';
  window.resourceBaseUrl = '/static/';</script>
  <script src="/static/umi.e9292ebd.js"></script>
</body>
</html>

当然将模板中 public 改成 static 能够解决,但是在开发环境中会找不到该文件,总感觉不优雅。
请问有没有较好的解决方案?感谢~

type(enhancement)

Most helpful comment

默认 publicPath 已改为 '/static/'。

All 14 comments

+1, static应该用绝对路径吧
image
image

+1 引入favicon不方便啊

默认 publicPath 已改为 '/static/'。

  1. dev的环境下logo ok, 但是build出来还是不行
  2. 如果是依赖包中的图片(如上图),dev下加载不出来,build出来就ok

@arvinxx @webup @superlbr

在 jsx 里的引用现在可以暂时用类似这样的引用方式解决:

<img src={`${window.publicPath}img/logo.svg`} alt="" />

但是在其他地方,比如 less 里的 @import 一个相对服务器地址的绝对路径和 .webpackrc.js 里配置 @icon-url 的路径等。

另外一个 Workaround 就是手动把 /dist/static 下面的静态资源移动到 /dist 下,或者 package.json 里

"scripts": {
  "postbuild": "cp -a ./public/. ./dist/"
}

这个问题的具体表现:

在 production 环境,/public 下的所有文件都被复制到了 /dist/static 下,但是 URL 路径还是指向 /。但是如果把资源的引用地址改为 /static,在 development 环境又访问不到了。

默认 publicPath 已改为 '/static/'。

这个问题并不只是 publicPath 配置的问题,应该是一个 bug,希望尽快解决 @sorrycc

@mdluo
最近尝试配置 iconfont 本地部署时遇到了同样的问题,umi 1.3.4。

开始时使用 less 覆盖变量的方法,无效。转用 .webpackrc 配置,然而 src 下无法访问,于是只能放置字体文件到 public 下。

我的配置是:

  • iconfont 放置在 /public/assets/fonts/
  • .webpackrc theme 中配置 "icon-url": "'/assets/fonts/iconfont'"

如上配置开发环境下可以正常访问,build 部署测试字体文件全部 404,浏览器请求的路径是 /assets/fonts/,而实际上 build 时 public 内的文件夹已经被打包到了 dist/static 下,导致访问不到。

@sorrycc 希望能修复一下这个问题。

@sorrycc @mdluo @dreamerblue 我在使用electron+umijs时候也遇到和你同样的问题,例子就是官方的那个例子。在 production 环境,/public 下的所有文件都被复制到了 /dist/static 下,但是 URL 路径还是指向 /。但是如果把资源的引用地址改为 /static,css的url生成会变成/static/static/1.jpg,在 development 环境又访问不到了。换句话说,css url打包出来的地址会自行带上static,正因为多了一个static,我将publishPath设置为/,又导致umi.xx.js访问不到,因为它默认生成在dist/static目录下。如果没有nginx,就不能在build后运行了,连electron也不行,要么静态资源加载不了,要么我umi.59333.js访问不到。。希望sorrycc修复下。

这个问题你们怎么解决的

保证生产环境就ok

static 在 umi@2 时去掉了。

@sorrycc 希望能先出一个1.x的小版本先解决掉这个问题,或者给出一个规避方案保证生产环境

自己配个webpack?用copyplugin?

这是个 break change,在 umi@1 里不好修复。已在 umi@2 里解决,等下下周正式发布后升级解决吧。

@sorryccumi@2 里已解决, 怎么解决的啊, 文档里没看到呢... 显然也不是 https://umijs.org/zh/config/#publicpath 啊, 它默认值是 '/', 如果是它, 那整个源代码都全被拷贝到 dist 目录下了...用 webpack 的 copy 吗 https://umijs.org/zh/config/#copy ... 感觉好不优雅啊...

html 模板 中自己写也并不能帮自己完成把某个文件夹 copy/merge 到 dist 目录的功能.

好吧, 最后在一个 博客 里找到了可以在项目根目录下建 public 文件夹, 跟 create-react-app 类似, 感觉应该在 目录与约定 里说明下

Was this page helpful?
0 / 5 - 0 ratings

Related issues

afc163 picture afc163  ·  3Comments

zhanchengkun picture zhanchengkun  ·  3Comments

onReadyL picture onReadyL  ·  3Comments

nguyenhuutinh picture nguyenhuutinh  ·  3Comments

haiing picture haiing  ·  3Comments