Served static HTML file fails to load resources
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- css, javascript, node.js, typescript
调研方向
使用 dev 脚本复现由 fastify-cli v7.0.1 生成的项目,并检查 dist/routes/root.js、dist/public/index.html 和 dist/assets 目录。从 fastifyStatic 注册和根路由开始,然后验证生成的应用如何提供公共文件。在生成的项目中,链接的 CSS 和 JavaScript 资源能够加载且没有 404 响应,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the issue has not already been raised
Issue
Ok - I'm at the end of my rope on this one and I need a little help!
In short, the problem is. I'm serving a static html file with links to external javascript files and css files and these files are not being found. It seems like the Fastify router is trying to route the links (if that makes sense).
Here are the details:
I have created a new Fastify project using fastify-cli v7.0.1 with: fastify generate my-project --esm --lang=ts
Using the npm scripts from the generated project, I run "dev" and the html file is served ok, but I get a 404 on GET http://localhost:8000/assets/mdr.css (the css file linked to the html file).
The error on route "/":
{
"message": "Route GET:/assets/mdr.css not found",
"error": "Not Found",
"statusCode": 404
}
Here is the index.html file:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="./favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Home Page</title>
<link rel="stylesheet" href="assets/mdr.css"> <!-- CANNOT FIND THIS FILE -->
<!-- I ommitted the JS script as this is producing the same error and need to simplfy -->
</head>
<body
<div>Serving HTML from dist/public REV 1</div>
</body>
</html>
Here is my /dist folder structure:
/dist
app.js
/assets
mdr.css
/plugins
sensible.js <-- generated
support.js <-- generated
/public
index.html
/routes
/example
index.js <-- generated
root.js
And my root.js route:
import { FastifyPluginAsync } from "fastify";
import path from "path";
import { fileURLToPath } from "url";
import fastifyStatic from "@fastify/static";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.register(fastifyStatic, {
root: path.join(__dirname, "../","public"),
prefix: "/assets/", //<-- secondary problem - if set to "/public" I get an error that it is already defined somewhere else (not by me)
});
fastify.get("/", async function (request, reply:any) {
return reply.sendFile("index.html");
});
};
export default root;
The generated app is using Autoload on routes and plugins, all created by the generator.
I have just about everything I can think of to fix this and have not been able to.
Any help would be appreciated!
Thanks
_mark
- 主要语言
- JavaScript
- 星标
- 733
- 派生
- 179
- 平均合并
- 8 天 12 小时
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
fastify/fastify-cli 的其他 Issue
-
难度 5/5 一周以上 新手友好度 35/100
fastify/fastify-cli#846 · 2 条评论 ·
-
难度 5/5 一周以上 新手友好度 25/100
fastify/fastify-cli#841 ·
-
Generated Fastify ESM/TS app has broken `npm test` command (FASTIFY_AUTOLOAD_TYPESCRIPT)可能已有人在做 @GhaythBenAbid 于 25 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 45/100
fastify/fastify-cli#835 · 2 条评论 ·
-
Adding Path Alias Support in fastify-cli可能重新可做 @ali139713 于 48 天前认领,目前没有进行中的 PR。 未关闭
难度 4/5 3-5 天 新手友好度 52/100
fastify/fastify-cli#806 · 1 条评论 · 7 个 reaction ·
-
Only watch specified directory (--watch CLI param)可能已有人在做 @josephreilly22 于 237 天前认领。 未关闭good first issue
难度 3/5 1-2 天 新手友好度 38/100
fastify/fastify-cli#787 · 5 条评论 · 2 个 reaction ·
查看 fastify/fastify-cli 的全部 Issue
相似的 Issue
-
Progress difficulty filter lists Hard before Medium可能已有人在做 @Pandamachi 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
sysprog21/codetrial#281 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 71/100
yjh051108/dsh-routing-suite#227 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
-
needs-triage release-watch
难度 1/5 1 小时以内 新手友好度 76/100
-
难度 2/5 1-3 小时 新手友好度 66/100
remoteintech/remote-jobs#2271 ·
维护者通常 1 天内回复