修掉资源 404 被当成页面返回的 bug;加一条命令起开发模式
单页应用的兜底原本对任何找不到的路径都回 index.html,包括带扩展名的资源。 资源文件是带哈希的,每次打包换名字,浏览器缓存了旧 index.html 时会去请求一个已经 不存在的 .js,然后拿到一份 HTML,报出「MIME 类型不对」——一句完全指不到病根的错误。 现在带扩展名的路径找不到就回 404,index.html 也不再允许缓存。 另外 dev:all 一条命令同时起后端(--hot)和前端(Vite),省掉记两个终端和两个端口。
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 开发模式:一条命令同时起后端和前端。
|
||||
*
|
||||
* 后端带 --hot,改了服务端代码自己重载;前端是 Vite,改了界面代码浏览器立刻更新,
|
||||
* 两边都不用手动重启。访问 Vite 那个地址(5173),它会把接口请求转发给后端。
|
||||
*
|
||||
* 打包后的 `bun run start` 不具备这个能力:它把前端打成静态文件再由后端提供,
|
||||
* 代码一改就得重新打包重启。那种方式适合最终验证和部署。
|
||||
*/
|
||||
export {}
|
||||
|
||||
const procs = [
|
||||
Bun.spawn(['bun', 'run', '--hot', 'server/index.ts'], { stdio: ['inherit', 'inherit', 'inherit'] }),
|
||||
Bun.spawn(['bunx', 'vite'], { stdio: ['inherit', 'inherit', 'inherit'] }),
|
||||
]
|
||||
|
||||
const stopAll = () => {
|
||||
for (const p of procs) p.kill()
|
||||
process.exit(0)
|
||||
}
|
||||
process.on('SIGINT', stopAll)
|
||||
process.on('SIGTERM', stopAll)
|
||||
|
||||
await Promise.race(procs.map((p) => p.exited))
|
||||
stopAll()
|
||||
Reference in New Issue
Block a user