Next.JS app deploy in sub folder
Answered
White-winged Scoter posted this in #help-forum
White-winged ScoterOP
I want to deploy my Next.js project to
https://xyz.com/my-app, but my assets and <Link> tags are not functioning correctly. How can I resolve this issue?Answered by averydelusionalperson
IDK how you are deploying it, but u can use
https://nextjs.org/docs/app/api-reference/next-config-js/basePath
basePath ig.https://nextjs.org/docs/app/api-reference/next-config-js/basePath
18 Replies
IDK how you are deploying it, but u can use
https://nextjs.org/docs/app/api-reference/next-config-js/basePath
basePath ig.https://nextjs.org/docs/app/api-reference/next-config-js/basePath
Answer
@averydelusionalperson IDK how you are deploying it, but u can use `basePath` ig.
https://nextjs.org/docs/app/api-reference/next-config-js/basePath
White-winged ScoterOP
Actually
For example
Not working code
Working code
I have already added
<Link> tag working fine. But <Image> not working.For example
Not working code
<Image src="/images/logo.png" width="50px" height="50px" alt="logo" />Working code
<Image src="/my-app/images/logo.png" width="50px" height="50px" alt="logo" />I have already added
basePath in the next.config file, but the <Image> tag does not automatically append the path before the relative URL. Therefore, I am forced to manually add basePath before the relative URL.I think <Image /> here intentionally does not add the base path because you could need to use an image outside the nextjs app but in the same domain
@joulev If you import the image statically, does it work?
White-winged ScoterOP
Understood.
But what about metadata manifest file?
it's also not working
@White-winged Scoter But what about metadata manifest file?
Same reason. Instead of this, add app/manifest.json instead
Basically you probably should refrain from using the public folder wherever possible due to this basePath thingy
Some things might automatically get the base path, some don’t
So things that don’t depend on the path, such as statically imported images and file-based metadata, are immune to this problem
@joulev So things that don’t depend on the path, such as statically imported images and file-based metadata, are immune to this problem
White-winged ScoterOP
"file-based metadata" means?
its a string
@White-winged Scoter "file-based metadata" means?
Meaning instead of adding manifest.json to public and reference it in a metadata export, you add the manifest.json file in app/. https://nextjs.org/docs/app/api-reference/file-conventions/metadata
For manifest.json specifically, https://nextjs.org/docs/app/api-reference/file-conventions/metadata/manifest
White-winged ScoterOP
@joulev thanks for your answer. I think it's working fine now.
Thank you for your help 👍