How to fetch inside getStaticProps from the same domain. #13648
-
I'm trying to fetch inside a getStaticProps:
from the same domain as the client, using vercel functions. the problem is that i need change localhost with the corresponding deployment domain. and there is no way to know the deployment domain since vercel gives an arbitrary domain name. this is probably not the best approach, since i'm gonna change the domain later. if only there was a way to pass relative url to fetch, like what SWR does. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Update: // pages/api/posts/read/[slug].js
export function findPost(slug) {
return db.findPost(slug)
}
export default async (req, res) => {
const { query: { slug } } = req
const post = await findPost(slug)
res.json({ post })
}
// pages/post/[slug].js
import { findPost } from '...' // the path to the API file
export async function getStaticProps({ params }) {
const post = await findPost(params.slug)
return {
props: { post },
}
} |
Beta Was this translation helpful? Give feedback.
Update:
You can call your API code directly in the data-fetching methods, Next won't include this code in the browser bundle. Extract the API logic into a function and export it, then in your page you can import and use it: