英文:
Images not loading when deploying Next.js on GitHub Pages
问题
I have recently deployed my Next.js application on GitHub Pages. Everything seems to work fine, except for the fact that some images on my website are not loading properly.
The website is located at https://jamalakbara.github.io/bahana/ and the source code can be found at https://github.com/jamalakbara/bahana. When I run the application locally, all images are loading without any issues. However, when I deploy it on GitHub Pages, only some of the images are displayed while others are not.
Has anyone else experienced a similar issue when deploying Next.js on GitHub Pages? How can I resolve this issue and ensure that all images are properly displayed on my website?
I have checked the file paths and made sure that they are correct. I have also tried using different image formats, but the issue still persists.
英文:
I have recently deployed my Next.js application on GitHub Pages. Everything seems to work fine, except for the fact that some images on my website are not loading properly.
The website is located at https://jamalakbara.github.io/bahana/ and the source code can be found at https://github.com/jamalakbara/bahana. When I run the application locally, all images are loading without any issues. However, when I deploy it on GitHub Pages, only some of the images are displayed while others are not.
Has anyone else experienced a similar issue when deploying Next.js on GitHub Pages? How can I resolve this issue and ensure that all images are properly displayed on my website?
I have checked the file paths and made sure that they are correct. I have also tried using different image formats, but the issue still persists.
答案1
得分: 1
当您请求JS文件时,路径是这样的:https://jamalakbara.github.io/bahana/_next/static/kOjK2aFcF_hCMAU3ufAGg/_ssgManifest.js
。
当您请求图像时,路径是这样的:https://jamalakbara.github.io/img/logo.png
。
应该是:https://jamalakbara.github.io/bahana/img/logo.png
,如果您访问这个链接,您将看到您的资源确实显示出来了!
Github页面试图从https://jamalakbara.github.io/
提供图像内容。我们需要在每个图像请求中添加/bahana
。
这可能会变得非常繁琐,所以我会创建一个名为prefix.ts
的文件,内容如下:
const prefix = process.env.NEXT_PUBLIC_BASE_PATH || '';
export { prefix };
然后,在您使用Next的Image标签的任何地方,您需要将源更新为:
<Image src={`${prefix}/logo.png`} />
或者替换成相应的图像文件名。
英文:
When you make the request for the JS files the path is this: https://jamalakbara.github.io/bahana/_next/static/kOjK2aFcF_hCMAU3ufAGg/_ssgManifest.js
When you make the request for images it's this: https://jamalakbara.github.io/img/logo.png
It should be: https://jamalakbara.github.io/bahana/img/logo.png
and if you go to this you'll see that your asset does in fact appear!
Github pages is trying to serve the content from https://jamalakbara.github.io/ for images. We'll need to append the /bahana
to every image request.
That can get really tedious so how I would do it is make a file called prefix.ts
with the following:
const prefix = process.env.NEXT_PUBLIC_BASE_PATH || '';
export { prefix };
Then everywhere you use Next's Image tag you'd update the source to be:
<Image src={`${prefix}/logo.png`} />
or whatever the name of the image file is.
答案2
得分: -1
为解决CORS标头问题并允许访问jamalakbara.github.io,您可以按照以下步骤操作:
-
定位您的代码中发出对jamalakbara.github.io请求的位置。
-
在您的服务器端脚本的开头添加以下代码以启用CORS:
header('Access-Control-Allow-Origin: jamalakbara.github.io');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('Access-Control-Allow-Headers: X-Requested-With, Content-Type');
注意:如果需要,用适当的域名或通配符(*)替换jamalakbara.github.io。
- 保存并部署您的代码更改。
现在,您的服务器将允许来自jamalakbara.github.io域的请求。
英文:
To resolve the issue of CORS headers and allow access to jamalakbara.github.io, you can follow these steps:
Locate the file in your code where the request to jamalakbara.github.io is being made.
Add the following code at the beginning of your server-side script to enable CORS:
header('Access-Control-Allow-Origin: jamalakbara.github.io');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('Access-Control-Allow-Headers: X-Requested-With, Content-Type');
Note: Replace jamalakbara.github.io with the appropriate domain name or wildcard (*) if needed.
Save and deploy your code changes.
Now your server will allow requests from jamalakbara.github.io domain.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论