Troubleshooting downloadable documents in NextJS using Sanity.io schema and groq queries.
13 replies
Last updated: Jun 13, 2022
S
Hi all,this
opens a page with 404 error
but it should be a downloadable document .
<a href={`${mediaURL}?dl=`}>Manuscript</a>
but it should be a downloadable document .
Jun 13, 2022, 9:06 AM
S
Hello
user F
, can you share the way you query for mediaURL? I suspect that the data of the document (just like with images and videos) is not dereferenced yet. Could you share more Info please π
Jun 13, 2022, 9:29 AM
S
I use the groq like so
then I use it in the front-end like this
and return it like so
now it redirects me to a 404 page
const homepageQuery = groq`*[_type == "documentfile"]{ "documentURL": documentfile.asset->url }[0]`;
<a href={`${documentfile}?dl=`}>PDF</a>
return { props: { data: { documentfile, product: res.productByHandle, }, }, }; }
Jun 13, 2022, 9:35 AM
S
Also sinds I'm using nextjs I should probably use
instead of
right?
<link>
<a>
Jun 13, 2022, 9:38 AM
S
I will check your query in a sec, but with
<Link>and
<a>in NextJS:Link is used for any kind of internal routing, which also handles preload behaviour etc. Since you only use the url here for downloading I would stay with the normal
<a>.
Jun 13, 2022, 9:53 AM
S
Ah okay
Jun 13, 2022, 9:57 AM
S
And about your file issue, I think I need to understand your schema. It seems you have a
documentcalled
documentfilewhich in it has a
file, am I right?
Jun 13, 2022, 10:04 AM
S
this is what the schema looks like
export default { name: "documentfile", title: "DocumentFile", type: "document", fields: [ { title: 'Document', description: "Place document here", name: 'documentfile', type: 'file', fields: [ { name: 'description', type: 'string', title: 'Description' } ] }, ], };
Jun 13, 2022, 10:05 AM
K
What's the href like in your console in the end?
undefined?dl=or something?
Jun 13, 2022, 10:33 AM
S
Thank you for sharing. So I think that one problem with this schema is that you named the document and the file the same, which can be a but unclear and I suspect hinder the query. I would try something like this:
The query would look something like this.
But If you are embeding the query for
// documentfile.js export default { name: "documentfile", title: "DocumentFile", type: "document", fields: [ { title: 'Document', description: "Place document here", name: 'file', type: 'file', fields: [ { name: 'description', type: 'string', title: 'Description' } ] }, ], };
// query const homepageQuery = groq`*[_type == "documentfile"]{ "documentURL": file.asset->url }[0]`;
documentfilewithin the overall homepageQuery, you might want to try something like this:
//Query const homepageQuery = groq`*[_type == "PAGEDOCNAMEorID"]{ β¦, "documentURL": documentfile.file.asset->url }[0]`;
Jun 13, 2022, 10:41 AM
S
hmmm something is still off but I do get it returned in console
Jun 13, 2022, 11:11 AM
K
Then you should be able to use
documentfile.documentURL(careful with capitalization).
Jun 13, 2022, 11:12 AM
S
Ah yes that works thank you !
Jun 13, 2022, 11:14 AM
S
Wonderful! Thank you
user F
π Jun 13, 2022, 12:43 PM
Sanityβ build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.