Coder Social home page Coder Social logo

Comments (3)

njugray avatar njugray commented on May 1, 2024

静态资源只需要指定URL, 相当于传统页面模板中js的引入. 北斗仅在 view 插件中提供一个辅助方法 helper.asset(), 源码 方便通过配置config, 拼装出资源的真实路径.

config.default.js

module.exports =  {
  react: {
    assetHost: '//yourcdn.com',
    assetPath: '/project/1.0.0',
  },
};

这时使用 helper.asset('main.js')会得到 //yourcdn.com/project/1.0.0/main.js

CDN资源只服务客户端, 服务端不直接使用.

from beidou.

lizhiyao avatar lizhiyao commented on May 1, 2024

感谢解答。

放在 CDN 上的前端静态资源,通常是有缓存机制的。

传统的在发布新版本时,解决由于 CDN 缓存无法更新到最新资源的方式是给资源地址加上时间戳。
比如://mycdn.com/project/build/main.js?t=1234

如果是基于 Webpack 构建的资源,可以将文件名中自动加入 hash 值,比如: main.hash.js,并且使得 HTML 中自动注入引用 main.hash.jsscript 标签。

在北斗框架体系下,执行 npm run build 时,会在项目根目录的 build 文件夹中生成客户端运行的静态资源。比如:/build/main.js。结合上述配置和 helper.asset('main.js') 最终可以在页面中得到 //mycdn.com/project/build/main.js这样的路径。

在控制台可以看到构建结果是 filema,e.js?hash 形式的:

image

其构建结果记录在项目根目录 /.stats.json 中。


目前项目的发布方式计划是:/client 下的客户端代码使用公司的发布系统构建发布,本质上是在一台服务器上用 node 去构建资源,并将资源同步到 cdn。 此时前端资源文件名是没有 hash 值的,生成的 .stats.json 是在运行发布系统的服务器上。

整个基于北斗框架的 node 项目,会按照文档中推荐的方式进行构建+部署。

问题是:在北斗体系下,推荐的规避 CDN 资源缓存的做法是什么?可以想到的是在启动 node 服务时,定义一个时间戳变量,然后在渲染页面时使用,但是觉得这种方式不够好。

from beidou.

njugray avatar njugray commented on May 1, 2024

总结下之前讨论的结果:

  1. 理想情况下, 资源的构建和发布和Node应用的发布实在是由单个系统完全控制, 这种情况下可在构建后动态分析.stats.json, 读取文件hash值(或自行计算文件哈希), 将结果输出到文件中, 供config文件读取使用.

  2. 前端资源和Node应用独立发布的情况下, 需要自行手动指定版本号, 或者使用config.pkg.version (或任何其他自定义字段)配置资源版本。启动时在config中动态生成时间戳, 这种方法需要谨慎使用, 每个Work和Agent都会执行这段逻辑, 生成的时间戳极有可能不相同.

from beidou.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.