
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYou're really close! The key is to pass the parameters as the second argument to the fetch() method. Here's how to fix your code:
export const getServerSideProps = async ({ query }) => {
const { orderId } = query
const orderDetails = await sanityClient.fetch(
`*[_type == "orders" && orderId.current == $orderId][0]{...}`,
{ orderId } // Pass parameters as second argument
)
return {
props: {
orderDetails,
},
}
}The important bits:
getServerSideProps, you get the URL parameters from the context.query object (not router.query - that's for client-side components)sanityClient.fetch()$ prefixThis approach is much safer than string interpolation because the Sanity client automatically handles proper escaping, preventing GROQ injection attacks and syntax errors from special characters.
If you need to handle cases where orderId might not be present in the URL, you can add a check:
export const getServerSideProps = async ({ query }) => {
const { orderId } = query
if (!orderId) {
return { notFound: true }
}
const orderDetails = await sanityClient.fetch(
`*[_type == "orders" && orderId.current == $orderId][0]{...}`,
{ orderId }
)
return {
props: {
orderDetails,
},
}
}Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store