Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Bug ]: vite 模板 blocklet deploy后, 不支持动态加载 modulepreload 包 #219

Closed
FireTable opened this issue Oct 10, 2022 · 9 comments
Assignees
Milestone

Comments

@FireTable
Copy link
Contributor

FireTable commented Oct 10, 2022

问题

tweet-token 迁移到vite时候发现
目前在vite中, 如果用了 React.lazy / vite自带的 import.meta.glob 语法

dynamic import的包, blocklet deploy后, 会变成以下这样

<link rel="modulepreload" as="script" crossorigin="" href="/.blocklet/proxy/z8iZoLRKRXHzqdJ2vFZEi4H5UXT9ADsurxZRK/assets/admin.e7b77da7.js">

其中应该是 crossorigin="" / rel="modulepreload" 属性影响vite加载包的功能, 导致无法加载dynamic的包
image

查看network发现, 并没有附带 header.referer 参数, 导致被 blocklet-server 拦截了
image

// 无referer test
image

// 有referer test
image

  • 现状: 只能用单文件打包方式

解决思路

  1. blocklet server 不拦截 header.referer === undefined
    • 晨哥沟通后, 发现 referer 参数是需要的, 不能这样玩
  2. 配置vite项目

为什么没进行下去

在翻 vite 文档的时候, 发现英文文档 5小时前 更新了一个参数 build.modulePreload.resolveDependencies, 感觉有戏

image

image

所以先做问题记录, 后续再做修复

@FireTable FireTable self-assigned this Oct 10, 2022
@FireTable FireTable changed the title [ Bug ]: vite 模板不支持 blocklet deploy后, 动态加载 modulepreload 包 [ Bug ]: vite 模板 blocklet deploy后, 不支持动态加载 modulepreload 包 Oct 10, 2022
@FireTable
Copy link
Contributor Author

FireTable commented Oct 10, 2022

@LancelotLewis : 发现使用 link modulepreload 加载 js 文件天然就没有 referer, 使用内测版似乎就能解决问题了

坐等 vite3.2 更新 build.modulePreload 属性

@FireTable
Copy link
Contributor Author

vitejs/vite#9938

vite 相关 PR

@LancelotLewis
Copy link
Collaborator

在 3.2 beta 中设置 build.modulePreload: false 禁用掉 modulepreload 可以成功

@LancelotLewis
Copy link
Collaborator

发现使用 link modulepreload 加载 js 文件天然就没有 referer

是 chrome 内核的 bug,firefox 实测没有问题

在 chrome 下的表现是 link modulepreload 加载 css 有带 referer,加载 js 文件不带 referer
而在 firefox 下的表现是 link modulepreload 加载 css 和 js 都会携带 referer

考虑到 modulepreload 在不同浏览器下的实现是不统一的,所以禁用 modulepreload 可能是目前最省心的选择

@FireTable
Copy link
Contributor Author

军哥的 vcm 也遇到同样的问题

后续支持后, 需要通知 vcm / tweet-token

@LancelotLewis
Copy link
Collaborator

@wangshijun wangshijun added this to the 2022 Q4 milestone Oct 30, 2022
@yumanman88
Copy link

请问,这个修改modulepreload的crossorigin是已经在3.2.0实现了吗

@FireTable
Copy link
Contributor Author

请问,这个修改modulepreload的crossorigin是已经在3.2.0实现了吗

https://cn.vitejs.dev/config/build-options.html#build-modulepreload

@LancelotLewis
Copy link
Collaborator

LancelotLewis commented Oct 18, 2023

请问,这个修改modulepreload的crossorigin是已经在3.2.0实现了吗

@yumanman88
在新版的 blocklet-server,不再对静态资源有 referer 限制,所以目前不需要修改 build.modulePreload 也可以实现正常加载拆包的应用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants